Skip to content

fix(diff): report a one-operand call as a usage error with exit 2 (fixes #4320) - #4383

Merged
KuSh merged 3 commits into
rtk-ai:developfrom
TechWizard9999:fix/4320-diff-one-operand-usage
Oct 3, 2026
Merged

KuSh merged 3 commits into
rtk-ai:developfrom
TechWizard9999:fix/4320-diff-one-operand-usage

Conversation

@TechWizard9999

Copy link
Copy Markdown
Contributor

Summary

Fixes #4320: rtk diff <file> with a single file operand treated it as the stdin form — it ignored the file, echoed stdin back as a diff to condense, and exited 0. Real diff reports a usage error and exits 2.

Changes

  1. src/main.rs — Commands::Diff dispatch: with no second file, only the - operand reads a piped diff from stdin; a real file operand alone now prints diff's own message (diff: missing operand after '<file>') and exits 2, before opening the file — the same order real diff uses, so a missing operand is reported too.
  2. src/main.rs — the Diff variant doc now states the full contract: 0 identical, 1 different, 2 read error or usage error; - is the stdin form.

Testing

  • one_file_operand_is_a_usage_error_not_a_stdin_condense: exits 2 with the missing operand after message, stdin not echoed back
  • explicit_stdin_dash_still_condenses: rtk diff - keeps reading a piped diff and exits 0
  • cargo fmt --all -- --check, cargo clippy --all-targets, and cargo test --all pass

@KuSh could you please review?

Real `diff <file>` with one operand reports `diff: missing operand
after '<file>'` and exits 2 before opening the file. rtk treated the
single operand as the stdin form, ignored the file, echoed stdin back
as a diff to condense, and exited 0 — so a `diff <file> && next` step
ran where `diff` would stop, and a missing operand was never reported.

The `-` operand still reads a piped diff from stdin; only a real file
operand alone takes the usage-error path. The hook rewrites a bare
`diff <file>` to `rtk diff <file>`, so the command an agent wrote now
fails where the tool it stands in for fails.

Fixes rtk-ai#4320.
- one_file_operand_is_a_usage_error_not_a_stdin_condense: `rtk diff
  <file>` exits 2 with diff's `missing operand after` message, and
  stdin is not echoed back as a diff.
- explicit_stdin_dash_still_condenses: `rtk diff -` keeps reading a
  piped diff from stdin and exits 0.
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working cli diff labels Oct 1, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes a bug where rtk diff <file> with a single file operand was incorrectly treated as the stdin form, silently echoing stdin back as a diff and exiting 0. Now only rtk diff - triggers stdin mode, while a lone file operand prints a missing operand usage error and exits 2, matching real diff's behavior.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4320


Analyzed automatically by wshm · This is an automated analysis, not a human review.

The one-operand test ran with stdin closed and an existing file, so a
branch that still condensed stdin, or opened the operand first, passed it.
Pipe a diff in, name a missing file, and assert empty stdout, the usage
error and no read error. Also state the comment positively, give FILE2's
help the real rule, and replace the FEATURES.md line that documented the
removed form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 1 of 3 — APPROVED

Claim: rtk diff <file> with a single file operand reports diff's usage error and exits 2 instead of condensing stdin; rtk diff - keeps condensing a piped unified diff.
Scope: accept (round 1, frozen). #3781 edits the same Diff doc comment and FEATURES.md line; this one lands first, #3781 rebases onto it.
Ran: base 3a12dfe37 vs head × 17 operand forms vs GNU diff 3.12, LC_ALL=C: one operand (existing file, missing file, spaced name, ./-, /dev/stdin, -- o.txt) with stdin from a file and from /dev/null; - with a piped diff and with /dev/null; two operands identical / different / missing; - o.txt, o.txt -; the forms clap hands to diff ('', -u o.txt, three operands). Invariants: under the hook (rtk rewrite "diff o.txt" → rtk diff o.txt; rtk diff o.txt && echo next now stops, rc 2), every decision, exact conditions (stdin /dev/null and a piped file). Savings: n/a — the new path prints only the usage error; the - and two-file paths are byte-identical before/after.
Pins: 3 decisions (- guard, exit 2, usage error before any read), 7 mutated, 2 survived the PR's own tests (echo stdin before the error; open the operand before the error) — both killed by the fixup's test.
Matrix: executed this round; pin check: executed this round
CI @b17a355b2: all green (11/11)

Blocks merge (0)

Checked and correct — no need to re-verify

  • Every one-operand file form now matches GNU diff: rc 2, diff: missing operand after '<f>', nothing on stdout, the operand never opened (a missing file gets the usage error, not a read error).
  • Only a byte-exact - reaches the stdin mode; ./- and /dev/stdin get the usage error, as with diff.
  • Two-operand results and exit codes (0/1/2) are unchanged, as are the clap-fallback forms that already ran the real diff.
  • fmt, clippy and cargo test --all pass on the head and on the head merged onto current develop.

Your questions

  • "@KuSh could you please review?" → done, this is the review.

Next

Fixup b17a355 pushed, code-reviewed: it adds one_missing_operand_is_a_usage_error_before_any_read (piped diff on stdin, missing operand → rc 2, usage error, no read error, empty stdout), which fails if the branch condenses stdin or opens the file before erroring; it also states the branch comment without history, gives file2's help the real rule (omit only when the first operand is -), and replaces the FEATURES.md line that still documented rtk diff <fichier1> as "stdin as second file". Approving.
Rounds: 1/3. Threads: 0 open, 0 resolved this round.

@KuSh
KuSh merged commit 1ed8c7c into rtk-ai:develop Oct 3, 2026
11 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cli diff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk diff <file> with one operand echoes stdin and exits 0 where diff exits 2

2 participants