From 15d04a6d05c734cd72ee64142f5d2793226decd4 Mon Sep 17 00:00:00 2001 From: thekevinbot <248217931+thekevinbot@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:09:28 -0500 Subject: [PATCH 1/4] Test that the watch walk skips gitignored directories and re-walks on a .gitignore edit --- packages/rust/tests/scanner.rs | 38 ++++++++++ packages/rust/tests/watch_gitignore_change.rs | 75 +++++++++++++++++++ packages/rust/tests/watcher.rs | 58 ++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 packages/rust/tests/watch_gitignore_change.rs diff --git a/packages/rust/tests/scanner.rs b/packages/rust/tests/scanner.rs index 4b941bff..8631c726 100644 --- a/packages/rust/tests/scanner.rs +++ b/packages/rust/tests/scanner.rs @@ -364,3 +364,41 @@ fn scan_dirs_from_a_directory_under_a_link_lists_it_at_its_target() { vec![root.join("real/sub")] ); } + +fn repo_ignoring_build() -> TempDir { + let dir = TempDir::new().unwrap(); + let root = dir.path(); + fs::create_dir(root.join(".git")).unwrap(); + fs::write(root.join(".gitignore"), "build/\n").unwrap(); + fs::create_dir_all(root.join("build/sub")).unwrap(); + fs::create_dir(root.join("docs")).unwrap(); + dir +} + +#[test] +fn scan_dirs_leaves_out_a_gitignored_directory_a_glob_reaches() { + let dir = repo_ignoring_build(); + let root = dir.path(); + let matcher = TableMatcher::new(&[("**/*.md", "t")], &[]) + .unwrap() + .with_gitignore(true); + + let mut dirs = scan_dirs(root, root, &matcher); + dirs.sort(); + + assert_eq!(dirs, vec![root.to_path_buf(), root.join("docs")]); +} + +#[test] +fn scan_dirs_enters_a_gitignored_directory_a_glob_spells() { + let dir = repo_ignoring_build(); + let root = dir.path(); + let matcher = TableMatcher::new(&[("build/x.md", "t")], &[]) + .unwrap() + .with_gitignore(true); + + let mut dirs = scan_dirs(root, root, &matcher); + dirs.sort(); + + assert_eq!(dirs, vec![root.to_path_buf(), root.join("build")]); +} diff --git a/packages/rust/tests/watch_gitignore_change.rs b/packages/rust/tests/watch_gitignore_change.rs new file mode 100644 index 00000000..f22363e1 --- /dev/null +++ b/packages/rust/tests/watch_gitignore_change.rs @@ -0,0 +1,75 @@ +//! A `.gitignore` edit while watching brings the directories it un-ignores +//! into the live index. Real watcher, real filesystem, SDK public API. + +use std::collections::HashMap; +use std::time::{Duration, Instant}; +use std::{fs, thread}; + +use dirsql::{DirSQL, Table, Value}; +use tempfile::TempDir; + +fn names(db: &DirSQL) -> Vec { + let mut out: Vec = db + .query("SELECT name FROM t") + .unwrap() + .iter() + .map(|r| match r.get("name") { + Some(Value::Text(s)) => s.clone(), + other => panic!("name was not text: {other:?}"), + }) + .collect(); + out.sort(); + out +} + +/// A repo whose `.gitignore` ignores `build/`, which holds `old.md`, watched +/// with a `**/*.md` table; then the `.gitignore` emptied. +fn un_ignored_build() -> (TempDir, DirSQL) { + let dir = TempDir::new().unwrap(); + fs::create_dir(dir.path().join(".git")).unwrap(); + fs::create_dir_all(dir.path().join("build/deep")).unwrap(); + fs::write(dir.path().join("build/deep/old.md"), "x").unwrap(); + fs::write(dir.path().join(".gitignore"), "build/\n").unwrap(); + let table = Table::new("t", "CREATE TABLE t (name TEXT)", "**/*.md", |path| { + let name = path.rsplit(['/', '\\']).next().unwrap().to_string(); + vec![HashMap::from([("name".into(), Value::Text(name))])] + }); + let db = DirSQL::new(dir.path(), vec![table]).unwrap(); + assert!(names(&db).is_empty()); + thread::sleep(Duration::from_millis(250)); + fs::write(dir.path().join(".gitignore"), "").unwrap(); + (dir, db) +} + +fn poll_until(db: &DirSQL, want: &[&str]) -> Vec { + let deadline = Instant::now() + Duration::from_secs(5); + while Instant::now() < deadline && names(db) != want { + db.poll_events(Duration::from_millis(200)).unwrap(); + } + names(db) +} + +// FSEvents and ReadDirectoryChangesW watch the whole tree with one watch, so +// there is no newly watched directory to walk. +#[cfg(not(any(target_os = "macos", target_os = "windows")))] +#[test] +fn a_gitignore_edit_indexes_the_files_of_a_directory_it_un_ignores() { + let (_dir, db) = un_ignored_build(); + + assert_eq!(poll_until(&db, &["old.md"]), ["old.md"]); +} + +#[test] +fn a_file_written_in_a_directory_a_gitignore_edit_un_ignores_is_indexed() { + let (dir, db) = un_ignored_build(); + thread::sleep(Duration::from_millis(250)); + db.poll_events(Duration::from_millis(200)).unwrap(); + + fs::write(dir.path().join("build/deep/new.md"), "x").unwrap(); + + assert!( + poll_until(&db, &["new.md", "old.md"]).contains(&"new.md".to_string()), + "got: {:?}", + names(&db) + ); +} diff --git a/packages/rust/tests/watcher.rs b/packages/rust/tests/watcher.rs index 6a19f72c..043a3af9 100644 --- a/packages/rust/tests/watcher.rs +++ b/packages/rust/tests/watcher.rs @@ -317,6 +317,64 @@ fn scoped_watcher_puts_no_watch_on_a_directory_no_table_glob_reaches() { assert!(!created_under(&events, &unmatched), "saw: {events:?}"); } +/// A repo ignoring `build/`, holding `docs` and each of `dirs`, watched by one +/// `**/*.md` table that honors `.gitignore`. +fn gitignored_repo(dirs: &[&str]) -> (TempDir, std::path::PathBuf, Watcher) { + let dir = TempDir::new().unwrap(); + let root = dir.path().canonicalize().unwrap(); + fs::create_dir(root.join(".git")).unwrap(); + fs::write(root.join(".gitignore"), "build/\n").unwrap(); + for sub in dirs.iter().chain(&["docs"]) { + fs::create_dir(root.join(sub)).unwrap(); + } + let watcher = Watcher::scoped(vec![WatchScope::new( + root.clone(), + TableMatcher::new(&[("**/*.md", "t")], &[]) + .unwrap() + .with_gitignore(true), + )]) + .unwrap(); + (dir, root, watcher) +} + +fn wrote_in_docs_but_not(watcher: &Watcher, root: &std::path::Path, ignored: &std::path::Path) { + fs::write(ignored.join("a.md"), "x").unwrap(); + fs::write(root.join("docs").join("a.md"), "x").unwrap(); + let kept = root.join("docs").join("a.md"); + let events = collect_events_until(watcher, Duration::from_secs(5), |seen| { + seen.contains(&FileEvent::Created(kept.clone())) + }); + assert!( + events.contains(&FileEvent::Created(kept.clone())), + "saw: {events:?}" + ); + assert!( + !events.contains(&FileEvent::Created(ignored.join("a.md"))), + "saw: {events:?}" + ); +} + +#[cfg(not(any(target_os = "macos", target_os = "windows")))] +#[test] +fn scoped_watcher_puts_no_watch_on_a_gitignored_directory_a_glob_reaches() { + let (_dir, root, watcher) = gitignored_repo(&["build"]); + + wrote_in_docs_but_not(&watcher, &root, &root.join("build")); +} + +#[cfg(not(any(target_os = "macos", target_os = "windows")))] +#[test] +fn scoped_watcher_puts_no_watch_on_a_gitignored_directory_made_later() { + let (_dir, root, watcher) = gitignored_repo(&[]); + let build = root.join("build"); + fs::create_dir(&build).unwrap(); + collect_events_until(&watcher, Duration::from_secs(5), |seen| { + seen.contains(&FileEvent::Created(build.clone())) + }); + + wrote_in_docs_but_not(&watcher, &root, &build); +} + #[test] fn scoped_watcher_reports_files_in_a_new_directory_a_table_glob_reaches() { let dir = TempDir::new().unwrap(); From e931cf9991fee3145b4f59a2e7fbbec816ef4640 Mon Sep 17 00:00:00 2001 From: thekevinbot <248217931+thekevinbot@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:23:52 -0500 Subject: [PATCH 2/4] Skip gitignored directories in the watch walk and re-walk on a .gitignore change The watch walk now applies the startup scan's gitignore rules, including the exemption for paths a glob spells out. A .gitignore written, created or deleted while watching re-walks its directory, watches the directories it un-ignores and indexes their files. --- .../2026-11-18-gitignored-watch.md | 1 + .../2026-11-18-gitignored-watch.md | 21 +++ packages/rust/src/inotify_watch.rs | 64 +++++++- packages/rust/src/notify_watch.rs | 27 +++- packages/rust/src/scanner.rs | 147 +++++++++++++++++- packages/rust/src/watcher.rs | 107 ++++++++++++- 6 files changed, 358 insertions(+), 9 deletions(-) create mode 100644 packages/rust/changelog.d/2026-11-18-gitignored-watch.md create mode 100644 packages/rust/migrations.d/2026-11-18-gitignored-watch.md diff --git a/packages/rust/changelog.d/2026-11-18-gitignored-watch.md b/packages/rust/changelog.d/2026-11-18-gitignored-watch.md new file mode 100644 index 00000000..9ef4bb83 --- /dev/null +++ b/packages/rust/changelog.d/2026-11-18-gitignored-watch.md @@ -0,0 +1 @@ +**Fixed** The live watcher no longer watches a directory `.gitignore` hides, matching the startup scan: a gitignored directory a table glob reaches, and one created there later, costs no watch, unless a table glob spells its path. When a `.gitignore` is written or deleted while watching, the directories it un-ignores are watched, and on Linux the files already in them are indexed. diff --git a/packages/rust/migrations.d/2026-11-18-gitignored-watch.md b/packages/rust/migrations.d/2026-11-18-gitignored-watch.md new file mode 100644 index 00000000..c7f66271 --- /dev/null +++ b/packages/rust/migrations.d/2026-11-18-gitignored-watch.md @@ -0,0 +1,21 @@ +### The watcher skips gitignored directories + +#### Summary + +The live watcher now skips the directories `.gitignore` hides, as the startup scan does, and re-walks from a `.gitignore` that changes while watching so the directories it un-ignores are watched and indexed. Nothing breaks and no code changes are required. + +#### Required changes + +_None._ + +#### Deprecations removed + +_None._ + +#### Behavior changes without code changes + +On Linux and the BSDs, a gitignored directory a table glob reaches costs no watch, and one a `.gitignore` edit un-ignores while watching gets one. On Linux, that edit also indexes the files already in the directory; before, only files written there afterward were picked up. Elsewhere, files already in an un-ignored directory are indexed at the next build. + +#### Verification + +On Linux, in a git repo whose `.gitignore` lists `build/`, run `dirsql --watch` with one table globbing `**/*.md`, then count the watches: `grep -c '^inotify wd:' /proc//fdinfo/` counts none under `build/`. diff --git a/packages/rust/src/inotify_watch.rs b/packages/rust/src/inotify_watch.rs index 4bb6dc53..ce3d95e6 100644 --- a/packages/rust/src/inotify_watch.rs +++ b/packages/rust/src/inotify_watch.rs @@ -1,7 +1,8 @@ #[cfg(test)] use crate::watcher::WatchFault; use crate::watcher::{ - FileEvent, WatchScope, tolerate_missing, walk_scope, watch_new_tree, watch_tree, + FileEvent, WatchScope, gitignore_dir, tolerate_missing, walk_scope, watch_new_tree, watch_tree, + watch_unignored, }; use inotify::{EventMask, Inotify, WatchDescriptor, WatchMask}; use std::collections::{HashMap, HashSet, VecDeque}; @@ -191,7 +192,11 @@ impl Source for Reader { let _ = watches.clone().remove(wd); }, ); + let unignored = dirs.unignored(scopes, &walk_scope, found.as_ref(), &mut |dir| { + add_watch_with(&watches, dir) + }); queued.extend(found); + queued.extend(unignored); } } } @@ -310,6 +315,27 @@ impl Dirs { } } + /// After `event`, the directories a `.gitignore` it names brought into the + /// walk, watched and reported as created. + fn unignored( + &mut self, + scopes: &[WatchScope], + walk: &dyn Fn(&WatchScope, &Path) -> Vec, + event: Option<&FileEvent>, + add: &mut dyn FnMut(&Path) -> Result, + ) -> Vec { + let Some(FileEvent::Created(path) | FileEvent::Deleted(path)) = event else { + return Vec::new(); + }; + if gitignore_dir(path).is_none() { + return Vec::new(); + } + let watched = self.paths.values().cloned().collect(); + watch_unignored(scopes, path, walk, &watched, &mut |dir| { + self.watch(dir, add) + }) + } + /// The event one inotify event amounts to, after watching any directory /// it brings into the tree and unwatching any it takes out. #[allow(clippy::too_many_arguments)] @@ -652,6 +678,42 @@ mod tests { ); } + fn unignored(event: Option) -> (Vec, Dirs) { + let mut dirs = watching(&[(1, "/r")]); + let mut next = 100; + let events = dirs.unignored( + &[everything("/r")], + &|_, dir| vec![dir.to_path_buf(), dir.join("build")], + event.as_ref(), + &mut |_| { + next += 1; + Ok(next) + }, + ); + (events, dirs) + } + + #[test] + fn unignored_watches_and_reports_a_directory_a_changed_gitignore_brings_in() { + for event in [ + FileEvent::Created(PathBuf::from("/r/.gitignore")), + FileEvent::Deleted(PathBuf::from("/r/.gitignore")), + ] { + let (events, dirs) = unignored(Some(event)); + assert_eq!(events, vec![FileEvent::Created(PathBuf::from("/r/build"))]); + assert_eq!(dirs.paths.get(&101), Some(&PathBuf::from("/r/build"))); + } + } + + #[test] + fn unignored_does_nothing_without_a_created_or_deleted_path() { + for event in [None, Some(FileEvent::Rescan)] { + let (events, dirs) = unignored(event); + assert!(events.is_empty()); + assert_eq!(dirs.paths.len(), 1); + } + } + #[test] fn dirs_watch_records_nothing_when_the_watch_fails() { let mut dirs = watching(&[]); diff --git a/packages/rust/src/notify_watch.rs b/packages/rust/src/notify_watch.rs index b79e74bc..ded30a41 100644 --- a/packages/rust/src/notify_watch.rs +++ b/packages/rust/src/notify_watch.rs @@ -1,6 +1,7 @@ use crate::matcher::TableMatcher; use crate::watcher::{ - FileEvent, WatchScope, missing_root, tolerate_missing, walk_scope, watch_new_tree, watch_tree, + FileEvent, WatchScope, gitignore_dir, missing_root, tolerate_missing, walk_scope, + watch_new_tree, watch_tree, }; use notify::event::{ModifyKind, RenameMode}; use notify::{ @@ -169,6 +170,21 @@ fn forward( return; } } + if let FileEvent::Created(path) | FileEvent::Modified(path) | FileEvent::Deleted(path) = + &fe + && let Some(dir) = gitignore_dir(path) + { + let Some(watcher) = watcher.upgrade() else { + return; + }; + let mut watcher = watcher.lock().expect("watching a directory does not panic"); + if let Err(fault) = watch_new_tree(scopes, dir, &walk_scope, &mut |dir| { + watcher.watch(dir, RecursiveMode::NonRecursive) + }) && tx.send(FileEvent::Fault(fault)).is_err() + { + return; + } + } if fe == FileEvent::Rescan && PER_DIRECTORY { let Some(watcher) = watcher.upgrade() else { return; @@ -383,6 +399,15 @@ mod tests { assert!(got.is_empty()); } + #[test] + fn forward_stops_at_a_gitignore_change_once_the_watcher_is_gone() { + let got = forwarded(vec![ + modify_event(vec![PathBuf::from("/nowhere/.gitignore")]), + modify_event(vec![PathBuf::from("/nowhere/a")]), + ]); + assert!(got.is_empty()); + } + #[test] fn translate_event_maps_create() { let results = translate_event(&create_event(vec![PathBuf::from("/tmp/test.txt")])); diff --git a/packages/rust/src/scanner.rs b/packages/rust/src/scanner.rs index 75c98e5e..488eca04 100644 --- a/packages/rust/src/scanner.rs +++ b/packages/rust/src/scanner.rs @@ -150,12 +150,18 @@ pub(crate) fn scan_dirs_linked( entered.push(path.to_path_buf()); } }; + let gitignore = ignore.respects_gitignore(); + let repo = if gitignore { + enclosing_repo(start, &holds_git) + } else { + None + }; let walker = Walk { ignore, glob: dirs_glob(ignore), - gitignore: false, - in_repo: false, - frames: Vec::new(), + gitignore, + in_repo: repo.is_some(), + frames: repo.map_or_else(Vec::new, |top| gitignores_above(start, top, &Frame::load)), dirs: true, on_link: &on_link, }; @@ -1166,7 +1172,7 @@ fn should_descend(is_dir: bool, rel_path: &Path, ignore: &TableMatcher) -> bool /// Whether a walk from the root enters the directory at `rel_path`: every /// directory on the way, itself included, survives [`should_descend`], and a /// table glob can still reach it. -pub(crate) fn reaches_dir(rel_path: &Path, ignore: &TableMatcher) -> bool { +fn reaches_dir(rel_path: &Path, ignore: &TableMatcher) -> bool { let mut prefix = PathBuf::new(); rel_path.components().all(|component| { prefix.push(component); @@ -1174,6 +1180,43 @@ pub(crate) fn reaches_dir(rel_path: &Path, ignore: &TableMatcher) -> bool { }) && dirs_glob(ignore).is_none_or(|glob| glob_reaches_dir(glob, rel_path)) } +/// Whether a walk from `root` enters `dir`: [`reaches_dir`], and, under a +/// matcher honoring `.gitignore`, no directory on the way is one the +/// `.gitignore` files in force there ignore and no table glob spells. +pub(crate) fn enters_dir(root: &Path, dir: &Path, ignore: &TableMatcher) -> bool { + let Ok(rel_path) = dir.strip_prefix(root) else { + return false; + }; + reaches_dir(rel_path, ignore) + && !(ignore.respects_gitignore() + && gitignore_hides(rel_path, dirs_glob(ignore), &|prefix| { + is_gitignored_dir(&root.join(prefix)) + })) +} + +/// Whether some directory on the way to `rel_path`, itself included, is one +/// `is_ignored` reports and `glob` does not spell. +fn gitignore_hides( + rel_path: &Path, + glob: Option<&PathGlob>, + is_ignored: &dyn Fn(&Path) -> bool, +) -> bool { + let mut prefix = PathBuf::new(); + rel_path.components().any(|component| { + prefix.push(component); + !glob.is_some_and(|glob| glob.names(&prefix)) && is_ignored(&prefix) + }) +} + +/// Whether the `.gitignore` files in force in the directory holding `dir` +/// ignore it, as the walk judges an entry: only `dir` itself is matched, its +/// parents having been judged on the way. +fn is_gitignored_dir(dir: &Path) -> bool { + dir.parent() + .and_then(|parent| enclosing_repo(parent, &holds_git)) + .is_some_and(|top| is_gitignored(&gitignores_above(dir, top, &Frame::load), dir, true)) +} + /// Whether some component of `glob` can enter the directory at `rel_path`. fn glob_reaches_dir(glob: &PathGlob, rel_path: &Path) -> bool { let states = rel_path.components().fold(glob.start(), |states, c| { @@ -1711,6 +1754,102 @@ mod tests { assert!(!reaches_dir(Path::new("apps/node_modules/pkg"), &ignore)); } + #[test] + fn gitignore_hides_a_directory_below_an_ignored_one_on_the_way() { + let is_ignored = |prefix: &Path| prefix == Path::new("build"); + assert!(gitignore_hides(Path::new("build"), None, &is_ignored)); + assert!(gitignore_hides(Path::new("build/sub"), None, &is_ignored)); + assert!(!gitignore_hides(Path::new("docs/build"), None, &is_ignored)); + assert!(!gitignore_hides(Path::new(""), None, &is_ignored)); + } + + #[test] + fn gitignore_hides_no_directory_a_glob_spells() { + let glob = compile_glob("build/x.md").unwrap(); + let is_ignored = |prefix: &Path| prefix.starts_with("build"); + assert!(!gitignore_hides( + Path::new("build"), + Some(&glob), + &is_ignored + )); + assert!(gitignore_hides( + Path::new("build/sub"), + Some(&glob), + &is_ignored + )); + } + + fn repo_ignoring_build() -> tempfile::TempDir { + let root = tempfile::tempdir().unwrap(); + fs::create_dir(root.path().join(".git")).unwrap(); + fs::write(root.path().join(".gitignore"), "build/\n").unwrap(); + root + } + + #[test] + fn enters_dir_skips_a_gitignored_directory_and_below_it() { + let root = repo_ignoring_build(); + let matcher = TableMatcher::new(&[("**/*.md", "t")], &[]) + .unwrap() + .with_gitignore(true); + assert!(enters_dir( + root.path(), + &root.path().join("docs/sub"), + &matcher + )); + assert!(!enters_dir( + root.path(), + &root.path().join("build"), + &matcher + )); + assert!(!enters_dir( + root.path(), + &root.path().join("build/sub"), + &matcher + )); + } + + #[test] + fn enters_dir_enters_a_gitignored_directory_a_glob_spells() { + let root = repo_ignoring_build(); + let matcher = TableMatcher::new(&[("build/x.md", "t")], &[]) + .unwrap() + .with_gitignore(true); + assert!(enters_dir( + root.path(), + &root.path().join("build"), + &matcher + )); + } + + #[test] + fn enters_dir_enters_a_gitignored_directory_when_gitignore_is_off() { + let root = repo_ignoring_build(); + let matcher = TableMatcher::new(&[("**/*.md", "t")], &[]).unwrap(); + assert!(enters_dir( + root.path(), + &root.path().join("build"), + &matcher + )); + } + + #[test] + fn enters_dir_is_false_outside_the_root() { + let matcher = TableMatcher::new(&[], &[]).unwrap(); + assert!(!enters_dir( + Path::new("/r"), + Path::new("/elsewhere"), + &matcher + )); + } + + #[test] + fn is_gitignored_dir_is_false_outside_any_repo() { + let root = tempfile::tempdir().unwrap(); + fs::write(root.path().join(".gitignore"), "build/\n").unwrap(); + assert!(!is_gitignored_dir(&root.path().join("build"))); + } + #[test] fn is_glob_match_accepts_a_matching_relative_path() { let set = compile_glob("docs/**/*.md").unwrap(); diff --git a/packages/rust/src/watcher.rs b/packages/rust/src/watcher.rs index 86542320..db79db03 100644 --- a/packages/rust/src/watcher.rs +++ b/packages/rust/src/watcher.rs @@ -1,5 +1,5 @@ use crate::matcher::TableMatcher; -use crate::scanner::{reaches_dir, scan_dirs_linked}; +use crate::scanner::{enters_dir, scan_dirs_linked}; use notify::ErrorKind; use std::collections::HashSet; use std::path::{Path, PathBuf}; @@ -86,8 +86,7 @@ impl WatchScope { } fn entered(&self, dir: &Path) -> Option { - let rel = dir.strip_prefix(&self.root).ok()?; - reaches_dir(rel, &self.ignore).then(|| dir.to_path_buf()) + enters_dir(&self.root, dir, &self.ignore).then(|| dir.to_path_buf()) } } @@ -130,6 +129,46 @@ pub(crate) fn watch_tree( } } +/// After an event at `path`, when it names a `.gitignore`: watch each +/// directory the walk from its directory now enters that `watched` lacks, and +/// report the outermost of those as created, so the files a `.gitignore` edit +/// un-ignored are indexed the way a new directory's are. +pub(crate) fn watch_unignored( + scopes: &[WatchScope], + path: &Path, + walk: &dyn Fn(&WatchScope, &Path) -> Vec, + watched: &HashSet, + watch: &mut dyn FnMut(&Path) -> Result<(), notify::Error>, +) -> Vec { + let Some(dir) = gitignore_dir(path) else { + return Vec::new(); + }; + let mut now = watched.clone(); + if let Err(e) = watch_tree(scopes, dir, walk, &mut now, &mut |dir| { + tolerate_missing(watch(dir)) + }) { + return vec![FileEvent::Fault(WatchFault::of(&e))]; + } + let fresh: HashSet<&PathBuf> = now.difference(watched).collect(); + let mut outermost: Vec = fresh + .iter() + .filter(|dir| { + dir.parent() + .is_none_or(|parent| !fresh.contains(&parent.to_path_buf())) + }) + .map(|dir| (*dir).clone()) + .collect(); + outermost.sort(); + outermost.into_iter().map(FileEvent::Created).collect() +} + +/// The directory holding `path` when `path` is a `.gitignore`, whose change +/// can bring directories below it into the walk. +pub(crate) fn gitignore_dir(path: &Path) -> Option<&Path> { + path.parent() + .filter(|_| path.file_name() == Some(std::ffi::OsStr::new(".gitignore"))) +} + pub(crate) fn walk_scope(scope: &WatchScope, dir: &Path) -> Vec { let (dirs, links) = scan_dirs_linked(&scope.root, dir, &scope.ignore); scope.remember(links); @@ -301,6 +340,68 @@ mod tests { ); } + #[test] + fn gitignore_dir_is_the_directory_of_a_gitignore_only() { + assert_eq!( + gitignore_dir(Path::new("/r/a/.gitignore")), + Some(Path::new("/r/a")) + ); + assert_eq!(gitignore_dir(Path::new("/r/a/x.md")), None); + } + + fn unignored( + path: &str, + watch: &mut dyn FnMut(&Path) -> Result<(), notify::Error>, + ) -> Vec { + let watched = HashSet::from([PathBuf::from("/r"), PathBuf::from("/r/kept")]); + watch_unignored( + &[scope("/r", &[])], + Path::new(path), + &|_, dir| { + ["", "kept", "build", "build/sub"] + .iter() + .map(|sub| dir.join(sub)) + .map(|dir| dir.components().collect()) + .collect() + }, + &watched, + watch, + ) + } + + #[test] + fn watch_unignored_watches_what_a_gitignore_change_brings_in_and_reports_the_outermost() { + let mut seen = Vec::new(); + let events = unignored("/r/.gitignore", &mut |dir| { + seen.push(dir.to_path_buf()); + Ok(()) + }); + assert_eq!(events, vec![FileEvent::Created(PathBuf::from("/r/build"))]); + seen.sort(); + assert_eq!( + seen, + vec![PathBuf::from("/r/build"), PathBuf::from("/r/build/sub")] + ); + } + + #[test] + fn watch_unignored_does_nothing_for_another_file() { + let events = unignored("/r/x.md", &mut |dir| panic!("watched {dir:?}")); + assert!(events.is_empty()); + } + + #[test] + fn watch_unignored_reports_a_failed_watch() { + let events = unignored("/r/.gitignore", &mut |_| Err(max_files_watch_error())); + assert_eq!(events, vec![FileEvent::Fault(WatchFault::Limit)]); + } + + #[test] + fn watch_unignored_tolerates_a_directory_gone_before_its_watch() { + let events = unignored("/r/.gitignore", &mut |_| Err(path_not_found_error())); + assert_eq!(events, vec![FileEvent::Created(PathBuf::from("/r/build"))]); + } + fn unwalked(_: &WatchScope, _: &Path) -> Vec { panic!("a directory the scan skips is never walked") } From 70642b3f2fcac42e8e4fd850fe91718c5efef486 Mon Sep 17 00:00:00 2001 From: thekevinbot <248217931+thekevinbot@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:29:55 -0500 Subject: [PATCH 3/4] e2e attestation for bot/1768-gitignored-watch-1118 --- .../e2e-attestations/bot-1768-gitignored-watch-1118.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 packages/ts/e2e-attestations/bot-1768-gitignored-watch-1118.json diff --git a/packages/ts/e2e-attestations/bot-1768-gitignored-watch-1118.json b/packages/ts/e2e-attestations/bot-1768-gitignored-watch-1118.json new file mode 100644 index 00000000..8353856e --- /dev/null +++ b/packages/ts/e2e-attestations/bot-1768-gitignored-watch-1118.json @@ -0,0 +1,7 @@ +{ + "command": "pnpm test:e2e", + "ran_at": 1795055395, + "exit_code": 0, + "commit": "e931cf9991fee3145b4f59a2e7fbbec816ef4640", + "branch": "bot/1768-gitignored-watch-1118" +} From 3be4ef90535ab5284cf051ab6023339e3972ab5e Mon Sep 17 00:00:00 2001 From: thekevinbot <248217931+thekevinbot@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:34:49 -0500 Subject: [PATCH 4/4] e2e attestation for bot/1768-gitignored-watch-1118 --- .../e2e-attestations/bot-1768-gitignored-watch-1118.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 packages/python/e2e-attestations/bot-1768-gitignored-watch-1118.json diff --git a/packages/python/e2e-attestations/bot-1768-gitignored-watch-1118.json b/packages/python/e2e-attestations/bot-1768-gitignored-watch-1118.json new file mode 100644 index 00000000..29e80d1b --- /dev/null +++ b/packages/python/e2e-attestations/bot-1768-gitignored-watch-1118.json @@ -0,0 +1,7 @@ +{ + "command": "uv run python -m pytest tests/e2e/watch_fault_test.py tests/e2e/fanout_test.py tests/e2e/fresh_declared_repros_test.py tests/e2e/glob_speed_gitignore_test.py tests/e2e/watch_speed_test.py -x -q", + "ran_at": 1795055689, + "exit_code": 0, + "commit": "70642b3f2fcac42e8e4fd850fe91718c5efef486", + "branch": "bot/1768-gitignored-watch-1118" +}