Skip to content

Conversation

@Thomas-Shephard
Copy link

@Thomas-Shephard Thomas-Shephard commented Jan 7, 2026

Summary

This PR ensures that the WriteFile and Edit tools respect existing file line endings (CRLF vs LF) to prevent unnecessary Git diff noise, particularly on Windows. It also fixes a bug where trailing newlines were unintentionally stripped during edit correction and defaults to the operating system's native line ending (os.EOL) when creating new files.

Details

As above

Related Issues

Relates to #3543
Closes #16148
Closes #16129

How to Validate

  1. Run the new regression tests:

    npx vitest packages/core/src/tools/line-endings.test.ts

    Verify that all tests pass, confirming CRLF preservation and OS EOL usage.

  2. Run the updated edit corrector tests:

    npx vitest packages/core/src/utils/editCorrector.test.ts

    Verify that the new test case for trailing newlines passes.

  3. Manual Verification (on Windows):

    • Create a file with CRLF endings manually.
    • Use the CLI to edit it.
    • Verify the file still has CRLF endings.
    • Ask the CLI to create a new file.
    • Verify the new file uses \r\n.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@google-cla
Copy link

google-cla bot commented Jan 7, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Thomas-Shephard, 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 significantly improves file handling within the CLI tools by introducing robust line ending preservation. It ensures that file modifications respect the original line ending style (CRLF or LF) to prevent disruptive Git diffs, particularly for Windows users. Additionally, new files will now be created with the operating system's native line endings, and a previous bug that incorrectly stripped trailing newlines during content correction has been fixed, leading to more predictable and consistent file output.

Highlights

  • Line Ending Preservation: The WriteFile and Edit tools now preserve existing file line endings (CRLF or LF) when modifying files, which helps prevent unnecessary Git diff noise, especially on Windows.
  • OS Native Line Endings for New Files: When creating new files, the tools will now default to using the operating system's native line ending (os.EOL).
  • Trailing Newline Bug Fix: A bug that caused trailing newlines to be unintentionally stripped during edit correction has been resolved.
  • New Line Ending Tests: Comprehensive regression tests have been added to validate line ending preservation and OS EOL usage across different scenarios.
  • Refactored Line Ending Detection: The detectLineEnding utility function has been moved to textUtils.ts for centralized and broader use.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link
Contributor

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

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 functionality to preserve existing line endings (CRLF/LF) in files modified by the WriteFile and Edit tools, which is a great improvement for cross-platform compatibility, especially on Windows. It also correctly defaults to the operating system's native line ending for new files and fixes a bug with trailing newlines during edit correction. The changes are well-structured, including moving the detectLineEnding function to a shared utility file and adding comprehensive regression tests. My review includes one suggestion to refactor a small piece of logic to improve maintainability by reducing code duplication.

Comment on lines 289 to 297
let finalContent = fileContent;
if (!isNewFile && originalContent) {
const lineEnding = detectLineEnding(originalContent);
if (lineEnding === '\r\n') {
finalContent = finalContent.replace(/\r?\n/g, '\r\n');
}
} else {
if (os.EOL === '\r\n') {
finalContent = finalContent.replace(/\r?\n/g, '\r\n');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This logic for handling line endings is correct, but it contains duplicated code which can make it harder to maintain. You can simplify this block by combining the conditions for converting to CRLF, which improves readability and maintainability.

      let finalContent = fileContent;
      const preserveCRLF = !isNewFile && originalContent && detectLineEnding(originalContent) === '\r\n';
      // The `else` branch in the original logic covers new files or existing empty files.
      const useOSEOLForNewFile = (isNewFile || !originalContent) && os.EOL === '\r\n';

      if (preserveCRLF || useOSEOLForNewFile) {
        finalContent = finalContent.replace(/\r?\n/g, '\r\n');
      }

Copy link
Author

@Thomas-Shephard Thomas-Shephard Jan 7, 2026

Choose a reason for hiding this comment

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

Fixed :)

@Thomas-Shephard Thomas-Shephard force-pushed the feature/eol-preservation branch from da1ce7e to 3414bb7 Compare January 7, 2026 20:08
@Thomas-Shephard Thomas-Shephard marked this pull request as ready for review January 7, 2026 20:11
@Thomas-Shephard Thomas-Shephard requested a review from a team as a code owner January 7, 2026 20:11
Copy link
Contributor

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

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 is a great improvement for cross-platform compatibility by preserving file line endings (CRLF/LF). The changes in EditTool and WriteFileTool to respect existing EOLs and use the OS default for new files are well-implemented, and the addition of regression tests in line-endings.test.ts is excellent. I've found one issue in the new trimPreservingTrailingNewline helper function in editCorrector.ts which could lead to incorrect trimming of strings with trailing newlines followed by other whitespace. My review includes a suggestion to fix this.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@jacob314 jacob314 added the status/need-issue Pull requests that need to have an associated issue. label Jan 7, 2026
@Thomas-Shephard
Copy link
Author

Hi @jacob314,

I've now created an issue for this PR :)

@TheNexusGene
Copy link

TheNexusGene commented Jan 8, 2026 via email

@gemini-cli gemini-cli bot added area/core Issues related to User Interface, OS Support, Core Functionality and removed status/need-issue Pull requests that need to have an associated issue. labels Jan 8, 2026

const writtenContent = fs.readFileSync(filePath, 'utf8');
// Expect 'line2' to be replaced by 'modified', and all endings to be CRLF
// Logic:
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this comment block

Copy link
Author

Choose a reason for hiding this comment

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

Removed

@jacob314
Copy link
Collaborator

Authored by /review-frontend, reviewed by Jacob Richman

Overall: Great improvement for platform consistency and fixing the trailing newline bug! The use of os.EOL for new files is a solid addition.

Consolidation Note: Please note that EditTool and SmartEditTool were recently consolidated into packages/core/src/tools/smart-edit.ts (commit f3625aa). You'll need to move your changes from edit.ts to smart-edit.ts.

Deduplication: Now that detectLineEnding is in textUtils.ts, could you also update smart-edit.ts to use it and remove its local definition? Also, consider applying your improved /\r?\n/g regex there as well.

Edit Corrector: The trimPreservingTrailingNewline fix looks correct. Tagging @anj-s to verify if this has any subtle impacts on LLM edit quality.

Tests: In line-endings.test.ts, please use vi.restoreAllMocks() in afterEach for better test isolation.

@Thomas-Shephard
Copy link
Author

Authored by /review-frontend, reviewed by Jacob Richman

Overall: Great improvement for platform consistency and fixing the trailing newline bug! The use of os.EOL for new files is a solid addition.

Consolidation Note: Please note that EditTool and SmartEditTool were recently consolidated into packages/core/src/tools/smart-edit.ts (commit f3625aa). You'll need to move your changes from edit.ts to smart-edit.ts.

Deduplication: Now that detectLineEnding is in textUtils.ts, could you also update smart-edit.ts to use it and remove its local definition? Also, consider applying your improved /\r?\n/g regex there as well.

Edit Corrector: The trimPreservingTrailingNewline fix looks correct. Tagging @anj-s to verify if this has any subtle impacts on LLM edit quality.

Tests: In line-endings.test.ts, please use vi.restoreAllMocks() in afterEach for better test isolation.

Thanks for the review! I checked the consolidation status, and it seems smart-edit.ts was renamed back to edit.ts recently, so I kept the changes there. I also verified that detectLineEnding is already being imported from textUtils in edit.ts and the local definition is removed, so that deduplication is taken care of. The improved regex is also already in place there. On the test side, I updated line-endings.test.ts to use vi.restoreAllMocks() in the afterEach block as requested, and I had to make a small tweak to write-file.test.ts to stop a restoreAllMocks flag from breaking the mock helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve EOLs newline writing failing on mixed unix/windows newline file

3 participants