nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] diff_edit fails to match SEARCH blocks on CRLF files

Open Mahdi-Hasan opened this issue 3 weeks ago • 3 comments

Description

diff_edit refuses edits to files with CRLF line endings. Models send SEARCH blocks with LF line breaks, and countOccurrences / applyBlocks in source/tools/file-ops/diff-edit.tsx match block.search literally, so a multi-line SEARCH block never matches a CRLF file.

This is the third file editor with the CRLF problem from #1964. string_replace is fixed in #1961 (#1930) and write_file in #1992 (#1964). The nc-review bot on #1992 asked for a follow-up issue so diff_edit is not lost when #1964 closes.

Environment

  • OS: Windows 11
  • Node version: v24.16.0
  • Nanocoder version: 1.32.0 (main at 49ff6fe3)
  • Provider: n/a, reproduced by calling the tool directly
  • Model: n/a

Steps to Reproduce

  1. Create a file with CRLF line endings: one\r\ntwo\r\nthree\r\n.
  2. Run diff_edit on it with an LF SEARCH block:
    <<<<<<< SEARCH
    one
    two
    =======
    one
    TWO
    >>>>>>> REPLACE
    

Expected Behavior

The block matches and the file becomes one\r\nTWO\r\nthree\r\n, still CRLF.

Actual Behavior

Search block 1 was not found in file. The file may have changed since you last read it. The file is unchanged.

Logs/Screenshots

n/a

Additional Context

Possible fix: reuse fitEditToLineEndings(fileContent, block.search, block.replace) from source/utils/line-endings.ts (added in #1961) in validateBlocks, applyBlocks and the preview. I'd like to take this once #1961 lands, if a maintainer assigns it to me. I'll wait for that before opening a PR.

  • [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

Mahdi-Hasan avatar Nov 07 '26 07:11 Mahdi-Hasan

Hi, I'd like to take this one. I'm on Windows myself and hit CRLF issues regularly. Plan: apply the same line-ending normalization used in the string_replace (#1961) and write_file (#1992) fixes to diff_edit's SEARCH matching, preserving the file's original CRLF endings when writing.

Edzard9 avatar Nov 07 '26 08:11 Edzard9

Thanks @Edzard9. I filed this as a follow-up to #1964 and asked in the description to take it once #1961 landed. #1961 is now merged, so I'm ready to start: fit each SEARCH/REPLACE block with fitEditToLineEndings in validateBlocks, applyBlocks and the preview, plus a CRLF spec. @addyCooks, your call on who takes it. I'll wait for assignment before opening a PR.

Mahdi-Hasan avatar Nov 07 '26 12:11 Mahdi-Hasan

Thanks all for the interest! @Mahdi-Hasan filed this and asked first (and wrote the sibling fixes in #1961/#1992), so I'm assigning it to them.

@Edzard9 @SatriaBagusNurhadianto, thanks for the PRs (#1995, #1994). Since the issue was already requested, we'll go with Mahdi's, so please close yours. Going forward, please comment on an issue and wait for assignment before opening a PR. Feel free to pick up another open issue.

addyCooks avatar Nov 10 '26 06:11 addyCooks