Skip to content

Commit 1ff0155

Browse files
committed
fix(fs_write): suppress spurious trailing-newline diff lines
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 #923
1 parent e14ea18 commit 1ff0155

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

crates/chat-cli/src/cli/chat/tools/fs_write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,11 @@ fn print_diff(
641641
new_str: &StylizedFile,
642642
start_line: usize,
643643
) -> Result<()> {
644-
let diff = similar::TextDiff::from_lines(&old_str.content, &new_str.content);
644+
// Normalize trailing newlines so that a file ending with "\n" vs without one does not
645+
// produce a spurious diff line. See: #923
646+
let old_content = old_str.content.trim_end_matches('\n');
647+
let new_content = new_str.content.trim_end_matches('\n');
648+
let diff = similar::TextDiff::from_lines(old_content, new_content);
645649

646650
// First, get the gutter width required for both the old and new lines.
647651
let (mut max_old_i, mut max_new_i) = (1, 1);

0 commit comments

Comments
 (0)