From 645895e3bf0a9e8af05ef79faab756df7091e1e6 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:42:14 +0800 Subject: [PATCH] fix(vfs): reload ignore rules after git init on Windows A watcher that started outside a repository caches, per directory, that no `.git` encloses it. After `git init` the explicit reload invalidated that cache from `/`, which is no ancestor of `C:\...`, so on Windows the stale entries stayed and `.git/info/exclude` / `.gitignore` entries written right after were not applied to listings until a file event arrived. Invalidate from the watch root's topmost ancestor instead. Also stop opening a directory as a plain file in the Python library index test: Windows refuses that without backup semantics; the mtime bump is only needed for coarse Unix file system clocks. --- crates/ide-index/src/python_library.rs | 6 ++++-- crates/ide-vfs/src/watch.rs | 6 +++++- crates/ide-vfs/tests/vfs.rs | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/ide-index/src/python_library.rs b/crates/ide-index/src/python_library.rs index bc2c2606c..e3eace396 100644 --- a/crates/ide-index/src/python_library.rs +++ b/crates/ide-index/src/python_library.rs @@ -421,8 +421,10 @@ mod tests { // `pip install`: a new top-level entry changes the folder's modification time. std::thread::sleep(Duration::from_millis(20)); write(dir.path(), "newpkg/__init__.py", "def installed_later(): pass\n"); - let file = fs::File::open(dir.path()).unwrap(); - file.set_modified(SystemTime::now() + Duration::from_secs(5)).ok(); + // Coarse file system clocks may not move within the test; push it forward. Windows can't + // open a directory as a plain file (it needs backup semantics), and NTFS keeps fine times. + #[cfg(unix)] + fs::File::open(dir.path()).unwrap().set_modified(SystemTime::now() + Duration::from_secs(5)).ok(); let second = libraries.index(&roots, &syntax, &cancel).unwrap(); assert!(!Arc::ptr_eq(&first, &second)); assert_eq!(candidates(&second, "installed_later"), ["from newpkg import installed_later"]); diff --git a/crates/ide-vfs/src/watch.rs b/crates/ide-vfs/src/watch.rs index 82ada236b..717b27309 100644 --- a/crates/ide-vfs/src/watch.rs +++ b/crates/ide-vfs/src/watch.rs @@ -142,7 +142,11 @@ impl Watcher { /// Explicit refresh for writes to Git common metadata outside the watched root. pub fn reload_ignore_rules(&self) { self.rules.reload_repo_level(); - self.rules.invalidate_tree(self.rules.repo_root().unwrap_or(Path::new("/"))); + // Outside a repository, everything cached up to the filesystem root may have probed for a + // `.git` that now exists (`git init`). `/` is no ancestor of `C:\…` on Windows, so take + // the watch root's own topmost ancestor. + let top = self.root.ancestors().last().unwrap_or(&self.root); + self.rules.invalidate_tree(self.rules.repo_root().unwrap_or(top)); } /// The watched directory (canonical). diff --git a/crates/ide-vfs/tests/vfs.rs b/crates/ide-vfs/tests/vfs.rs index d179974b8..f02b1bc1a 100644 --- a/crates/ide-vfs/tests/vfs.rs +++ b/crates/ide-vfs/tests/vfs.rs @@ -350,6 +350,27 @@ fn watcher_read_dir_reuses_its_ignore_rules() { assert!(watcher.read_dir(&other, &[]).unwrap().iter().all(|e| !e.ignored)); } +/// A repository made after the watch started (`git init` in an open project) and the ignore +/// entries written into it are seen by the next listing once the rules are reloaded. +#[test] +fn watcher_read_dir_sees_a_repository_made_after_it_started() { + let (_t, r) = root(); + write(&r.join("shared.ignore-me"), "x"); + write(&r.join("private.ignore-me"), "x"); + let sink: SharedSink = Arc::new(|_| {}); + let watcher = Watcher::start(&r, vec![], sink, Box::new(|_| {})).unwrap(); + assert_eq!(watcher.repo_root(), None); + // Listed once before `git init`: the rules remember that no repository encloses the folder. + assert!(watcher.read_dir(&r, &[]).unwrap().iter().all(|e| !e.ignored)); + write(&r.join(".git/HEAD"), "ref: refs/heads/main\n"); + write(&r.join(".gitignore"), "/shared.ignore-me\n"); + write(&r.join(".git/info/exclude"), "/private.ignore-me\n"); + watcher.reload_ignore_rules(); + let ignored: BTreeSet = + watcher.read_dir(&r, &[]).unwrap().into_iter().filter(|e| e.ignored).map(|e| e.name).collect(); + assert_eq!(ignored, BTreeSet::from(["private.ignore-me".to_string(), "shared.ignore-me".to_string()])); +} + #[test] fn watcher_reports_changes() { let (_t, r) = root();