Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

fs: Stop FakeFs deleting files it should leave alone (#63416)

Petros Amoiridis82aeef2d8f4c
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 00f64d93e63..0cc7d665844 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -2998,20 +2998,32 @@ impl Fs for FakeFs {
}
})?;
+ // POSIX `rename` succeeds without doing anything when both names resolve
+ // to the same file. Falling through would assign the entry onto itself
+ // and then remove it, destroying the file. The lookup above has already
+ // reported a missing source, so only an existing one reaches here.
+ if old_path == new_path {
+ return Ok(());
+ }
+
let inode = match moved_entry {
FakeFsEntry::File { inode, .. } => inode,
FakeFsEntry::Dir { inode, .. } => inode,
_ => 0,
};
- state.moves.insert(inode, new_path.clone());
-
+ let mut moved = true;
state.write_path(&new_path, |e| {
match e {
btree_map::Entry::Occupied(mut e) => {
if options.overwrite {
*e.get_mut() = moved_entry;
- } else if !options.ignore_if_exists {
+ } else if options.ignore_if_exists {
+ // `RealFs` reports success without moving anything here,
+ // leaving the source in place. Removing it instead would
+ // destroy a file the caller still expects to find.
+ moved = false;
+ } else {
anyhow::bail!("path already exists: {new_path:?}");
}
}
@@ -3022,6 +3034,12 @@ impl Fs for FakeFs {
Ok(())
})?;
+ if !moved {
+ return Ok(());
+ }
+
+ state.moves.insert(inode, new_path.clone());
+
state
.write_path(&old_path, |e| {
if let btree_map::Entry::Occupied(e) = e {
diff --git a/crates/fs/tests/integration/fs_tests.rs b/crates/fs/tests/integration/fs_tests.rs
index 06e9facb987..a18aef6eddf 100644
--- a/crates/fs/tests/integration/fs_tests.rs
+++ b/crates/fs/tests/integration/fs_tests.rs
@@ -590,6 +590,82 @@ async fn test_realfs_rename_ignore_if_exists_leaves_source_and_target_unchanged(
assert_eq!(std::fs::read_to_string(&target).unwrap(), "from target");
}
+#[gpui::test]
+async fn test_fake_fs_rename_ignore_if_exists_leaves_source_and_target_unchanged(
+ executor: BackgroundExecutor,
+) {
+ let fs = FakeFs::new(executor);
+ fs.insert_tree(
+ path!("/root"),
+ json!({
+ "source.txt": "from source",
+ "target.txt": "from target",
+ }),
+ )
+ .await;
+
+ let handle = fs
+ .open_handle(Path::new(path!("/root/source.txt")))
+ .await
+ .unwrap();
+
+ let result = fs
+ .rename(
+ Path::new(path!("/root/source.txt")),
+ Path::new(path!("/root/target.txt")),
+ RenameOptions {
+ ignore_if_exists: true,
+ ..Default::default()
+ },
+ )
+ .await;
+
+ assert!(result.is_ok());
+
+ assert_eq!(
+ fs.load(Path::new(path!("/root/source.txt"))).await.unwrap(),
+ "from source"
+ );
+ assert_eq!(
+ fs.load(Path::new(path!("/root/target.txt"))).await.unwrap(),
+ "from target"
+ );
+
+ // An ignored rename must not be recorded as a move either, or a handle held
+ // across it reports a path its file never went to.
+ assert!(
+ handle.current_path(&(fs.clone() as Arc<dyn Fs>)).is_err(),
+ "an ignored rename should not record a move"
+ );
+}
+
+#[gpui::test]
+async fn test_fake_fs_rename_onto_itself_keeps_the_file(executor: BackgroundExecutor) {
+ let fs = FakeFs::new(executor);
+ fs.insert_tree(
+ path!("/root"),
+ json!({
+ "a.txt": "content",
+ }),
+ )
+ .await;
+
+ let path = Path::new(path!("/root/a.txt"));
+ let result = fs
+ .rename(
+ path,
+ path,
+ RenameOptions {
+ overwrite: true,
+ ..Default::default()
+ },
+ )
+ .await;
+
+ assert!(result.is_ok());
+ assert_eq!(fs.load(path).await.unwrap(), "content");
+}
+
#[gpui::test]
#[cfg(unix)]
async fn test_realfs_broken_symlink_metadata(executor: BackgroundExecutor) {