Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 73 additions & 4 deletions rust/crates/runtime/src/file_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,23 +666,31 @@ 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",
));
}

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)?;

Expand All @@ -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)
Expand Down Expand Up @@ -1243,6 +1261,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");
Expand Down