Skip to content

fix(diff): exit 2 on unreadable operand per POSIX diff convention#2473

Open
hgunduzoglu wants to merge 1 commit into
rtk-ai:developfrom
hgunduzoglu:fix/diff-error-exit-code-posix
Open

fix(diff): exit 2 on unreadable operand per POSIX diff convention#2473
hgunduzoglu wants to merge 1 commit into
rtk-ai:developfrom
hgunduzoglu:fix/diff-error-exit-code-posix

Conversation

@hgunduzoglu

Copy link
Copy Markdown
Contributor

Summary

Completes #2446. rtk diff now follows the full POSIX diff exit-code convention:

Scenario Before #2394 After #2394 (merged) This PR
Files identical 0 ✓ 0 ✓ 0 ✓
Files differ 0 ✗ 1 ✓ 1 ✓
Unreadable operand (missing file, etc.) 1 ✗ 1 ✗ 2 ✓

The differing-files row was resolved by #2394 (merged). This PR closes the remaining gap: I/O errors.

Root cause

run() read both operands with ?. A read failure propagated up to main()'s error handler, which returns exit 1 for any Err — indistinguishable from "files differ". So a typo'd path or a deleted file was silently reported as a difference (exit 1) instead of trouble (exit 2).

Fix

Read the operands explicitly via a small read_operands helper: on any I/O error it prints a diff-style message to stderr and returns exit code 2, mirroring system diff. No change to the identical/differ paths.

$ rtk diff one.yaml missing.yaml; echo "exit:$?"
rtk diff: missing.yaml: No such file or directory (os error 2)
exit:2                  # was: exit:1

$ diff one.yaml missing.yaml; echo "exit:$?"   # system diff, for parity
diff: missing.yaml: No such file or directory
exit:2

Testing

  • 4 new unit tests: first-operand-missing → 2, second-operand-missing → 2, both-readable → Ok, and exit-2 distinct from 0/1. Paths built from CARGO_MANIFEST_DIR so they don't depend on cwd.
  • cargo fmt --all --check && cargo clippy --all-targets && cargo test — clean, 2203 passed.
  • Manual verification against system diff for all three exit codes (0/1/2).

No output-format change, so token savings are unaffected (the error path prints a one-line stderr message, same as before).

rtk diff read its operands with `?`, so a missing or unreadable file
collapsed to exit 1 in main's error handler — indistinguishable from
"files differ". POSIX diff reserves exit 2 for trouble (I/O errors),
1 for differences, 0 for identical. Read operands explicitly, print a
diff-style message to stderr, and return 2 on failure.

The differing-files case (exit 0 -> 1) was already fixed by rtk-ai#2394.

Fixes rtk-ai#2446
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant