nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings

Open addyCooks opened this issue 1 month ago • 2 comments

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

  1. Create a.txt containing one\r\ntwo\r\nthree\r\n.
  2. Call string_replace with old_str: "one\ntwo" and new_str: "ONE\nTWO".
  3. Create b.txt with the same content. Call string_replace with old_str: "two" and new_str: "TWO\nTWO-b".

Expected behaviour

  1. a.txt becomes ONE\r\nTWO\r\nthree\r\n.
  2. b.txt becomes one\r\nTWO\r\nTWO-b\r\nthree\r\n.

Actual behaviour

  1. The edit is refused with Content not found in file and "status":"no_candidate", and nothing is written.
  2. 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. Match old_str after converting it to that ending, instead of an exact split(old_str).
  • Line 106: convert new_str to the same ending before replaceFirstLiteral.
  • Mention line endings in the edit-recovery evidence (source/utils/edit-recovery.ts).
  • Add a spec with a CRLF fixture.

addyCooks avatar Nov 02 '26 16:11 addyCooks

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.

Mahdi-Hasan avatar Nov 05 '26 06:11 Mahdi-Hasan

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!

addyCooks avatar Nov 05 '26 07:11 addyCooks