Repository navigation
fix(core): normalize CRLF line endings when computing write_file diffs and diffStat - #30489
bitwizard25 wants to merge 2 commits into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where files with CRLF line endings were causing misleading diffs and inflated telemetry in the write_file tool. By normalizing line endings to LF specifically for the diffing and statistical analysis processes, the tool now correctly identifies only the actual changes made to the content, while ensuring that the original file's line ending format is preserved upon writing to disk. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
📊 PR Size: size/M
|
🛑 Action Required: Evaluation ApprovalSteering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged. Maintainers:
Once approved, the evaluation results will be posted here automatically. |
There was a problem hiding this comment.
Code Review
This pull request introduces CRLF line ending normalization when generating diffs and calculating diff statistics in the WriteFileTool. Specifically, it normalizes both original and proposed content from CRLF to LF before passing them to Diff.createPatch and getDiffStat. This ensures that line ending differences do not clutter the diff outputs or distort the added/removed line counts, while still preserving CRLF line endings on disk. A comprehensive unit test has been added to verify this behavior. I have no additional feedback to provide.
| ? '' // Or some indicator of unreadable content | ||
| : originalContent; | ||
| : originalContent.replace(/\r\n/g, '\n'); | ||
| const normalizedFileContent = fileContent.replace(/\r\n/g, '\n'); |
There was a problem hiding this comment.
The diffs are now computed on LF-normalized text, but the write only rewrites line endings when the original file is CRLF (if (useCRLF)). For a file with LF endings, finalContent keeps whatever endings content carries. Example: the file is "a\nb\n" and the model sends content: "a\r\nb\r\n". The file on disk becomes CRLF, yet the approval diff, the "Written" diff and diffStat all report no change; before this PR those diffs showed it. edit.ts avoids this by normalizing new_string to LF before writing.
Normalizing fileContent to LF before the useCRLF branch would keep disk and diff in step. A test with an LF file and CRLF content should leave the file LF on disk with an empty diff.
There was a problem hiding this comment.
Good catch, @paolo-perrone — resolved in 758a4c9. Normalized fileContent to LF before the useCRLF branch (matching edit.ts) so disk bytes and diffs stay in step, and added a regression test verifying that writing CRLF content to an LF file keeps LF on disk with an empty approval/result diff and 0/0 diffStat.
TLDR
Fixes #30480.
Normalizes
\r\nto\nbefore computing the confirmation diff, execution result diff, andgetDiffStatinpackages/core/src/tools/write-file.ts(matchingedit.ts), while keeping raworiginalContentuntouched fordetectLineEndingso CRLF line endings are still preserved on disk.Dive Deeper
write_fileread the existing file raw (preserving\r\n) and diffed it against LF model content in bothshouldConfirmExecuteandexecute(Diff.createPatchandgetDiffStat), causing every unchanged line to appear removed and re-added and inflating+N/-Ntelemetry and rewind counts.\r\nto\non the inputs passed toDiff.createPatchandgetDiffStatinpackages/core/src/tools/write-file.ts. Disk write CRLF detection (detectLineEnding(originalContent)) andgetDiffContextSnippetcontinue to operate on the raw original and final contents.packages/core/src/tools/write-file.test.tsverifying that overwriting 1 line of a 5-line CRLF file produces a 1-line approval diff, a 1-line result diff,model_added_lines: 1/model_removed_lines: 1, and preserves\r\non disk. Confirmed failing onmainand passing with this fix.Reviewer Test Plan
packages/core/src/tools/write-file.test.ts: all tests passingprettier --check&eslint --max-warnings 0on modified files: passed