fix(fs_write): suppress spurious trailing-newline diff lines#3717
Open
sandikodev wants to merge 1 commit intoaws:mainfrom
Open
fix(fs_write): suppress spurious trailing-newline diff lines#3717sandikodev wants to merge 1 commit intoaws:mainfrom
sandikodev wants to merge 1 commit intoaws:mainfrom
Conversation
When the only difference between the old and new file content is a
trailing newline, similar::TextDiff produces a Delete/Insert pair for
the last line that clutters the diff output shown to the user.
Normalize both sides with trim_end_matches('\n') before diffing so
that a trailing-newline-only change is not shown as a modification.
Fixes aws#923
1ff0155 to
1053fb5
Compare
sandikodev
added a commit
to sandikodev/amazon-q-developer-cli
that referenced
this pull request
Apr 4, 2026
The codebase has insta as a workspace dependency but its usage is minimal and undocumented. Contributors currently have no guidance on: - when to use assert_eq!/assert! vs insta snapshot testing - how to run the cargo-insta review workflow - how to handle ANSI escape codes in terminal output tests Add TESTING.md at the repo root covering: - Quick reference table for choosing assertion style - Standard assertion examples - Full insta workflow (write -> run -> review -> accept) - ANSI escape code stripping pattern - Real example referencing the pattern used in PR aws#3717 Add a one-line pointer in CONTRIBUTING.md.
Author
|
Update (amended commit): The fix was originally using Three tests were added covering all scenarios:
Tests use |
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.
Issue
Closes #923
Problem
When
fs_writemodifies a file, the diff shown to the user always has a spurious last line:The content is identical — the only difference is a trailing newline. This is a cosmetic artefact caused by
write_to_filealways appending\nto file content, while the diff compares the raw strings before normalization.Fix
Strip at most one trailing newline from both sides before passing to
TextDiff::from_lines:strip_suffix(nottrim_end_matches) is used deliberately — it removes exactly one trailing newline so that intentional multi-newline differences (e.g. old has 1 trailing newline, new has 3) are still shown.Testing
Three tests cover all scenarios:
test_print_diff_trailing_newline_only_change_is_not_shown— the bug case: trailing-newline-only diff produces zero deletions and zero insertionstest_print_diff_real_change_is_shown— regression guard: a genuine content change still appears (usesinsta::assert_snapshot!for the exact formatted output)test_print_diff_multiple_trailing_newlines_difference_is_shown— edge case: intentional extra newlines are still shown as insertionsTests use
strip_ansi_escapes(already in the workspace) to strip terminal color codes before asserting.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.