[Bug] write_file rewrites CRLF files with LF
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
- Have a file with CRLF line endings
- Read the file content
- Use write_file to write it back
string_replace
- Have a file with CRLF line endings
- 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
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] I can reproduce this issue consistently
- [x] This issue occurs with the latest version of nanocoder
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.
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?
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.tsxwith a CRLF fixture, plus a changeset. - Keep it separate from #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.
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.
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.
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.