Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
worktree: Reload git state when a watcher rescan covers a repository (#61541)
Eric Holk64672ee8161a
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rsindex 01a76c04699..a7784922c74 100644--- a/crates/fs/src/fs.rs+++ b/crates/fs/src/fs.rs@@ -1853,6 +1853,20 @@ impl FakeFs {self.state.lock().buffered_events.clear();}+ /// Simulates the kernel's watch queue overflowing: all buffered+ /// (undelivered) events are lost, and the watcher reports only a single+ /// `Rescan` event for `root`, mirroring how the native backends report+ /// lost sync (FSEvents `kFSEventStreamEventFlagMustScanSubDirs`, inotify+ /// `IN_Q_OVERFLOW`, Windows `ERROR_NOTIFY_ENUM_DIR`).+ ///+ /// Note that the fake file system's state is unaffected; like a real+ /// overflow, only the notifications are lost, not the changes themselves.+ pub fn simulate_watcher_overflow(&self, root: impl Into<PathBuf>) {+ let mut state = self.state.lock();+ state.buffered_events.clear();+ state.emit_event([(root, Some(PathEventKind::Rescan))]);+ }+pub fn create_file_before_next_watch_add(&self,watch_path: impl AsRef<Path>,diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rsindex 9350d193a8a..297292476e0 100644--- a/crates/worktree/src/worktree.rs+++ b/crates/worktree/src/worktree.rs@@ -1485,6 +1485,18 @@ impl LocalWorktree {new_repos.next();}Ordering::Equal => {+ // A change to a repository's git state is signaled by bumping+ // `git_dir_scan_id`, and the diff below detects it via `!=`. If the+ // value ever regresses (e.g. a rescan re-inserting the repository+ // with a fresh scan id of 0), a bump from the same cycle is wiped+ // out and the corresponding git update is silently lost.+ debug_assert!(+ new_repo.git_dir_scan_id >= old_repo.git_dir_scan_id,+ "git_dir_scan_id for repository at {:?} regressed from {} to {}",+ new_repo.work_directory_abs_path,+ old_repo.git_dir_scan_id,+ new_repo.git_dir_scan_id,+ );if new_repo.git_dir_scan_id != old_repo.git_dir_scan_id|| new_repo.work_directory_abs_path!= old_repo.work_directory_abs_path@@ -3575,11 +3587,28 @@ impl BackgroundScannerState {let work_directory_id = work_dir_entry.id;+ // A repository can be re-inserted when its `.git` entry is re-discovered, e.g.+ // during a watcher-forced rescan. Carry the existing `git_dir_scan_id` forward+ // in that case: the snapshot diff detects git changes by comparing scan ids, so+ // resetting the id would wipe out a bump made earlier in the same scan cycle,+ // silently dropping the corresponding git update. Deliberately don't bump the+ // id here either: re-insertion is snapshot bookkeeping, not evidence that git+ // state changed. It also happens on non-lossy paths (explicit refreshes,+ // path-prefix scans) where we know nothing in `.git` changed, and a bump would+ // trigger a spurious full reload of the repository's git state. Only+ // `update_git_repositories` claims that git state changed, by stamping the+ // current scan id.+ let git_dir_scan_id = self+ .snapshot+ .git_repositories+ .get(&work_directory_id)+ .map_or(0, |existing_repository| existing_repository.git_dir_scan_id);+let local_repository = LocalRepositoryEntry {work_directory_id,work_directory,work_directory_abs_path: work_directory_abs_path.as_path().into(),- git_dir_scan_id: 0,+ git_dir_scan_id,dot_git_abs_path,common_dir_abs_path,repository_dir_abs_path,@@ -4826,6 +4855,31 @@ impl BackgroundScanner {}}+ // A rescan event means the watcher lost sync and events under the+ // rescanned path were dropped, possibly including events inside `.git`+ // directories. Reload the git state of every repository with a git+ // directory under the rescanned path, since changes there may have+ // gone unseen.+ if self.track_git_repositories && matches!(event.kind, Some(PathEventKind::Rescan))+ {+ for repository in snapshot.git_repositories.values() {+ let affected_by_rescan = [+ &repository.dot_git_abs_path,+ &repository.common_dir_abs_path,+ &repository.repository_dir_abs_path,+ ]+ .iter()+ .any(|git_dir_abs_path| git_dir_abs_path.starts_with(abs_path.as_path()));+ let dot_git_abs_path = repository.dot_git_abs_path.to_path_buf();+ if affected_by_rescan && !dot_git_abs_paths.contains(&dot_git_abs_path) {+ log::debug!(+ "reloading git repo at {dot_git_abs_path:?} due to rescan of {abs_path:?}"+ );+ dot_git_abs_paths.push(dot_git_abs_path);+ }+ }+ }+if self.track_git_repositories&& abs_path.as_path()diff --git a/crates/worktree/tests/integration/worktree_tests.rs b/crates/worktree/tests/integration/worktree_tests.rsindex 41b27cb09c3..4b57bfc92d5 100644--- a/crates/worktree/tests/integration/worktree_tests.rs+++ b/crates/worktree/tests/integration/worktree_tests.rs@@ -3066,9 +3066,17 @@ async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng)let buffered_event_count = fs.as_fake().buffered_event_count();if buffered_event_count > 0 && rng.random_bool(0.3) {- let len = rng.random_range(0..=buffered_event_count);- log::info!("flushing {} events", len);- fs.as_fake().flush_events(len);+ if rng.random_bool(0.2) {+ log::info!(+ "simulating watcher overflow, losing {} events",+ buffered_event_count+ );+ fs.as_fake().simulate_watcher_overflow(root_dir);+ } else {+ let len = rng.random_range(0..=buffered_event_count);+ log::info!("flushing {} events", len);+ fs.as_fake().flush_events(len);+ }} else {randomly_mutate_fs(&fs, root_dir, 0.6, &mut rng).await;mutations_len -= 1;@@ -3155,6 +3163,120 @@ async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng)}}+#[gpui::test(iterations = 100)]+async fn test_random_git_updates_with_watcher_overflows(cx: &mut TestAppContext, mut rng: StdRng) {+ // Property: every git state change is eventually signaled via+ // `UpdatedGitRepositories`, no matter how the events reporting it are+ // batched, delayed, or lost to watcher overflows.+ init_test(cx);+ let operations = env::var("OPERATIONS")+ .map(|o| o.parse().unwrap())+ .unwrap_or(40);++ let root_dir = Path::new(path!("/test"));+ let dot_git = root_dir.join(".git");+ // Random fs mutations are confined to this subdirectory so that they+ // cannot rename or delete `.git` itself.+ let src_dir = root_dir.join("src");+ let fs = FakeFs::new(cx.background_executor.clone()) as Arc<dyn Fs>;+ fs.as_fake()+ .insert_tree(+ root_dir,+ json!({+ ".git": {},+ "src": {+ "main.rs": "fn main() {}",+ },+ }),+ )+ .await;++ let worktree = Worktree::local(+ root_dir,+ true,+ fs.clone(),+ Default::default(),+ true,+ WorktreeId::from_proto(0),+ &mut cx.to_async(),+ )+ .await+ .unwrap();+ worktree+ .update(cx, |tree, _| tree.as_local_mut().unwrap().scan_complete())+ .await;+ cx.executor().run_until_parked();++ // Set when the fake repository's state is mutated, cleared when the+ // worktree signals a git update. A signal observed after a mutation+ // prompts downstream consumers (the GitStore) to re-read the repository,+ // at which point they see that mutation's state, so clearing on any+ // subsequent signal is sound.+ let pending_git_update: Rc<Cell<bool>> = Rc::new(Cell::new(false));+ worktree.update(cx, {+ let pending_git_update = pending_git_update.clone();+ |_, cx| {+ cx.subscribe(&cx.entity(), move |_, _, event, _| {+ if matches!(event, Event::UpdatedGitRepositories(_)) {+ pending_git_update.set(false);+ }+ })+ .detach();+ }+ });++ fs.as_fake().pause_events();+ let mut commit_count = 0;+ for _ in 0..operations {+ match rng.random_range(0_u32..100) {+ // Change the repository's git state, e.g. a commit moving HEAD.+ 0..25 => {+ commit_count += 1;+ log::info!("setting HEAD to commit {commit_count}");+ fs.as_fake().set_head_for_repo(+ &dot_git,+ &[("src/main.rs", format!("fn main() {{}} // {commit_count}"))],+ format!("sha-{commit_count}"),+ );+ pending_git_update.set(true);+ }+ // The watch queue overflows: all undelivered events are lost and+ // only a rescan for the root is reported.+ 25..40 => {+ log::info!(+ "simulating watcher overflow, losing {} events",+ fs.as_fake().buffered_event_count()+ );+ fs.as_fake().simulate_watcher_overflow(root_dir);+ }+ // Deliver a prefix of the queued events.+ 40..65 => {+ let buffered_event_count = fs.as_fake().buffered_event_count();+ let len = rng.random_range(0..=buffered_event_count);+ log::info!("flushing {len} of {buffered_event_count} events");+ fs.as_fake().flush_events(len);+ }+ // Unrelated churn in the working tree.+ _ => {+ randomly_mutate_fs(&fs, &src_dir, 1.0, &mut rng).await;+ }+ }+ cx.executor().run_until_parked();+ }++ log::info!("quiescing");+ fs.as_fake().unpause_events_and_flush();+ cx.executor().run_until_parked();++ worktree.read_with(cx, |tree, _| {+ tree.as_local().unwrap().snapshot().check_invariants(true)+ });+ assert!(+ !pending_git_update.get(),+ "a git state change was never signaled via UpdatedGitRepositories"+ );+}+// The worktree's `UpdatedEntries` event can be used to follow along with// all changes to the worktree's snapshot.fn check_worktree_change_events(tree: &mut Worktree, cx: &mut Context<Worktree>) {@@ -4564,6 +4686,137 @@ async fn test_noisy_dot_git_events_do_not_emit_git_repo_update(}}+#[gpui::test]+async fn test_watcher_overflow_rescan_reloads_git_state(cx: &mut TestAppContext) {+ // When the OS watch queue overflows, pending events are dropped and the+ // watcher reports only a `Rescan` event for the worktree root. The dropped+ // events may have included changes inside `.git`, so the rescan must+ // trigger a git state reload even though no `.git` event is ever seen.+ init_test(cx);+ let fs = FakeFs::new(cx.background_executor.clone());+ fs.insert_tree(+ path!("/root"),+ json!({+ ".git": {},+ "file.txt": "content",+ }),+ )+ .await;++ let tree = Worktree::local(+ path!("/root").as_ref(),+ true,+ fs.clone(),+ Default::default(),+ true,+ WorktreeId::from_proto(0),+ &mut cx.to_async(),+ )+ .await+ .unwrap();+ tree.update(cx, |tree, _| tree.as_local().unwrap().scan_complete())+ .await;+ cx.run_until_parked();++ let repo_update_count: Rc<Cell<usize>> = Rc::new(Cell::new(0));+ tree.update(cx, {+ let repo_update_count = repo_update_count.clone();+ |_, cx| {+ cx.subscribe(&cx.entity(), move |_, _, event, _| {+ if matches!(event, Event::UpdatedGitRepositories(_)) {+ repo_update_count.set(repo_update_count.get() + 1);+ }+ })+ .detach();+ }+ });++ // A git state change occurs while events are queued but undelivered, and+ // is then lost to a watcher overflow: the only event the worktree ever+ // receives is the root rescan.+ fs.pause_events();+ fs.set_head_for_repo(+ path!("/root/.git").as_ref(),+ &[("file.txt", "content".into())],+ "sha-after-overflow",+ );+ fs.simulate_watcher_overflow(path!("/root"));+ fs.unpause_events_and_flush();+ cx.run_until_parked();++ assert!(+ repo_update_count.get() > 0,+ "a watcher overflow rescan should reload git state, since the dropped \+ events may have included .git changes"+ );+}++#[gpui::test]+async fn test_git_update_in_same_batch_as_rescan_is_not_lost(cx: &mut TestAppContext) {+ // When a `.git` event and a watcher rescan arrive in the same batch,+ // `update_git_repositories` stamps the repository's `git_dir_scan_id`, but+ // the rescan then re-inserts the repository entry. If the re-insertion+ // resets `git_dir_scan_id`, the stamp is lost before the snapshot diff can+ // observe it. The git update must still be signaled via+ // `UpdatedGitRepositories`.+ init_test(cx);+ let fs = FakeFs::new(cx.background_executor.clone());+ fs.insert_tree(+ path!("/root"),+ json!({+ ".git": {},+ "file.txt": "content",+ }),+ )+ .await;++ let tree = Worktree::local(+ path!("/root").as_ref(),+ true,+ fs.clone(),+ Default::default(),+ true,+ WorktreeId::from_proto(0),+ &mut cx.to_async(),+ )+ .await+ .unwrap();+ tree.update(cx, |tree, _| tree.as_local().unwrap().scan_complete())+ .await;+ cx.run_until_parked();++ let repo_update_count: Rc<Cell<usize>> = Rc::new(Cell::new(0));+ tree.update(cx, {+ let repo_update_count = repo_update_count.clone();+ |_, cx| {+ cx.subscribe(&cx.entity(), move |_, _, event, _| {+ if matches!(event, Event::UpdatedGitRepositories(_)) {+ repo_update_count.set(repo_update_count.get() + 1);+ }+ })+ .detach();+ }+ });++ // Deliver the git change and the rescan in a single batch, as happens when+ // a rescan arrives while other events are still queued.+ fs.pause_events();+ fs.set_head_for_repo(+ path!("/root/.git").as_ref(),+ &[("file.txt", "content".into())],+ "sha-with-rescan",+ );+ fs.emit_fs_event(path!("/root"), Some(PathEventKind::Rescan));+ fs.unpause_events_and_flush();+ cx.run_until_parked();++ assert!(+ repo_update_count.get() > 0,+ "a git update processed in the same batch as a rescan should still be \+ signaled via UpdatedGitRepositories"+ );+}+#[gpui::test]async fn test_linked_worktree_event_in_unregistered_common_git_dir_does_not_panic(executor: BackgroundExecutor,