[Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings
Summary
string_replace compares old_str with the raw file text. Models send LF, so a multi-line edit on a CRLF file is not found. A single-line edit that adds lines writes LF into a CRLF file.
Steps to reproduce
- Create
a.txtcontainingone\r\ntwo\r\nthree\r\n. - Call
string_replacewithold_str: "one\ntwo"andnew_str: "ONE\nTWO". - Create
b.txtwith the same content. Callstring_replacewithold_str: "two"andnew_str: "TWO\nTWO-b".
Expected behaviour
a.txtbecomesONE\r\nTWO\r\nthree\r\n.b.txtbecomesone\r\nTWO\r\nTWO-b\r\nthree\r\n.
Actual behaviour
- The edit is refused with
Content not found in fileand"status":"no_candidate", and nothing is written. - The file is written with mixed endings:
one␍␊
- TWO␊ <- actual (LF only)
+ TWO␍␊ <- expected
TWO-b␍␊
read_file returns the CRLF text unchanged, so the model sees \r it cannot easily reproduce. Any Windows checkout with core.autocrlf=true has CRLF files.
Environment
Windows 11, Node v22.22.3, Nanocoder 1.31.0 (main at 948a4e74)
Suggested fix
source/tools/file-ops/string-replace.tsx:89: detect the file's dominant line ending. Matchold_strafter converting it to that ending, instead of an exactsplit(old_str).- Line 106: convert
new_strto the same ending beforereplaceFirstLiteral. - Mention line endings in the edit-recovery evidence (
source/utils/edit-recovery.ts). - Add a spec with a CRLF fixture.
Hi @addyCooks, you offered me this one on #1722, so I'm picking it up. The fix is in #1961: old_str and new_str are converted to the file's line ending when most of its lines use CRLF. Could you assign it to me? Thanks.
Hey @Mahdi-Hasan , assigning this to you, and I’ll review it this time. For future issues, please comment first and wait to be assigned before opening a PR. Thanks!