Skip to content

[Bug] write_file rewrites CRLF files with LF #1964

Closed

Description

@jens-diewald

Description

Nanocoder file tools either fail on files with CRLF line endings or silently replace all line endings with LF.
This regularly sends my model into a long feedback loop, where it tries to figure out the problem and writing all sorts of scripts to fix it.

Below i give examples for write_file and string_replace. I suspect there are more tools affected by the problem, it may be a general problem with file handling?

Environment

  • OS: Windows 11
  • Node version: v24.14.1
  • Nanocoder version: 1.32.0
  • Provider: OpenAI-compatible API
  • Model: Qwen3.6-27B

Steps to Reproduce

write_file

  1. Have a file with CRLF line endings
  2. Read the file content
  3. Use write_file to write it back

string_replace

  1. Have a file with CRLF line endings
  2. Use string_replace with exact content of the file

Expected Behavior

write_file: Line endings are preserverd
string_replace: The tool succeeds and replaces the file content

Actual Behavior

write_file: Line endings are changed to LF
string_replace: The tool fails to match the file content and fails

Additional Context

  • I have searched existing issues to ensure this is not a duplicate
  • I can reproduce this issue consistently
  • This issue occurs with the latest version of nanocoder

Activity

  1. added
    bug
    on Nov 5, 2026
  2. added
    needs-triage
    area:tools
    possible-duplicate
    and removed
    needs-triage
    on Nov 5, 2026
  3. github-actions commented on Nov 5, 2026

    @github-actions
    Contributor

    Issue triage: possible duplicate of #1930

    @jens-diewald — this looks like it covers the same ground as #1930 (open): [Bug] stringreplace fails multi-line edits on CRLF files and writes mixed line endings.

    This has already been reported as issue #1930, which is still open and covers the same underlying defect: the file tools mishandle CRLF line endings, with string_replace failing to match CRLF file content and write_file rewriting the file with LF endings. Your reproduction steps and expected behaviour match that report, so continuing it there keeps the fix in one place.

    If this is wrong, reply here saying why and a maintainer will take it from there. Otherwise it will be closed in 14 days.


    Automated issue triage. It flags; it never closes on first sight. A maintainer can remove the label to dismiss this.

  4. jens-diewald commented on Nov 5, 2026

    @jens-diewald
    Author

    Huh, sorry about that. I looked into this problem in detail on Monday and the other issue was not there then. But at the time, I didn't get around to actually opening my issue.
    The new release, in which the problem still occurs, reminded me of it, and I just did a quick search, which apparently was too quick.

    However, the existing issue does only seem to cover string_replace. I do not think that fixes all the issues with CRLF files.
    Should i note that in the other issue, or do you want to keep this one open?

  5. removed
    possible-duplicate
    on Nov 5, 2026
  6. RealBhupesh commented on Nov 6, 2026

    @RealBhupesh
    Contributor

    I'd like to pick up the write_file half of this. #1961/#1950 only touch string_replace (I checked the changed files), so write_file still turns CRLF files into LF.

    Spec:

    • In source/tools/file-ops/write-file.tsx, when the target file already exists and mostly uses CRLF, and the incoming content has no \r\n, write the content with CRLF. New files and content that already has its own line endings stay as they are.
    • Add a spec in write-file.spec.tsx with a CRLF fixture, plus a changeset.
    • Keep it separate from fix(tools): match and keep CRLF line endings in string_replace #1961 so the two PRs don't overlap, and reuse that PR's line-ending helper if it lands first.

    Disclosure: the fix and tests will be written with an AI coding assistant, and I'll check them before opening the PR. Could a maintainer assign this to me? I'll wait for that before opening a PR.

  7. RealBhupesh commented on Nov 6, 2026

    @RealBhupesh
    Contributor

    Update: I'm not continuing with this one, so I'm releasing my earlier claim. Anyone is welcome to take the write_file part. The finding stands: write_file rewrites a CRLF file with LF (source/tools/file-ops/write-file.tsx, the writeFile(absPath, contentStr, 'utf-8') call has no line-ending handling), and the open string_replace PRs don't touch it. No PR from me exists.

  8. Mahdi-Hasan commented on Nov 6, 2026

    @Mahdi-Hasan
    Contributor

    Hi @addyCooks, I'd like to take the write_file part of this, since #1961 only covers string_replace. Plan: when the target file exists and mostly uses CRLF, and the new content has no \r\n, write it with CRLF. New files and content with its own line endings stay as they are. It would reuse the line-ending helper from #1961 and add a CRLF spec in write-file.spec.tsx. Could you assign it to me? I'll wait for that before opening a PR.

  9. changed the title [-][Bug] File Tools fail with Windows Line Endings (CRLF)[/-] [+][Bug] write_file rewrites CRLF files with LF[/+] on Nov 7, 2026
  10. addyCooks commented on Nov 7, 2026

    @addyCooks
    Collaborator

    Thanks @jens-diewald, confirmed on main. Both halves reproduce. string_replace is tracked in #1930 (fix in #1961), but write_file isn't, so I'm keeping this open for the write_file half and removing the duplicate flag.

    @Mahdi-Hasan, assigned to you. Please reuse the line-ending helper from #1961 and add a CRLF test.

  11. added a commit that references this issue on Nov 17, 2026
    1db3cbe
  12. added a commit that references this issue on Nov 17, 2026
    ccddaaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:toolsbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions