Skip to content

fix(tools): keep CRLF line endings when write_file overwrites a CRLF file - #1992

Merged
akramcodez merged 3 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1964-write-file-crlf
Nov 17, 2026
Merged

akramcodez merged 3 commits into
Nano-Collective:mainfrom
Mahdi-Hasan:fix/1964-write-file-crlf

Conversation

@Mahdi-Hasan

@Mahdi-Hasan Mahdi-Hasan commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

write_file wrote the model's content as sent. Models send LF, so rewriting a CRLF file turned every line into LF. Fixes #1964 (the write_file half; string_replace is #1930).

It reuses source/utils/line-endings.ts from #1961 (now merged), as asked on the issue.

  • New fitContentToLineEndings(existing, content) in source/utils/line-endings.ts.
  • When the existing file is mostly CRLF, every line break in the new content is written as CRLF, including content that mixes both.
  • New files and LF files get the content as sent.
  • The mutation firewall checks the content that is actually written.
  • The terminal diff, the VS Code diff and the ACP diff (Zed and other editors) use the same content. Before, an LF rewrite of a CRLF file showed every line as changed.
  • computeLineDiff now splits on CRLF too. Each CRLF line kept its CR, which Ink rendered as a blank line after every unchanged line.

diff_edit has the same CRLF bug. It is tracked in #1993, to keep this PR to #1964.

Preview

Terminal preview when the model rewrites one line of a CRLF file with LF content:

Before (every line shows as changed):

⚒ write_file
Path: config.ts
Size: 5 lines, 81 characters (~21 tokens)

Diff: +4 -4
   1 -export const a = 1;
   2 -export const b = 2;
   3 -export const c = 3;
   4 -export const d = 4;
   1 +export const a = 1;
   2 +export const b = 20;
   3 +export const c = 3;
   4 +export const d = 4;

After (only the changed line, and the file stays CRLF on disk):

⚒ write_file
Path: config.ts
Size: 5 lines, 85 characters (~22 tokens)

Diff: +1 -1
   1 export const a = 1;
   2 -export const b = 2;
   2 +export const b = 20;
   3 export const c = 3;
   4 export const d = 4;

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Changeset

  • Added a changeset (pnpm changeset) describing this change for the changelog

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files

  • All existing tests pass (pnpm test:all completes successfully)

  • Tests cover both success and error scenarios

  • 5 new tests in write-file.spec.tsx: the repro from the issue, mixed content on a CRLF file, CRLF content on an LF file, a new file, and the preview and result diffs on a CRLF file. The repro and the diff test fail without the change.

  • 1 new test in acp-tool-call.spec.ts: the ACP diff for an LF rewrite of a CRLF file. Fails without the change.

  • 4 new tests in line-endings.spec.ts for the helper.

  • 1 new test in inline-diff.spec.ts: CRLF lines lose their CR. Fails without the change.

  • inline-diff + line-endings + write-file + acp-tool-call + string-replace specs: 188 pass. Types, Biome and knip pass.

  • The one failure, write_file validator: rejects Windows system directories, also fails without this change on Windows. CI on Linux should settle it.

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

Checklist

  • If this was for an open issue, I was assigned to it
  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

@github-actions github-actions Bot added the area:tools Tool implementations and tool-calling label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

nc-review: comments — 2 important, 1 nit

@Mahdi-Hasan — a few things worth a look, none blocking.

The PR adds a source/utils/line-endings.ts helper module and uses it to keep CRLF files CRLF across write_file (and, bundled in from #1961, string_replace), applying the same fit to the bytes written, the mutation firewall, the terminal preview/result diff, the VS Code preview diff and the ACP diff. The core logic is correct and I traced the fit logic, the mixed-file escape hatch and the keptAsSent reconstruction in the string_replace result preview against the surrounding code and they hold up; the new tests are real regression tests that fail without the change. It is not ready as-is because it is stacked on the still-open #1961 and carries that PR's commits and changeset verbatim, and because the third file editor (diff_edit) still fails on the same CRLF files the issue asks about.

🟠 important · duplicate · source/utils/line-endings.ts

This PR is stacked on the still-open #1961 ("fix(tools): match and keep CRLF line endings in string_replace", same author) and, as the description says, "this diff also shows its commits". The result is that two open PRs carry byte-identical source/utils/line-endings.ts, source/utils/line-endings.spec.ts, source/tools/file-ops/string-replace.tsx, source/tools/file-ops/string-replace-preview.tsx and source/tools/file-ops/string-replace.spec.tsx changes, plus two .changeset/string-replace-crlf.md files with the same name and the same "Closes #1930" body. Whichever lands second will conflict on source/utils/line-endings.ts and on the changeset filename, and the changelog would get two copies of the same entry (or #1930 gets closed twice).

This is a stacking-hygiene problem rather than a rejected idea, so it should not block the write_file work. The fix is mechanical: land #1961 first, rebase #1992 on top of it, and drop the duplicated commits and the duplicated .changeset/string-replace-crlf.md from this branch so it carries only the write_file change and its own changeset. Also worth deciding which PR closes #1930 so the issue is not closed twice.

🟠 important · completeness · source/tools/file-ops/diff-edit.tsx

diff_edit — the third file editor, and the one CLAUDE.md names alongside string_replace and write_file — still has the exact bug this issue describes. countOccurrences does content.split(search) and applyBlocks replaces block.search literally (source/tools/file-ops/diff-edit.tsx:134-186), so an LF SEARCH block against a CRLF file matches zero times and the edit is refused with "Search block N could not be applied cleanly". Unlike string_replace, diff_edit has no edit-recovery payload either, so a model using it on Windows gets no way back in.

#1964 only enumerates write_file and string_replace under "Steps to Reproduce", so this is not strictly required to close the issue — but the issue explicitly asks "I suspect there are more tools affected by the problem, it may be a general problem with file handling?", and merging this PR closes #1964, after which nothing tracks the diff_edit case. The new source/utils/line-endings.ts helper is exactly the tool for the job: fitEditToLineEndings(fileContent, block.search, block.replace) in executeDiffEdit (and the matching fit in validateBlocks and the preview) would bring diff_edit in line with the other two. If you would rather not widen this PR, please open a follow-up issue for diff_edit before #1964 closes, so the remaining case is not silently lost.

⚪ nit · correctness · source/utils/line-endings.ts

fitContentToLineEndings bails out on content.includes('\r\n'), i.e. a single CRLF anywhere in the new content means the whole content is written unconverted. For a file that was 100% CRLF, a model that echoes the file back but normalises (or adds) one CRLF line can therefore land a mostly-LF file — the very outcome this PR exists to prevent. The behaviour is documented in the changeset, so it is a defensible heuristic rather than a bug, and the 'content already carries CRLF, leave it alone' rule is the safer default. It is worth either converting only the LF breaks (content.replace(/(?<!\r)\n/g, '\r\n')) or noting in the changeset that one stray CRLF disables the whole conversion, so users are not surprised when a CRLF file still flips.


🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional

Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with /re-review.

@github-actions github-actions Bot added the agent:comments nc-review left non-blocking findings label Nov 7, 2026
@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three addressed:

  1. Stacking on fix(tools): match and keep CRLF line endings in string_replace #1961: fix(tools): match and keep CRLF line endings in string_replace #1961 should land first. Once it does, I'll merge main into this branch, so only the write_file commits and .changeset/write-file-crlf.md remain, and only fix(tools): match and keep CRLF line endings in string_replace #1961 closes [Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings #1930.
  2. diff_edit: reproduced (an LF SEARCH block on a CRLF file fails with "Search block 1 was not found") and filed as [Bug] diff_edit fails to match SEARCH blocks on CRLF files #1993, so it stays tracked after [Bug] write_file rewrites CRLF files with LF #1964 closes. I'll wait for assignment there.
  3. One CRLF disabling the conversion: fixed in 0fe7f0f. When the existing file is mostly CRLF, every line break is now written as CRLF, the same way fitEditToLineEndings handles string_replace. Added tests for mixed content on a CRLF file and CRLF content on an LF file, and updated the changeset.

@Mahdi-Hasan

Copy link
Copy Markdown
Contributor Author

#1961 is merged, so I merged main in (5e03e53). The diff is now only the write_file change: 9 files, with .changeset/write-file-crlf.md as its only changeset. With colour forced on, the inline-diff, line-endings, write-file, acp-tool-call and string-replace specs pass (188). Types, Biome and knip pass.

…file

Models send LF, so rewriting a CRLF file with write_file turned every
line into LF. When the existing file is mostly CRLF and the new content
has no CRLF of its own, write it with CRLF. New files, LF files and
content with its own CRLF are written as sent.

The terminal diff, the VS Code diff and the ACP diff use the same
content, so the diff shows changed lines instead of every line.

Closes Nano-Collective#1964
computeLineDiff split on LF only, so each line of a CRLF file kept its
CR, and Ink rendered it as a blank line after every unchanged line.
One CRLF anywhere in the new content turned off the conversion, so a
model that echoed back one CRLF line could still flip a CRLF file to
mostly LF. Convert all line breaks whenever the existing file is mostly
CRLF, as fitEditToLineEndings does for string_replace.
@Mahdi-Hasan
Mahdi-Hasan force-pushed the fix/1964-write-file-crlf branch from 5e03e53 to 2f82ff5 Compare November 17, 2026 05:58
@akramcodez
akramcodez merged commit ccddaaf into Nano-Collective:main Nov 17, 2026
18 checks passed
@akramcodez

Copy link
Copy Markdown
Member

Thanks @Mahdi-Hasan, nice work keeping CRLF files intact and splitting out the diff_edit follow-up. Looks good to merge.

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

Labels

agent:comments nc-review left non-blocking findings area:tools Tool implementations and tool-calling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] write_file rewrites CRLF files with LF

2 participants