Skip to content

fix(diff,log): read - as stdin so the rewrite keeps the exit contract - #3781

Open
KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:fix/diff-dash-stdin-operand
Open

KuSh wants to merge 1 commit into
rtk-ai:developfrom
KuSh:fix/diff-dash-stdin-operand

Conversation

@KuSh

@KuSh KuSh commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #3780.

The bug

rtk diff handed - straight to the file read, so it looked for a file literally named - and reported trouble where diff compares stdin with the other operand:

$ printf 'alpha\nbeta\n' > other.txt
$ printf 'alpha\nbeta\n' | diff - other.txt; echo "exit=$?"
exit=0
$ printf 'alpha\nbeta\n' | rtk diff - other.txt; echo "exit=$?"     # before
rtk diff: -: No such file or directory (os error 2)
exit=2

It reaches users two ways: the hook rewrites a bare diff - expected that inherits its stdin into rtk diff - expected (a piped or redirected diff is not rewritten), and callers run rtk diff - expected directly. cmd | rtk diff - expected && <on-success> silently stopped running its success leg. rtk log - had the same defect.

The fix

  • utils::is_stdin_operand is the one answer to "this operand is -", used by the read, json, log and diff dispatch sites and by diff's operand reading; search keeps its own scan of its path list. rtk log - reaches the existing run_stdin.
  • diff_cmd resolves both operands together, since stdin can only be drained once. Stdin is read as bytes like a file, sharing one reader with the unified-diff path, so non-UTF-8 input takes the byte comparison.
  • A read failure carries the operand that failed: diff - missing.txt exits 2 and names missing.txt. The file operand is read before stdin, so an unreadable path fails without draining the producer.
  • Two operands that are one file are answered from metadata alone, without opening or reading: - -, a name given twice (looked up once, as diff does), and on Unix diff's own same_file rule — one device and inode plus matching mode, link count, owner, group, size and timestamps, or two device nodes for one device. For a regular-file stdin the size counted is what is left from its current offset, so a partly read redirect is compared rather than called identical. Directories are left to the read, which reports them as before. The stdin descriptor is inspected through utils::stdin_file/utils::stdin_metadata, which search now uses too.
  • When an operand was stdin, a refusal to list says stdin was consumed and has to be fed again to rtk bypass diff.
  • The diff/log help and the rtk diff syntax in FEATURES.md describe - on either operand, and exit 2 as an operand (file or stdin) that cannot be read.

Parity with GNU diff (3.12, LC_ALL=C)

invocation diff rtk diff
stdin first / second, identical 0 0
stdin first / second, differing 1 1
- - 0 0
- missing.txt, missing.txt - 2 2
non-UTF-8 stdin, identical / differing 0 / 1 0 / 1
- f.txt < f.txt 0 0
{ head -c 6; diff - f.txt; } < f.txt 1 1
piped - /dev/stdin, /dev/stdin /dev/stdin (Linux) 0 0
f.txt f.txt, hard link, symlink 0 0

Three inputs still differ: closed stdin next to a file (<&-) — the Rust runtime reopens a closed fd 0 on /dev/null before main, so rtk sees empty input where diff exits 2; a directory given twice keeps rtk's exit 2 for any directory operand; a FIFO given twice is answered at once, where diff waits on its open. On macOS, /dev/stdin next to a stdin pipe does not stat alike (devfs fdesc masks the mode by the descriptor's access bits), so both are read — reasoned from XNU's source, not observed on a Mac.

Tests

tests/diff_exit_code_test.rs spawns the real binary and reads $?, so each value is pinned after run_cli and std::process::exit, not just as what diff_cmd::run returns. It covers each stdin shape, the guard branch that renders the same verdict in two shapes, - - and stdin aliases answered without reading (pipe kept open, with a deadline), a partly read redirect, a mode-000 file given twice (which only the metadata path can pass), an endless device given twice (Linux, under a memory cap), a missing operand, non-UTF-8 stdin, the refusal hint and rtk log -. The spawns go through common::rtk_command; the memory-capped case sets RLIMIT_AS from pre_exec instead of wrapping rtk in sh -c 'ulimit -v', so rtk stays the command's own program and tests/common is untouched. With the metadata fast path disabled the capped child dies in 0.06s with rtk diff: /dev/zero: out of memory and exit 2, so the cap is what makes the regression fail fast rather than at the deadline.

Scope

rtk diff filters only the bare two-operand form; any flag falls through to the real diff. A single file operand (rtk diff o.txt) is the usage error #4383 landed for #4320, kept here; a bare rtk diff - stays in #3780.

cargo fmt --all -- --check, cargo clippy --all-targets -- -D warnings and cargo test --all are clean; CI is green on Ubuntu, macOS and Windows.

@KuSh
KuSh marked this pull request as draft August 30, 2026 22:21
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch 7 times, most recently from c8f7a6f to 96f3df4 Compare September 1, 2026 11:24
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch from 96f3df4 to 47d9bab Compare September 2, 2026 21:44
@KuSh
KuSh marked this pull request as ready for review September 2, 2026 21:44
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch 2 times, most recently from 5760d52 to e86c79e Compare September 17, 2026 22:37
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch 2 times, most recently from 0dfdc92 to ff1d8ed Compare September 28, 2026 01:39
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch 2 times, most recently from 7fb7a5b to 51c858d Compare September 28, 2026 03:25
@KuSh KuSh changed the title fix(diff): read - as stdin so the rewrite keeps diff's exit contract fix(diff,log): read - as stdin so the rewrite keeps the exit contract Sep 28, 2026
@KuSh
KuSh force-pushed the fix/diff-dash-stdin-operand branch 2 times, most recently from 0784c33 to 8c35b07 Compare October 3, 2026 15:04
`rtk diff` handed `-` straight to the file read, so it looked for a file
literally named "-", failed with ENOENT and reported trouble where
`diff` compares stdin with the other operand. That reaches users two
ways: the hook rewrites a bare `diff - expected` that inherits its stdin
into `rtk diff - expected` (a piped or redirected `diff` is not
rewritten), and callers run `rtk diff - expected` directly. For
identical input `diff` exits 0, so `cmd | rtk diff - expected &&
<on-success>` silently stopped running its success leg — a verification
inverted by a filter that is supposed to be transparent. `rtk log -`
had the same defect.

Four commands had grown four different answers to "this operand may be
`-`": `read` and `json` compared against `Path::new("-")` at dispatch,
`search` scanned its path list, `log` and `diff` did not handle it at
all. Add `utils::is_stdin_operand` and route the `read`, `json`, `log`
and `diff` dispatch sites and `diff`'s operand reading through it;
`search` keeps its own scan of its path list.

`diff` needs more than a predicate, because it has two operands and
stdin can only be drained once, so `diff_cmd` resolves both operands
together. Stdin is read as bytes like a file operand, sharing one
reader with the unified-diff path, so non-UTF-8 input takes the same
byte comparison, and a read failure carries the operand that failed, so
`diff - missing.txt` exits 2 and names `missing.txt` rather than `-`.
The file operand is read before stdin, so an unreadable path fails
without draining the producer first. `diff - -` names one stream twice
and is identical by construction: it answers with the identical verdict
and leaves stdin unread, as `diff` does, instead of finding an empty
second operand. When an operand was stdin, a refusal to list says
stdin was consumed and has to be fed again to `rtk bypass diff`, which
holds for a pipe and for an inherited redirect alike, and the
edit-script refusal now separates that hint with `;` like the other
refusals.

Any two operands that are one file are answered like `- -`, from
metadata alone, so neither is opened or read. On Unix that is `diff`'s
own `same_file` rule: one device and inode plus matching mode, link
count, owner, group, size and timestamps, or two device nodes for one
device. This covers `/dev/stdin /dev/fd/0`, the file stdin is redirected
from and a link to the other operand. A stdin pipe next to `/dev/stdin`
(`- /dev/stdin`) is recognised on Linux, where `/dev/stdin` stats to the
pipe itself; on macOS it is a devfs `fdesc` node whose mode is masked by
the descriptor's access bits, unlike `fstat` of stdin, so the rule does
not match and both are read. One name given twice is looked up once, as
`diff` does, so a file that changes between two lookups, a log being
appended to for instance, is still one file; this part holds on every
platform. For a regular-file stdin the size counted is what is left from
stdin's current offset, as in `diff`, so a partly read redirect is
compared rather than called identical. Directories are left to the read,
which reports them as before. Where `diff` has to open an operand first,
a FIFO given twice for instance, rtk answers without waiting on the
open. Elsewhere two different names are both read. The stdin descriptor
is inspected through `utils::stdin_file` and `utils::stdin_metadata`,
which `search` now uses for its own stdin check.

`log` takes the same dispatch route as `read` and `json`, so `rtk log -`
reaches the existing `run_stdin` rather than `run_file`. That keeps its
analytics row as `log (stdin)` instead of a bogus `cat -`, keeps `-v`
from printing `Analyzing log: -`, and keeps `rtk log -` and `rtk log` on
the same newline handling — `run_stdin` rebuilds lines, so a CRLF log
dedups identically either way.

The `diff` and `log` help and the `rtk diff` syntax in FEATURES.md now
describe `-` on either operand, alongside the single-`-` condense and
the single-file usage error, and widen exit 2 from a file that cannot be
read to any operand, stdin included.

Exit codes now match GNU diff on all four shapes: stdin first, stdin
second, both, and neither, including an unreadable file operand next to
stdin and, on Unix, two names for one file by `diff`'s stat rule. Three
inputs still differ. A `-` operand next to a file with stdin closed
(`<&-`): the Rust runtime reopens a closed fd 0 on /dev/null before
`main` runs, so rtk reads it as empty input where `diff` reports `-: Bad
file descriptor` and exits 2. A directory given twice keeps the exit 2
rtk gives any directory operand, where `diff` compares directories. A
FIFO given twice is answered at once, where `diff` waits on its open.

Adds tests/diff_exit_code_test.rs, which spawns the real binary and
reads `$?` for each stdin shape, so the value is pinned after `run_cli`
and `std::process::exit`, not just as what `diff_cmd::run` returns. It
also pins the case that is easy to break — the never-worse guard picks
rtk's rendering or the raw baseline by token count, so the same verdict
reaches the user in two shapes depending on file size, and the exit code
must not vary with that choice.

The spawns are built with `common::rtk_command`, so the children's data
never lands in the developer's own. The case that caps address space sets
`RLIMIT_AS` from `pre_exec` rather than wrapping rtk in `sh -c 'ulimit
-v'`, so the binary stays the command's own program and nothing outside
`tests/common` has to name it.

Refs rtk-ai#3780.

Co-Authored-By: Claude Opus 5 <[email protected]>
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