One stray CRLF makes edit and multiedit rewrite every line ending in the file
Crush Contributor License Agreement
Thank you for your interest in contributing to Crush (the "Project"), maintained by Charm (https://charm.land). This Contributor License Agreement ("Agreement") sets out the terms under which you submit Contributions to the Project.
- This PR follows the guidelines in
CONTRIBUTING.mdregarding the scope of PRs. - I have added an entry to
CHANGELOG.mdconcisely describing the change. - I have accepted (or will accept, when prompted by the CLA assistant bot) the Contributor License Agreement.
Description
A single \r\n anywhere in a file makes edit and multiedit rewrite every line ending in it.
loadExistingFile normalises the whole file to LF and remembers one boolean for the entire file:
// internal/fsext/fileutil.go:243
func ToUnixLineEndings(content string) (string, bool) {
if strings.Contains(content, "\r\n") {
return strings.ReplaceAll(content, "\r\n", "\n"), true
}
return content, false
}
// internal/agent/tools/edit.go:310
oldContent, isCrlf = fsext.ToUnixLineEndings(string(content))and the write path converts the whole result back:
// internal/agent/tools/edit.go:437
writeContent := result
if isCrlf {
writeContent, _ = fsext.ToWindowsLineEndings(writeContent)
}
if err := commitFileChange(edit, sessionID, filePath, oldContent, writeContent); err != nil {multiedit.go:320 has the same two lines. For a file with mixed endings, every line that the edit did not touch changes on disk, while the response says only Content replaced in file: <path>.
a.txt containing "one\ntwo\nthree\r\nfour\n":
edit {old_string: "two", new_string: "TWO"}
disk: "one\r\nTWO\r\nthree\r\nfour\r\n"Three untouched lines were rewritten, and git diff shows three of four.
Why I think this is unintended
write.go:137writesparams.Contentbyte-for-byte and never touches endings. Two write tools, two conventions.viewstrips the\rfrom every line (view.go:336,view.go:291), so the model cannot tell that the file is mixed — it has no way to know an edit will rewrite the rest of the file.The two halves of the round trip disagree with each other.
ToWindowsLineEndingsguards on the same predicate:// internal/fsext/fileutil.go:251 func ToWindowsLineEndings(content string) (string, bool) { if !strings.Contains(content, "\r\n") { ... } }There "contains one CRLF" means "already Windows, leave it alone", while the caller reads it as "convert everything".
editalready has a mechanism for telling the model it changed something it was not asked to change —whitespaceCorrectedNoteinedit_whitespace.go, wired atedit.go:447, andedit.mdpromises the response says when that happens. Nothing covers line endings.
edit_test.go:32 (TestReplaceContentPreservesCRLFAndMetadata) pins the uniform-CRLF case only; the mixed case is not covered.
Options
- Map matches back to the original text. Normalise only for matching, then apply the edit to the original bytes, so untouched lines keep their own endings. Correct in every case, but it needs an offset mapping back to the original, and the LF text
viewhands the model has to be accounted for. - Only restore CRLF when the file was uniformly CRLF, and for a mixed file convert just the inserted text to the ending of the line it replaces. Smaller, keeps the current matching path, untouched lines stay put.
- Keep the rewrite and say so — extend the existing note so the response states the file's line endings were normalised. Smallest change, but the diff is still something the model did not ask for.
I lean towards 1 if the mapping can be kept small, otherwise 2, since both stop the untouched lines from changing.
If someone has already fixed this on another branch, please point me at it and I will close this.
Source: charmbracelet/crush