Compare commits

...
Author SHA1 Message Date
b84330808 5cc1178a21 fix(runtime): match CRLF line endings in edit_file
read_file and grep_search return a file's lines joined with `\n`, so a
multi-line old_string quoted from a CRLF file never matched and
edit_file failed with "old_string not found in file". On Windows that is
every multi-line edit in a checkout made with Git's default
core.autocrlf=true.

When every line break in the file is `\r\n`, convert old_string and
new_string to CRLF before matching, so the edit applies and inserted
lines keep the file's line endings. LF and mixed files are unchanged.
2026-11-22 19:19:37 +00:00
+73 -4
View File
@@ -666,13 +666,21 @@ pub fn edit_file(
) -> io::Result<EditFileOutput> {
let absolute_path = normalize_path(path)?;
let original_file = fs::read_to_string(&absolute_path)?;
if old_string == new_string {
// read_file and grep_search return lines joined with `\n`, so text quoted
// from a CRLF file arrives without its `\r`. Match the file's line endings
// so multi-line edits apply and inserted lines stay CRLF.
let (target, replacement) = if uses_crlf_line_endings(&original_file) {
(to_crlf(old_string), to_crlf(new_string))
} else {
(old_string.to_owned(), new_string.to_owned())
};
if target == replacement {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"old_string and new_string must differ",
));
}
if !original_file.contains(old_string) {
if !original_file.contains(&target) {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"old_string not found in file",
@@ -680,9 +688,9 @@ pub fn edit_file(
}
let updated = if replace_all {
original_file.replace(old_string, new_string)
original_file.replace(&target, &replacement)
} else {
original_file.replacen(old_string, new_string, 1)
original_file.replacen(&target, &replacement, 1)
};
fs::write(&absolute_path, &updated)?;
@@ -698,6 +706,16 @@ pub fn edit_file(
})
}
/// Whether the text has line breaks and every one of them is `\r\n`.
fn uses_crlf_line_endings(text: &str) -> bool {
let line_feeds = text.matches('\n').count();
line_feeds > 0 && text.matches("\r\n").count() == line_feeds
}
fn to_crlf(text: &str) -> String {
text.replace("\r\n", "\n").replace('\n', "\r\n")
}
/// Expands a glob pattern and returns matching filenames.
pub fn glob_search(pattern: &str, path: Option<&str>) -> io::Result<GlobSearchOutput> {
glob_search_impl(pattern, path, None)
@@ -1251,6 +1269,57 @@ mod tests {
assert!(output.replace_all);
}
#[test]
fn edits_crlf_file_with_lines_copied_from_read_file() {
let path = temp_path("edit-crlf.rs");
std::fs::write(&path, "fn main() {\r\n println!(\"hi\");\r\n}\r\n")
.expect("initial write should succeed");
let path_str = path.to_string_lossy();
// read_file returns the lines joined with `\n`, so a block quoted from
// its output does not contain the file's `\r\n` line endings.
let read = read_file(path_str.as_ref(), None, None).expect("read should succeed");
let old_string = read
.file
.content
.lines()
.take(2)
.collect::<Vec<_>>()
.join("\n");
assert_eq!(old_string, "fn main() {\n println!(\"hi\");");
edit_file(
path_str.as_ref(),
&old_string,
"fn main() {\n println!(\"hello\");\n println!(\"world\");",
false,
)
.expect("edit should match the CRLF lines");
assert_eq!(
std::fs::read_to_string(&path).expect("read back should succeed"),
"fn main() {\r\n println!(\"hello\");\r\n println!(\"world\");\r\n}\r\n"
);
let _ = std::fs::remove_file(&path);
}
#[test]
fn edit_keeps_line_endings_of_lf_and_mixed_files() {
let path = temp_path("edit-lf.txt");
std::fs::write(&path, "one\ntwo\r\nthree\n").expect("initial write should succeed");
edit_file(
path.to_string_lossy().as_ref(),
"one\n",
"uno\nwon\n",
false,
)
.expect("edit should succeed");
assert_eq!(
std::fs::read_to_string(&path).expect("read back should succeed"),
"uno\nwon\ntwo\r\nthree\n"
);
let _ = std::fs::remove_file(&path);
}
#[test]
fn rejects_binary_files() {
let path = temp_path("binary-test.bin");