Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions packages/rust/changelog.d/2026-11-18-gitignored-watch.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions packages/rust/migrations.d/2026-11-18-gitignored-watch.md
Original file line number Diff line number Diff line change
@@ -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/<pid>/fdinfo/<inotify fd>` counts none under `build/`.
64 changes: 63 additions & 1 deletion packages/rust/src/inotify_watch.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -310,6 +315,27 @@ impl<W: Eq + Hash + Clone> Dirs<W> {
}
}

/// 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<PathBuf>,
event: Option<&FileEvent>,
add: &mut dyn FnMut(&Path) -> Result<W, notify::Error>,
) -> Vec<FileEvent> {
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)]
Expand Down Expand Up @@ -652,6 +678,42 @@ mod tests {
);
}

fn unignored(event: Option<FileEvent>) -> (Vec<FileEvent>, Dirs<u32>) {
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(&[]);
Expand Down
27 changes: 26 additions & 1 deletion packages/rust/src/notify_watch.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")]));
Expand Down
147 changes: 143 additions & 4 deletions packages/rust/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -1166,14 +1172,51 @@ 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);
should_descend(true, &prefix, ignore)
}) && 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| {
Expand Down Expand Up @@ -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();
Expand Down
Loading
Loading