Skip to content

fix(diff): compare files byte-accurately instead of by normalized lines - #3471

Closed
Hydr0gen19 wants to merge 1 commit into
rtk-ai:developfrom
Hydr0gen19:fix/issue-3469
Closed

Hydr0gen19 wants to merge 1 commit into
rtk-ai:developfrom
Hydr0gen19:fix/issue-3469

Conversation

@Hydr0gen19

Copy link
Copy Markdown
Contributor

Summary

  • rtk diff a b compared files with str::lines(), which drops the line terminator. CRLF vs LF and a missing final newline produced an empty change list, so byte-different files were reported as [ok] Files are identical and exited 0. Comparison is now byte-accurate: only strictly equal contents exit 0.
  • When the bytes differ but every line matches, the terminators are the only possible cause, so the output says that explicitly instead of claiming the files are identical.
  • Added --ignore-whitespace to keep the lenient comparison available on request (trims trailing whitespace, ignores line endings and the final newline).

Fixes #3469

Why this matters beyond rtk diff

src/discover/rules.rs rewrites plain diff <args> to rtk diff, so an agent
running diff a b got the false-equal answer and the 0 exit code without ever
asking for rtk. That is exactly the case the issue describes as breaking
diff-based equality checks.

Behaviour

Input Before After
identical bytes [ok] Files are identical, exit 0 unchanged
CRLF vs LF [ok] Files are identical, exit 0 identical line content; files differ in line endings or the final newline, exit 1
final newline present vs absent [ok] Files are identical, exit 0 same as above, exit 1
line content differs change list, exit 1 unchanged
--ignore-whitespace, whitespace-only difference n/a [ok] Files are identical (ignoring whitespace), exit 0

Trailing-space differences were already reported as changes before this PR; they
still are, and --ignore-whitespace now suppresses them.

Note: for very small files guard::never_worse still prefers the raw content
over the condensed message, as it does for every other rtk diff output. The
exit code — the part scripts rely on — is correct in every case.

On the added flag

--ignore-whitespace is the one part of this PR that is not strictly the bug
fix. It is here because the triage comment on the issue asks for the lenient
comparison to stay available behind an explicit flag, and without it this change
removes a behaviour and leaves nothing in its place. It is about ten isolated
lines — happy to drop it and keep the fix minimal if you would rather.

It deliberately has no -w short form: in GNU diff, -w means "ignore all
white space", which is a broader thing than what this does.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets && cargo test
    (clippy clean; see note below on two pre-existing flaky tests)
  • Unit tests added in src/cmds/git/diff_cmd.rs: CRLF vs LF, missing final
    newline, trailing space, byte-identical CRLF files, and both
    --ignore-whitespace paths. They fail on develop and pass here.
  • Manual testing:
$ printf 'alpha\r\nbeta\r\n' > crlf.txt; printf 'alpha\nbeta\n' > lf.txt

# before
$ rtk diff crlf.txt lf.txt
[ok] Files are identical
$ echo $?
0

# after
$ rtk diff crlf.txt lf.txt
crlf.txt → lf.txt
   identical line content; files differ in line endings or the final newline
$ echo $?
1

$ rtk diff --ignore-whitespace crlf.txt lf.txt
[ok] Files are identical (ignoring whitespace)
$ echo $?
0

Two tests (core::tracking::tests::test_timed_execution_* and
cmds::dotnet::dotnet_trx::tests::test_parse_trx_files_in_dir_since_ignores_older_files)
fail intermittently on cargo test --all in my environment and pass when run on
their own. They are unrelated to this change — different runs fail on different
tests, and nothing here touches tracking or dotnet.

Docs

docs/usage/FEATURES.md — added the --ignore-whitespace option and a note on
the byte-accurate comparison and exit codes.

`rtk diff a b` split both files with `str::lines()`, which drops the line
terminator. CRLF vs LF and a missing final newline therefore produced an
empty change list, so byte-different files were reported as
"[ok] Files are identical" and exited 0.

Since the `diff` rewrite rule routes plain `diff a b` through rtk, this
made rtk unusable as an equality oracle in scripts and agent workflows.

Byte equality is now the only path to exit 0. When the contents differ
but every line matches, the terminators are the only possible cause and
the output says so instead of claiming the files are identical.

`--ignore-whitespace` keeps the lenient comparison available: it trims
trailing whitespace and ignores line endings and the final newline.
@KuSh

KuSh commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #3519, which adds the same byte-equality guard for #3469.

Your version is the more thorough of the two on the guard itself — checking content1 == content2 up front, before split_lines, is the cleaner placement, and the test_render_byte_identical_exit_zero_with_crlf case (both files CRLF, must still be identical) is one the others miss.

What tipped it to #3519 is scope. This PR also adds a --ignore-whitespace CLI flag, changes diff_cmd::run's signature, touches main.rs and edits docs/usage/FEATURES.md — none of which #3469 asks for, and each of which is a separate review surface. It also only ever ran check-target (skipped), so the flag's behaviour was never CI-verified. #3519 is 57 lines in one file with a full green matrix.

If you'd still like --ignore-whitespace, it's worth its own PR on top — it's a reasonable feature, just not one that should ride along with a correctness fix.

@KuSh KuSh closed this Aug 30, 2026
@Hydr0gen19

Copy link
Copy Markdown
Contributor Author

Fair call, and the scope point lands. I flagged --ignore-whitespace in the description as the part that was not the bug fix, and then shipped it in the same PR anyway — saying so does not make it a smaller review surface. A correctness fix and a new CLI option should not share one.

The CI note is the more useful half for me. I ran the suite in a container and treated that as covered, without checking whether the matrix had actually exercised the flag. It had not.

If it is any use: test_render_byte_identical_exit_zero_with_crlf — both files CRLF, identical bytes, must still exit 0 — is self-contained on this branch and depends on nothing else in it. Lift it into #3519 as-is if you want it, no need to wait on me or credit anything.

If --ignore-whitespace still looks worth having once #3519 is in, I will open it on its own.

@Hydr0gen19

Copy link
Copy Markdown
Contributor Author

Saw #3519 land with the CRLF case in it — thanks for routing the test over rather than letting it go with the PR. Better outcome than merging mine would have been: the fix stayed one focused change, and the coverage still made it in.

The scope point stuck, for what it is worth. Obvious in hindsight, and not something I was going to see from inside my own diff.

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.

2 participants