CORE JSC

International Technology Partnership

Roo Tools

Fixing Roo Code's Diff Edits That Repeatedly Fail on Files With Mixed CRLF and LF Line Endings

A diff edit that should match a block of code character-for-character fails with "SEARCH block not found" even though the exact same text is clearly visible, unchanged, in the file open in the editor. The file looks completely normal on screen because CRLF and LF line endings render identically — but one of them has to be present in the search text and the actual file content for a diff match to succeed, and a mixed-ending file quietly breaks that assumption.

Core JSC Team·November 23, 2026
Roo CodeLine EndingsGitDiff MatchingDeveloper Tools

The Problem

Roo Code attempts a diff-based edit on a file, providing a search block that's visibly, exactly the same text as what's shown in the editor for that section of the file. The edit fails with an error like SEARCH block not found in the file content, even on a second or third attempt after carefully re-reading the file first. Opening the file and comparing the search text to what's on screen character by character shows no visible difference at all — the content genuinely matches as far as anything displayed in the editor shows.

Why It Happens

CRLF and LF line endings are visually indistinguishable in virtually every text editor, but they're different bytes in the actual file

Windows-style line endings use two characters, carriage return plus line feed (\r\n), while Unix-style line endings use just the line feed (\n) alone. A text editor renders both as an ordinary line break with no visual indicator of which one is actually present — the difference is entirely invisible on screen, even though it's a real, different sequence of bytes in the file on disk.

A diff match requires exact byte-for-byte agreement, and a search block built with one line-ending style can't match file content using the other

Diff-based editing works by finding an exact substring match between the provided search text and the file's actual content — not a visual or semantic comparison, but a literal byte comparison. If the search block was constructed (by typing, by copying from a different source, or by the file having been read with line endings normalized one way) using \n, and the actual file on disk uses \r\n for some or all of its lines, the strings differ at the byte level even though they display identically, and the match correctly fails according to its own logic.

A single file commonly ends up with mixed line endings from edits made across different tools, operating systems, or git configurations over its history

A file edited at different points by a Windows-native editor, a Unix-based tool, and a git configuration with autocrlf settings that changed over the project's lifetime can end up with some lines using \r\n and others using \n within the very same file — this isn't a hypothetical edge case so much as an extremely common real-world state for a file that's passed through several contributors' different environments over time.

The Fix

1. Check the file's actual line-ending composition directly, rather than relying on what the editor displays

file myfile.js
# Reports "with CRLF line terminators" or similar if present

grep -c $'\r' myfile.js
# Counts how many lines actually contain a carriage return byte

These commands reveal the file's actual byte-level line-ending composition directly, which no amount of visual inspection in an editor can distinguish — confirming whether a file has consistent or genuinely mixed endings is the necessary first step before trying to work around the mismatch.

2. Normalize the entire file to one consistent line-ending style before attempting further diff edits

# Convert to LF consistently (most common target for a JS/TS codebase)
dos2unix myfile.js
# Or, without the dos2unix utility:
sed -i 's/\r$//' myfile.js

Normalizing the whole file to a single, consistent line-ending style removes the mismatch at its source — once every line uses the same ending, a search block constructed from the editor's displayed content (which always uses the normalized form once the file itself is normalized) reliably matches again.

3. Configure git's line-ending handling explicitly for the project, rather than leaving it to each contributor's local defaults

# .gitattributes
* text=auto eol=lf
*.bat text eol=crlf

A .gitattributes file that explicitly declares the intended line-ending policy per file type — rather than relying on each contributor's own core.autocrlf setting, which varies by platform and personal configuration — prevents the mixed-ending state from being reintroduced by a future commit from a contributor with different local settings.

4. For a one-off edit on a file that can't be normalized immediately, use a tool-level file write instead of a diff-based edit

Rather than retrying a diff-based SEARCH/REPLACE edit against a
known mixed-ending file, write the file's full new content directly
(a whole-file write tool rather than a diff match), sidestepping
the byte-level line-ending comparison entirely for this one edit.

When normalizing the whole file isn't immediately appropriate — a shared file mid-review, or one where line-ending changes would otherwise show up as noisy, unrelated diff lines in version control — writing the complete new file content directly bypasses the exact-match requirement that mixed endings specifically break, without touching every other line's ending in the process.

Why This Works

A diff match failing on text that looks identical on screen is never a sign the diff tool is confused or unreliable — it's doing exactly what an exact byte comparison is supposed to do, correctly reporting that two strings differing only in an invisible line-ending byte sequence are, in fact, not identical. Checking the file's actual line-ending composition directly, normalizing it to one consistent style, configuring git to prevent the mix from recurring, and falling back to a whole-file write for an edit that can't wait for normalization each address the same root byte-level mismatch rather than treating it as a flaky or unexplained diff failure.

Conclusion

A diff-based edit failing on text that's visibly identical in the editor is a byte-level line-ending mismatch between the search block and the file's actual content, not an unreliable diff-matching mechanism. Checking the file's real line-ending composition, normalizing it to a single consistent style, locking that policy in with .gitattributes, and using a whole-file write as an immediate workaround together turn an invisible, repeatedly-failing mismatch into a resolved and prevented one.