Repository navigation
fix(tools): match and keep CRLF line endings in string_replace - #1961
Conversation
string_replace compared old_str with the raw file text. Models send LF, so a multi-line edit to a CRLF file was not found, and new lines were written as LF into a CRLF file. Add fitEditToLineEndings: when most lines of the file end in CRLF, convert old_str and new_str to CRLF. Keep the edit as sent when only the LF search text matches (an LF region of a mixed file). Use it in the tool, its validator, the VS Code diff and the preview. Closes Nano-Collective#1930 Co-Authored-By: Claude Opus 5.5 <[email protected]>
nc-review: needs work — 1 blocking, 2 important, 1 nit@Mahdi-Hasan — there is a blocking item below. The core CRLF matching and writing logic in
🔴 blocking · This PR duplicates the still-open PR #1950: identical title ( The PR description notes that the issue was assigned to you after 🟠 important · The ACP diff path still matches and replaces on the raw const occurrences = current.split(oldStr).length - 1;
if (occurrences !== 1) {
return {type: 'diff', path: absPath, oldText: oldStr, newText: newStr};
}For a CRLF file the model sends Fix by running 🟠 important · The preview re-derives the line-ending fit from the file content it just read, but in the
With Please add a preview spec for the mixed-ending case so the reported location is asserted, not just the written bytes. ⚪ nit · The new This is outside the scope of issue #1930, so it is not a blocker — but since the utility is already generic, wiring it into 🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with |
The ACP diff still matched the raw LF old_str, so for a CRLF file it showed a hunk that did not match what the tool wrote. Run the edit through fitEditToLineEndings there too. The result preview re-fitted against the file after the write. In a mixed-ending file an edit kept as sent was then not found, and the preview reported line 1. Use new_str as sent when that is what the file holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Addressed the nc-review findings in 6d9c7ab:
The duplicate flag (#1950) is for a maintainer to decide. I've claimed #1930 on the issue. |
addyCooks
left a comment
There was a problem hiding this comment.
Thanks @Mahdi-Hasan, the CRLF fitting looks right, and I checked it locally: with main merged in, the type check and Biome pass, the line-ending, string_replace and ACP specs pass (95), and the new tests fail without the change. The ACP diff and result-preview fixes from the review bot are in.
Two small things before merge:
- The branch conflicts with main in
source/tools/file-ops/string-replace.spec.tsx(both sides added tests at the end). Please merge main and keep both sets. - Please drop the "Generated with Claude Code" line from the PR description.
I'll approve the CI run once the branch is updated. Happy to re-review then.
…lace-crlf # Conflicts: # source/tools/file-ops/string-replace.spec.tsx
|
@addyCooks Thanks! Both done:
With main merged: type check and Biome pass, and the line-ending, string_replace and ACP tool-call specs pass (95). |
The mixed-ending result test looked for the plain text 'Location: lines 5-6'. CI renders colour codes between 'Location: ' and the line numbers, so the substring was not found. Strip the styling first, as the read-file spec does.
|
Thanks @addyCooks for fixing that test in 42d04ce, and sorry for the miss. Colour was off in my local Windows runs, so the styled |
addyCooks
left a comment
There was a problem hiding this comment.
Thanks, both points from my review are done (branch merged, description cleaned) and the CRLF fitting looks right. The new tests fail without the change. A follow-up issue for the same CRLF miss in diff_edit would be welcome.
Approving 🎉
…Collective#1961) * fix(tools): match and keep CRLF line endings in string_replace string_replace compared old_str with the raw file text. Models send LF, so a multi-line edit to a CRLF file was not found, and new lines were written as LF into a CRLF file. Add fitEditToLineEndings: when most lines of the file end in CRLF, convert old_str and new_str to CRLF. Keep the edit as sent when only the LF search text matches (an LF region of a mixed file). Use it in the tool, its validator, the VS Code diff and the preview. Closes Nano-Collective#1930 Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(tools): fit CRLF edits in the ACP diff and the result preview The ACP diff still matched the raw LF old_str, so for a CRLF file it showed a hunk that did not match what the tool wrote. Run the edit through fitEditToLineEndings there too. The result preview re-fitted against the file after the write. In a mixed-ending file an edit kept as sent was then not found, and the preview reported line 1. Use new_str as sent when that is what the file holds. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(string-replace): strip ANSI before matching the result location The mixed-ending result test looked for the plain text 'Location: lines 5-6'. CI renders colour codes between 'Location: ' and the line numbers, so the substring was not found. Strip the styling first, as the read-file spec does. --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: addyCooks <[email protected]>
Description
string_replacecomparedold_strwith the raw file text. Models send LF, so a multi-line edit to a CRLF file was refused withContent not found in file, and an edit that added lines wrote LF lines into the file. Fixes #1930.source/utils/line-endings.tswithfitEditToLineEndings(fileContent, oldStr, newStr).old_strandnew_strare converted to CRLF before matching and writing.new_stras sent when that is what the file holds, so mixed-ending files report the right lines.diff_edithas the same CRLF miss. It is left for a follow-up, to keep this PR to [Bug] string_replace fails multi-line edits on CRLF files and writes mixed line endings #1930.Type of Change
Changeset
pnpm changeset) describing this change for the changelogTesting
Automated Tests
New features include passing tests in
.spec.ts/tsxfilesAll existing tests pass (
pnpm test:allcompletes successfully)Tests cover both success and error scenarios
5 new tests in
string-replace.spec.tsx: both repros from the issue, the validator, the preview, and the result preview in a mixed-ending file. All fail onmain.1 new test in
acp-tool-call.spec.ts: the ACP diff for an LF edit to a CRLF file. Fails onmain.6 new tests in
source/utils/line-endings.spec.ts, including the mixed-file case.string-replace.spec.tsx+acp-tool-call.spec.ts+line-endings.spec.ts: 88 pass. Types, biome and knip pass.Developed on Windows, where
pnpm test:avahas unrelated failures onmain(POSIX paths). CI on Linux should settle it.Manual Testing
Checklist