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.

worktree: Clean up watches when symlinks disappear during scanning (#65939)

Ben Kunkle63089a6ebed9
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index 9fc91c5a55d..e6ef06beb70 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -1466,6 +1466,7 @@ struct FakeFsState {
buffered_events: Vec<PathEvent>,
metadata_call_count: usize,
read_dir_call_count: usize,
+ read_dir_hooks: BTreeMap<PathBuf, BoxFuture<'static, ()>>,
path_write_counts: std::collections::HashMap<PathBuf, usize>,
job_event_subscribers: Arc<Mutex<Vec<JobEventSender>>>,
trash: Mutex<SlotMap<TrashId, (TrashedEntry, FakeFsEntry)>>,
@@ -1861,6 +1862,7 @@ impl FakeFs {
events_paused: false,
read_dir_call_count: 0,
metadata_call_count: 0,
+ read_dir_hooks: Default::default(),
path_write_counts: Default::default(),
job_event_subscribers: Arc::new(Mutex::new(Vec::new())),
trash: Mutex::new(SlotMap::with_key()),
@@ -2083,6 +2085,19 @@ impl FakeFs {
self.state.lock().watches.watch_calls.clone()
}
+ /// The paths currently registered with the watcher backend.
+ pub fn registered_watch_paths(&self) -> Vec<PathBuf> {
+ self.state.lock().watches.registered_paths.clone()
+ }
+
+ /// Runs once, immediately before the next `read_dir` resolves `path`.
+ pub fn before_read_dir(&self, path: impl AsRef<Path>, hook: BoxFuture<'static, ()>) {
+ self.state
+ .lock()
+ .read_dir_hooks
+ .insert(normalize_path(path.as_ref()), hook);
+ }
+
/// Creates `path` without emitting an event when `watch_path` is next
/// watched, simulating a change that lands while the watch is installed.
pub fn create_file_before_next_watch_add(
@@ -3411,6 +3426,10 @@ impl Fs for FakeFs {
) -> Result<Pin<Box<dyn Send + Stream<Item = Result<PathBuf>>>>> {
self.simulate_random_delay().await;
let path = normalize_path(path);
+ let hook = self.state.lock().read_dir_hooks.remove(&path);
+ if let Some(hook) = hook {
+ hook.await;
+ }
let mut state = self.state.lock();
state.read_dir_call_count += 1;
let entry = state.entry(&path)?;
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index 8d5477f52a7..28ee817079b 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -5436,6 +5436,26 @@ impl BackgroundScanner {
Some(job.abs_path.clone())
};
+ if let Some(watched_abs_path) = watched_abs_path {
+ // Deletion cleanup needs the canonical path even if enumeration fails.
+ let mut state = self.state.lock().await;
+ if job.is_external {
+ state
+ .snapshot
+ .external_canonical_to_relative
+ .insert(watched_abs_path.clone(), job.path.clone());
+ }
+ if let Some(entry_id) = state
+ .snapshot
+ .entry_for_path(&job.path)
+ .map(|entry| entry.id)
+ {
+ state
+ .watched_dir_abs_paths_by_entry_id
+ .insert(entry_id, watched_abs_path);
+ }
+ }
+
let mut child_paths = self
.fs
.read_dir(&job.abs_path)
@@ -5652,23 +5672,6 @@ impl BackgroundScanner {
}
state.populate_dir(job.path.clone(), new_entries, new_ignore);
- if let Some(watched_abs_path) = &watched_abs_path {
- if job.is_external {
- state
- .snapshot
- .external_canonical_to_relative
- .insert(watched_abs_path.clone(), job.path.clone());
- }
- if let Some(entry_id) = state
- .snapshot
- .entry_for_path(&job.path)
- .map(|entry| entry.id)
- {
- state
- .watched_dir_abs_paths_by_entry_id
- .insert(entry_id, watched_abs_path.clone());
- }
- }
for new_job in new_jobs.into_iter().flatten() {
job.scan_queue
diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rs
index 15a8808b316..d63a6b2405f 100644
--- a/crates/worktree/tests/integration/worktree_tests.rs
+++ b/crates/worktree/tests/integration/worktree_tests.rs
@@ -869,6 +869,69 @@ async fn test_scan_symlinks_always(cx: &mut TestAppContext) {
});
}
+#[gpui::test]
+async fn test_deleted_external_symlink_before_read_dir_cleans_up_watch(cx: &mut TestAppContext) {
+ init_test(cx);
+ cx.update(|cx| {
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.project.worktree.scan_symlinks =
+ Some(settings::ScanSymlinksSetting::Always);
+ });
+ });
+ });
+
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree("/root", json!({})).await;
+ fs.insert_tree("/outside", json!({"data": {}})).await;
+ fs.create_symlink("/root/link".as_ref(), "/outside/data".into())
+ .await
+ .unwrap();
+
+ fs.before_read_dir(
+ "/root/link",
+ Box::pin({
+ let fs = fs.clone();
+ async move {
+ let mut watched_paths = fs.registered_watch_paths();
+ watched_paths.sort();
+ assert_eq!(
+ watched_paths,
+ vec![PathBuf::from("/outside/data"), PathBuf::from("/root")]
+ );
+ fs.remove_file("/root/link".as_ref(), RemoveOptions::default())
+ .await
+ .unwrap();
+ }
+ }),
+ );
+
+ let tree = Worktree::local(
+ Path::new("/root"),
+ true,
+ fs.clone(),
+ Default::default(),
+ true,
+ WorktreeId::from_proto(0),
+ &mut cx.to_async(),
+ )
+ .await
+ .unwrap();
+ cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
+ .await;
+ tree.flush_fs_events(cx).await;
+
+ tree.read_with(cx, |tree, _| {
+ assert_eq!(
+ tree.entries(true, 0)
+ .map(|entry| entry.path.as_ref())
+ .collect::<Vec<_>>(),
+ vec![rel_path("")]
+ );
+ });
+ assert_eq!(fs.registered_watch_paths(), vec![PathBuf::from("/root")]);
+}
+
#[gpui::test]
async fn test_relative_path_for_external_abs_path(cx: &mut TestAppContext) {
init_test(cx);