Repository navigation
Conversation
KuSh
marked this pull request as draft
August 30, 2026 22:21
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
7 times, most recently
from
September 1, 2026 11:24
c8f7a6f to
96f3df4
Compare
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
from
September 2, 2026 21:44
96f3df4 to
47d9bab
Compare
KuSh
marked this pull request as ready for review
September 2, 2026 21:44
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
2 times, most recently
from
September 17, 2026 22:37
5760d52 to
e86c79e
Compare
2 tasks done
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
2 times, most recently
from
September 28, 2026 01:39
0dfdc92 to
ff1d8ed
Compare
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
2 times, most recently
from
September 28, 2026 03:25
7fb7a5b to
51c858d
Compare
- as stdin so the rewrite keeps diff's exit contract- as stdin so the rewrite keeps the exit contract
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
2 times, most recently
from
October 3, 2026 15:04
0784c33 to
8c35b07
Compare
`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]>
KuSh
force-pushed
the
fix/diff-dash-stdin-operand
branch
from
October 19, 2026 16:21
8c35b07 to
6afa3d6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #3780.
The bug
rtk diffhanded-straight to the file read, so it looked for a file literally named-and reported trouble wherediffcompares stdin with the other operand:It reaches users two ways: the hook rewrites a bare
diff - expectedthat inherits its stdin intortk diff - expected(a piped or redirecteddiffis not rewritten), and callers runrtk diff - expecteddirectly.cmd | rtk diff - expected && <on-success>silently stopped running its success leg.rtk log -had the same defect.The fix
utils::is_stdin_operandis the one answer to "this operand is-", used by theread,json,loganddiffdispatch sites and bydiff's operand reading;searchkeeps its own scan of its path list.rtk log -reaches the existingrun_stdin.diff_cmdresolves 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.diff - missing.txtexits 2 and namesmissing.txt. The file operand is read before stdin, so an unreadable path fails without draining the producer.- -, a name given twice (looked up once, asdiffdoes), and on Unixdiff's ownsame_filerule — 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 throughutils::stdin_file/utils::stdin_metadata, whichsearchnow uses too.rtk bypass diff.diff/loghelp and thertk diffsyntax 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)diffrtk diff- -- missing.txt,missing.txt -- f.txt < f.txt{ head -c 6; diff - f.txt; } < f.txt- /dev/stdin,/dev/stdin /dev/stdin(Linux)f.txt f.txt, hard link, symlinkThree inputs still differ: closed stdin next to a file (
<&-) — the Rust runtime reopens a closed fd 0 on/dev/nullbeforemain, so rtk sees empty input wherediffexits 2; a directory given twice keeps rtk's exit 2 for any directory operand; a FIFO given twice is answered at once, wherediffwaits on its open. On macOS,/dev/stdinnext to a stdin pipe does not stat alike (devfsfdescmasks 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.rsspawns the real binary and reads$?, so each value is pinned afterrun_cliandstd::process::exit, not just as whatdiff_cmd::runreturns. 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 andrtk log -. The spawns go throughcommon::rtk_command; the memory-capped case setsRLIMIT_ASfrompre_execinstead of wrapping rtk insh -c 'ulimit -v', so rtk stays the command's own program andtests/commonis untouched. With the metadata fast path disabled the capped child dies in 0.06s withrtk diff: /dev/zero: out of memoryand exit 2, so the cap is what makes the regression fail fast rather than at the deadline.Scope
rtk difffilters only the bare two-operand form; any flag falls through to the realdiff. A single file operand (rtk diff o.txt) is the usage error #4383 landed for #4320, kept here; a barertk diff -stays in #3780.cargo fmt --all -- --check,cargo clippy --all-targets -- -D warningsandcargo test --allare clean; CI is green on Ubuntu, macOS and Windows.