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.

Rewatch a directory that was deleted and recreated (#61372)

Marcos Alcantara20d29fc6bc2f
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs
index f56c08e63f7..893c25fe4f8 100644
--- a/crates/fs/src/fs.rs
+++ b/crates/fs/src/fs.rs
@@ -1478,6 +1478,8 @@ struct FakeWatches {
registered_paths: Vec<PathBuf>,
watch_calls: Vec<PathBuf>,
event_sink: Option<Box<dyn Fn(notify::Result<notify::Event>) + Send + Sync>>,
+ /// A file to create, without an event, when the given path is next watched.
+ file_to_create_on_watch: Option<(PathBuf, PathBuf)>,
}
#[cfg(feature = "test-support")]
@@ -1787,6 +1789,29 @@ impl fs_watcher::WatchBackend for FakeWatchBackend {
"fake filesystem state is locked; this execution would have caused a test hang",
);
state.watches.watch_calls.push(path.clone());
+ if let Some((_, file_path)) = state
+ .watches
+ .file_to_create_on_watch
+ .take_if(|(watch_path, _)| *watch_path == path)
+ {
+ let inode = state.get_and_increment_inode();
+ let mtime = state.get_and_increment_mtime();
+ state
+ .write_path(&file_path, |entry| {
+ let btree_map::Entry::Vacant(entry) = entry else {
+ anyhow::bail!("file already exists: {}", file_path.display());
+ };
+ entry.insert(FakeFsEntry::File {
+ inode,
+ mtime,
+ len: 0,
+ content: Vec::new(),
+ git_dir_path: None,
+ });
+ Ok(())
+ })
+ .map_err(|error| notify::Error::generic(&error.to_string()))?;
+ }
state.watches.registered_paths.push(path);
Ok(())
}
@@ -2054,6 +2079,19 @@ impl FakeFs {
self.state.lock().watches.watch_calls.clone()
}
+ /// 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(
+ &self,
+ watch_path: impl AsRef<Path>,
+ path: impl AsRef<Path>,
+ ) {
+ self.state.lock().watches.file_to_create_on_watch = Some((
+ normalize_path(watch_path.as_ref()),
+ normalize_path(path.as_ref()),
+ ));
+ }
+
pub fn flush_events(&self, count: usize) {
self.state.lock().flush_events(count);
}
diff --git a/crates/fs/src/fs_watcher.rs b/crates/fs/src/fs_watcher.rs
index 836e769376d..4d0d185f5ae 100644
--- a/crates/fs/src/fs_watcher.rs
+++ b/crates/fs/src/fs_watcher.rs
@@ -144,9 +144,14 @@ impl FsWatcher {
fn add_existing_path(&self, path: Arc<Path>) -> anyhow::Result<()> {
let case_insensitive = !self.fs.is_path_case_sensitive(&path);
let key = WatchKey::for_registration(SanitizedPath::new(&path), case_insensitive);
- if self.registrations.lock().contains_key(&key) {
- log::trace!("path to watch is already watched: {path:?}");
- return Ok(());
+ let existing = self.registrations.lock().get(&key).cloned();
+ if let Some(existing) = existing {
+ log::trace!("path to watch is already registered: {path:?}");
+ return existing.os_watcher.rewatch_if_stale(
+ &mut existing.os_watcher.state.lock(),
+ SanitizedPath::new(&path),
+ &key,
+ );
}
match register_existing_path(
&self.native_watcher,
@@ -801,6 +806,7 @@ struct WatcherRegistrationState {
struct PathRegistrationState {
watcher_ids: Vec<WatcherRegistrationId>,
has_os_watcher: bool,
+ stale: bool,
}
/// The registered watch paths for one backend, keyed by [`WatchKey`] so that
@@ -870,6 +876,17 @@ impl WatcherState {
.is_some_and(|cooldown_until| cooldown_until > Instant::now())
}
+ fn start_native_watch_limit_cooldown(&mut self, path: &Path) {
+ let should_log = !self.is_native_watch_limit_cooldown_active();
+ self.cooldown_until = Some(Instant::now() + *NATIVE_WATCH_LIMIT_COOLDOWN);
+ if should_log {
+ log::warn!(
+ "OS file watch limit reached while watching {path:?}; skipping new native file watcher registrations for {} seconds",
+ NATIVE_WATCH_LIMIT_COOLDOWN.as_secs()
+ );
+ }
+ }
+
fn remove_registration(&mut self, id: WatcherRegistrationId) -> Option<Arc<SanitizedPath>> {
let registration_state = self.watchers.remove(&id)?;
let path_state = self.paths.get_mut(&registration_state.key)?;
@@ -905,10 +922,8 @@ pub struct OsWatcher {
recursive: bool,
state: Arc<Mutex<WatcherState>>,
- // Never hold the state lock while calling the backend: a backend call can be
- // slow (a poll watch scans its whole tree up front, an FSEvents watch rebuilds
- // the stream) and dispatch needs the state to route every other registration's
- // events. Whoever re-locks the state afterwards must re-check what they assumed.
+ // Hold state across backend mutations. Callbacks don't acquire state or
+ // invert the lock order.
backend: Mutex<Option<Box<dyn WatchBackend>>>,
event_tx: async_channel::Sender<notify::Result<notify::Event>>,
_dispatch_task: Task<()>,
@@ -998,7 +1013,9 @@ impl OsWatcher {
.covered_by_recursive_ancestor(&path, self.recursive);
let path_already_registered = state.paths.contains(&key);
- if !path_already_covered && !path_already_registered {
+ if path_already_registered {
+ self.rewatch_if_stale(&mut state, &path, &key)?;
+ } else if !path_already_covered {
if self.kind == OsWatcherKind::Native && state.is_native_watch_limit_cooldown_active() {
self.diagnostics.record(|| {
WatchDiagnosticEvent::new(
@@ -1013,18 +1030,16 @@ impl OsWatcher {
return Ok(None);
}
- drop(state);
match self.watch(path.as_path()) {
Ok(()) => {}
Err(error)
if self.kind == OsWatcherKind::Native && is_max_files_watch_error(&error) =>
{
- self.start_native_watch_limit_cooldown(path.as_path());
+ state.start_native_watch_limit_cooldown(path.as_path());
return Ok(None);
}
Err(error) => return Err(error),
}
- state = self.state.lock();
}
let id = state.last_registration;
@@ -1043,11 +1058,34 @@ impl OsWatcher {
.or_insert_with(|| PathRegistrationState {
watcher_ids: vec![id],
has_os_watcher: !path_already_covered,
+ stale: false,
});
Ok(Some(id))
}
+ fn rewatch_if_stale(
+ &self,
+ state: &mut WatcherState,
+ path: &SanitizedPath,
+ key: &WatchKey,
+ ) -> anyhow::Result<()> {
+ let Some(path_state) = state.paths.get_mut(key) else {
+ return Ok(());
+ };
+ if !path_state.stale {
+ return Ok(());
+ }
+
+ log::trace!("rewatching stale path: {path:?}");
+ // A renamed directory's watch survives under the old path; unwatch it
+ // so the backend won't skip installation as a duplicate.
+ self.unwatch(path.as_path()).log_err();
+ self.watch(path.as_path())?;
+ path_state.stale = false;
+ Ok(())
+ }
+
#[cfg(test)]
fn dispatch(&self, event: notify::Result<notify::Event>) {
dispatch(self.kind, &self.state, event);
@@ -1068,25 +1106,11 @@ impl OsWatcher {
);
}
- fn start_native_watch_limit_cooldown(&self, path: &Path) {
- let mut state = self.state.lock();
- let now = Instant::now();
- let should_log = !state.is_native_watch_limit_cooldown_active();
- state.cooldown_until = Some(now + *NATIVE_WATCH_LIMIT_COOLDOWN);
- if should_log {
- log::warn!(
- "OS file watch limit reached while watching {path:?}; skipping new native file watcher registrations for {} seconds",
- NATIVE_WATCH_LIMIT_COOLDOWN.as_secs()
- );
- }
- }
-
pub fn remove(&self, id: WatcherRegistrationId) {
let mut state = self.state.lock();
let Some(path) = state.remove_registration(id) else {
return;
};
- drop(state);
self.unwatch(path.as_path()).log_err();
}
@@ -1224,7 +1248,47 @@ fn dispatch(
log::trace!("handle {kind:?} watcher event: {event:?}");
let callbacks = {
- let state = state.lock();
+ let mut state = state.lock();
+ // Invalidate before callbacks can trigger a scan.
+ if !kind.is_recursive() && event.need_rescan() {
+ // Overflow may have dropped removal events.
+ for path_state in state.paths.0.values_mut() {
+ path_state.stale = true;
+ }
+ } else if !kind.is_recursive()
+ && matches!(
+ event.kind,
+ EventKind::Remove(_) | EventKind::Modify(notify::event::ModifyKind::Name(_))
+ )
+ {
+ for path in &event.paths {
+ let path = SanitizedPath::new(path);
+ for event_key in [WatchKey::exact(path), WatchKey::folded(path)] {
+ let Some(path_state) = state.paths.get_mut(&event_key) else {
+ continue;
+ };
+ path_state.stale = true;
+ if !matches!(event.kind, EventKind::Modify(_)) {
+ continue;
+ }
+ // Descendant watches follow the directory without their own rename events.
+ for (key, path_state) in &mut state.paths.0 {
+ let descendant = match (key, &event_key) {
+ (WatchKey::Exact(key), WatchKey::Exact(event_key)) => {
+ key.starts_with(event_key)
+ }
+ (WatchKey::Folded(key), WatchKey::Folded(event_key)) => {
+ Path::new(key.as_ref()).starts_with(Path::new(event_key.as_ref()))
+ }
+ _ => false,
+ };
+ if descendant {
+ path_state.stale = true;
+ }
+ }
+ }
+ }
+ }
if event.need_rescan() {
state
.watchers
@@ -1526,6 +1590,58 @@ mod tests {
assert_eq!(native_backend.watch_calls, &[first_path.to_path_buf()]);
}
+ #[cfg(not(any(target_os = "windows", target_os = "macos")))]
+ #[test]
+ fn removed_or_renamed_path_is_rewatched_once() {
+ let dir = PathBuf::from("/Repo/Dir");
+ let subdir = PathBuf::from("/Repo/Dir/Subdir");
+ let removal = notify::Event {
+ paths: vec![dir.clone()],
+ ..notify::Event::new(EventKind::Remove(notify::event::RemoveKind::Folder))
+ };
+ let overflow = notify::Event::new(EventKind::Other).set_flag(notify::event::Flag::Rescan);
+ let rename = |path: &str| notify::Event {
+ paths: vec![PathBuf::from(path)],
+ ..notify::Event::new(EventKind::Modify(notify::event::ModifyKind::Name(
+ notify::event::RenameMode::From,
+ )))
+ };
+
+ for (event, case_insensitive, rewatched) in [
+ (removal, false, &dir),
+ (overflow, false, &dir),
+ (rename("/Repo/Dir"), false, &subdir),
+ (rename("/repo/dir"), true, &subdir),
+ ] {
+ let backend = Arc::new(Mutex::new(FakeWatchBackend::default()));
+ let watcher = test_os_watcher(OsWatcherKind::Native, Some(backend.clone()));
+ for path in [&dir, &dir, &subdir] {
+ watcher
+ .add(path.as_path().into(), case_insensitive, |_| {})
+ .expect("add watch")
+ .expect("watch registered");
+ }
+
+ watcher.dispatch(Ok(event));
+ let rewatched = SanitizedPath::new(rewatched);
+ let key = WatchKey::for_registration(rewatched, case_insensitive);
+ watcher
+ .rewatch_if_stale(&mut watcher.state.lock(), rewatched, &key)
+ .expect("rewatch");
+
+ let backend = backend.lock();
+ assert_eq!(
+ backend.watch_calls,
+ &[
+ dir.clone(),
+ subdir.clone(),
+ rewatched.as_path().to_path_buf()
+ ]
+ );
+ assert_eq!(backend.unwatch_calls, &[rewatched.as_path().to_path_buf()]);
+ }
+ }
+
fn modify_event(path: &str) -> notify::Event {
notify::Event {
paths: vec![PathBuf::from(path)],
diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs
index 66997d31929..537876700a2 100644
--- a/crates/worktree/src/worktree.rs
+++ b/crates/worktree/src/worktree.rs
@@ -5406,6 +5406,32 @@ impl BackgroundScanner {
let mut root_canonical_path = None;
let mut new_entries: Vec<Entry> = Vec::new();
let mut new_jobs: Vec<Option<ScanJob>> = Vec::new();
+
+ // Watch before reading so a child created after enumeration still
+ // produces an event.
+ //
+ // For external entries, watch the canonical (resolved) path so OS-level
+ // FS events on the real filesystem location are observed. The same
+ // canonical path is stored in both `external_canonical_to_relative`
+ // (for translating canonical-path FS events back to worktree-relative
+ // paths) and `watched_dir_abs_paths_by_entry_id` (used by `remove_path`
+ // to know which abs path to unwatch), so both cleanup paths agree on
+ // the path the watcher was actually registered on.
+ let watched_abs_path: Option<Arc<Path>> = if job.is_external {
+ self.fs
+ .canonicalize(job.abs_path.as_ref())
+ .await
+ .ok()
+ .map(|canonical| {
+ let canonical: Arc<Path> = canonical.into();
+ self.watcher.add(&canonical).log_err();
+ canonical
+ })
+ } else {
+ self.watcher.add(job.abs_path.as_ref()).log_err();
+ Some(job.abs_path.clone())
+ };
+
let mut child_paths = self
.fs
.read_dir(&job.abs_path)
@@ -5622,33 +5648,6 @@ impl BackgroundScanner {
}
state.populate_dir(job.path.clone(), new_entries, new_ignore);
- // For external entries, watch the canonical (resolved) path so OS-level
- // FS events on the real filesystem location are observed. The same
- // canonical path is stored in both `external_canonical_to_relative`
- // (for translating canonical-path FS events back to worktree-relative
- // paths) and `watched_dir_abs_paths_by_entry_id` (used by `remove_path`
- // to know which abs path to unwatch), so both cleanup paths agree on
- // the path the watcher was actually registered on.
- //
- // `canonicalize` is an async filesystem operation that may suspend, so
- // the lock must not be held across the await point below.
- drop(state);
- let watched_abs_path: Option<Arc<Path>> = if job.is_external {
- self.fs
- .canonicalize(job.abs_path.as_ref())
- .await
- .ok()
- .map(|canonical| {
- let canonical: Arc<Path> = canonical.into();
- self.watcher.add(&canonical).log_err();
- canonical
- })
- } else {
- self.watcher.add(job.abs_path.as_ref()).log_err();
- Some(job.abs_path.clone())
- };
-
- let mut state = self.state.lock().await;
if let Some(watched_abs_path) = &watched_abs_path {
if job.is_external {
state
diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rs
index 7bd1179d16a..2c7af1413bf 100644
--- a/crates/worktree/tests/integration/worktree_tests.rs
+++ b/crates/worktree/tests/integration/worktree_tests.rs
@@ -1660,8 +1660,10 @@ async fn test_root_rescan_keeps_root_watcher_registered(cx: &mut TestAppContext)
cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
.await;
- // Dropping and re-registering the root watch would open a window in
- // which filesystem events are lost.
+ // Dropping and re-registering the recursive root watch would open a window
+ // in which filesystem events are lost. Non-recursive watches may have been
+ // lost in the overflow, so the rescan reinstalls the root watch before
+ // reading the root again.
fs.emit_fs_event("/root", Some(PathEventKind::Rescan));
tree.flush_fs_events(cx).await;
@@ -1670,7 +1672,65 @@ async fn test_root_rescan_keeps_root_watcher_registered(cx: &mut TestAppContext)
.into_iter()
.filter(|path| path == Path::new("/root"))
.count();
- assert_eq!(root_watch_calls, 1);
+ let expected_root_watch_calls = if cfg!(any(target_os = "windows", target_os = "macos")) {
+ 1
+ } else {
+ 2
+ };
+ assert_eq!(root_watch_calls, expected_root_watch_calls);
+}
+
+// Native watches are recursive on macOS and Windows, so the new directory is
+// covered by the root watch and never watched separately.
+#[cfg(not(any(target_os = "windows", target_os = "macos")))]
+#[gpui::test]
+async fn test_new_directory_scan_does_not_miss_event_before_adding_watcher(
+ cx: &mut TestAppContext,
+) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree("/root", json!({})).await;
+
+ 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;
+
+ fs.create_file_before_next_watch_add("/root/new-directory", "/root/new-directory/file.txt");
+ fs.create_dir(Path::new("/root/new-directory"))
+ .await
+ .unwrap();
+
+ wait_for_condition(cx, |cx| {
+ tree.read_with(cx, |tree, _| {
+ tree.entry_for_path(rel_path("new-directory/file.txt"))
+ .is_some()
+ })
+ })
+ .await;
+
+ tree.read_with(cx, |tree, _| {
+ assert_eq!(
+ tree.entries(true, 0)
+ .map(|entry| entry.path.as_ref())
+ .collect::<Vec<_>>(),
+ vec![
+ rel_path(""),
+ rel_path("new-directory"),
+ rel_path("new-directory/file.txt"),
+ ]
+ );
+ });
}
#[gpui::test]