Skip to content

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

Closed

Description

@addyCooks

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 948a4e7)

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.

Activity

  1. added
    bug
    on Nov 2, 2026
  2. added theissue type on Nov 2, 2026
  3. added
    area:tools
    on Nov 2, 2026
  4. Mahdi-Hasan commented on Nov 5, 2026

    @Mahdi-Hasan
    Contributor

    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.

  5. addyCooks commented on Nov 5, 2026

    @addyCooks
    CollaboratorAuthor

    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!

  6. added a commit that references this issue on Nov 7, 2026
    f9d89b1
  7. added a commit that references this issue on Nov 12, 2026
    111485d
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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions