Skip to content

fix(core): normalize CRLF line endings when computing write_file diffs and diffStat - #30489

Open
bitwizard25 wants to merge 2 commits into
google-gemini:mainfrom
bitwizard25:fix/write-file-crlf-diff
Open

bitwizard25 wants to merge 2 commits into
google-gemini:mainfrom
bitwizard25:fix/write-file-crlf-diff

Conversation

@bitwizard25

Copy link
Copy Markdown

TLDR

Fixes #30480.

Normalizes \r\n to \n before computing the confirmation diff, execution result diff, and getDiffStat in packages/core/src/tools/write-file.ts (matching edit.ts), while keeping raw originalContent untouched for detectLineEnding so CRLF line endings are still preserved on disk.

Dive Deeper

  • Root cause: write_file read the existing file raw (preserving \r\n) and diffed it against LF model content in both shouldConfirmExecute and execute (Diff.createPatch and getDiffStat), causing every unchanged line to appear removed and re-added and inflating +N/-N telemetry and rewind counts.
  • Fix: Normalize \r\n to \n on the inputs passed to Diff.createPatch and getDiffStat in packages/core/src/tools/write-file.ts. Disk write CRLF detection (detectLineEnding(originalContent)) and getDiffContextSnippet continue to operate on the raw original and final contents.
  • Tests: Added a regression test in packages/core/src/tools/write-file.test.ts verifying 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\n on disk. Confirmed failing on main and passing with this fix.

Reviewer Test Plan

npx vitest run packages/core/src/tools/write-file.test.ts --coverage.enabled=false
  • packages/core/src/tools/write-file.test.ts: all tests passing
  • prettier --check & eslint --max-warnings 0 on modified files: passed

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • CRLF Normalization: Implemented CRLF to LF normalization before computing diffs and diffStats in the write_file tool to prevent inflated telemetry and incorrect diff visualization.
  • Preservation of Disk State: Ensured that the normalization only affects the diff calculation, while the original file content remains untouched to preserve existing line endings on disk.
  • Regression Testing: Added a new test case in write-file.test.ts to verify that single-line changes in CRLF files result in accurate diffs and stats while maintaining file integrity.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/m A medium sized PR label Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 109
  • Additions: +99
  • Deletions: -10
  • Files changed: 2

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/core/src/tools/write-file.ts Outdated
? '' // Or some indicator of unreadable content
: originalContent;
: originalContent.replace(/\r\n/g, '\n');
const normalizedFileContent = fileContent.replace(/\r\n/g, '\n');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch is waiting to be deployed

1 waiting deployment
eval-gate — 758a4c96 Waiting Nov 27, 2026 by bitwizard25 via Evaluate Steering & Regressions #2390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

write_file diff shows every line changed when the file uses CRLF line endings

2 participants