Repository navigation
fix(diff): rtk diff reports CRLF/LF-only differences as identical - #2643
Closed
amitmishra11 wants to merge 1 commit into
Closed
amitmishra11 wants to merge 1 commit into
amitmishra11 wants to merge 1 commit into
Conversation
str::lines() strips \r along with \n, so two files whose textual lines match but whose raw bytes differ (CRLF vs LF terminators, or a missing trailing newline) collapsed to an empty diff and were reported as identical with exit 0, even though `diff`/git see them as different. render_file_diff now falls back to a byte comparison when the line-based diff finds no changes: true byte-equal files are still "identical" (exit 0), but a byte difference invisible to lines() is now surfaced with a description (CRLF vs LF, trailing newline, or other) and exit 1, matching diff-convention behavior. Fixes rtk-ai#2627
Collaborator
|
Closing as a duplicate of #3519, which adds the same byte-equality guard before the "identical" verdict. The bug is confirmed live on 0.46.0: #3519 is the narrower change (57 lines, one file) and has a full green test matrix; this PR only ever ran That said, |
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.
Summary
Fixes #2627.
rtk diff <file1> <file2>reported two files as identical (exit 0) when they differed only in line-ending bytes (CRLF vs LF) — or, more generally, in anythingstr::lines()normalizes away (it strips\ralong with\n, and treats a missing trailing newline as just "one fewer empty trailing line").Root cause
render_file_diffsplits both files withcontent.lines()before comparing (src/cmds/git/diff_cmd.rs).str::lines()treats\r\nand\nas equivalent line terminators, so a CRLF file and an LF file with the same text collapse to identical line vectors, andcompute_difffinds zero changes — the early-return then reports "identical" with no byte comparison.Fix
In
render_file_diff, when the line-based diff finds no changes, fall back to an explicit byte comparison (content1 == content2) before declaring the files identical:describe_byte_only_difference: CRLF vs LF, trailing newline, or generic), exit 1 (diff convention).Scope note
The issue also flags
condense_unified_diff(this file) andcompact_diff(src/cmds/git/git.rs) as sharing the samestr::lines()pattern, but says those paths weren't independently verified to reproduce the bug. I scoped this PR to the confirmed, reproducible case (rtk diff <file> <file>/render_file_diff) rather than changing unverified code paths. Happy to follow up on those separately if confirmed.Test plan
src/cmds/git/diff_cmd.rs: CRLF-vs-LF (reproduces the exact issue), trailing-newline-only diff, and a same-CRLF-on-both-sides case to confirm true identical files still short-circuit correctly. Also unit tests for the newdescribe_byte_only_differencehelper.cargo test— could not run locally (no working MSVC/64-bit linker in my sandbox; see comment on fix: COPILOT_INSTRUCTIONS example drops kubectl get subcommand #2642 for details). The change is isolated torender_file_diffand a new pure helper function with no external dependencies; existing tests for that function (test_render_identical_files_exit_zero,test_render_modified_only_yaml_not_identical, etc.) are untouched and should still pass since the byte-equal fast path is preserved. Appreciate CI confirming.