M1.2: native file watching via notify

- notify 8.2.0 (CC0-1.0) behind the existing Watcher API, allowed by a
  crate-scoped [[licenses.exceptions]] entry in deny.toml (owner
  decision 2026-10-14, noted in AGENTS.md and NOTICE).
- Native notifications only mark directories dirty; after 100 ms of
  quiet (at most 10 intervals) dirty dirs are rescanned and diffed, so
  events, debouncing and rename pairing match the polling backend.
- Polling stays as fallback when the native watcher cannot start, and
  per directory when a native watch is refused. Lost events or watcher
  errors trigger a full rescan; root loss is checked on every tick.
- Additive API: WatchOptions::backend (Auto, Polling) and
  Watcher::is_native.
- Watch tests run on both backends (34 vfs tests on Windows).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-14 19:43:15 +02:00
parent 5d49b57b9c
commit 3f190a635e
11 changed files with 500 additions and 86 deletions

View file

@ -1,4 +1,5 @@
//! The polling watcher. Every wait is bounded by a timeout on the channel.
//! The watcher, run once per backend (native and polling). Every wait is
//! bounded by a timeout on the channel.
#![allow(
clippy::unwrap_used,
@ -15,7 +16,7 @@ use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::time::{Duration, Instant};
use common::{open, tree};
use lutra_vfs::{EntryKind, VfsEvent, WatchOptions};
use lutra_vfs::{EntryKind, Project, VfsEvent, WatchBackend, WatchOptions, Watcher};
const TIMEOUT: Duration = Duration::from_secs(10);
@ -35,15 +36,53 @@ fn wait_for(rx: &Receiver<VfsEvent>, want: impl Fn(&VfsEvent) -> bool) -> Vec<Vf
}
}
/// Starts a watcher with `backend`, checking that the native backend
/// really started on this machine.
fn start(
project: &Project,
backend: WatchBackend,
interval: Duration,
) -> (Watcher, Receiver<VfsEvent>) {
let (watcher, rx) = project
.watch_with(WatchOptions { interval, backend })
.unwrap();
assert_eq!(watcher.is_native(), backend == WatchBackend::Auto);
(watcher, rx)
}
fn watch(project: &Project, backend: WatchBackend) -> (Watcher, Receiver<VfsEvent>) {
start(project, backend, WatchOptions::default().interval)
}
/// Runs each listed test body once per backend.
macro_rules! both_backends {
($($name:ident),* $(,)?) => {
mod native {
$(#[test] fn $name() { super::$name(super::WatchBackend::Auto) })*
}
mod polling {
$(#[test] fn $name() { super::$name(super::WatchBackend::Polling) })*
}
};
}
both_backends!(
reports_create_modify_rename_and_delete,
reports_directories_and_watched_subdirectories,
ignored_hidden_and_excluded_directories_are_not_watched,
changes_within_one_interval_are_coalesced,
losing_the_root_degrades_to_an_error_event,
dropping_the_watcher_closes_the_channel,
);
fn is_created(path: &Path) -> impl Fn(&VfsEvent) -> bool + '_ {
move |e| matches!(e, VfsEvent::Created { path: p, .. } if p == path)
}
#[test]
fn reports_create_modify_rename_and_delete() {
fn reports_create_modify_rename_and_delete(backend: WatchBackend) {
let dir = tree(&[("keep.php", "")]);
let project = open(dir.path());
let (_watcher, rx) = project.watch().unwrap();
let (_watcher, rx) = watch(&project, backend);
fs::write(dir.path().join("new.php"), "a").unwrap();
wait_for(&rx, is_created(Path::new("new.php")));
@ -69,11 +108,10 @@ fn reports_create_modify_rename_and_delete() {
);
}
#[test]
fn reports_directories_and_watched_subdirectories() {
fn reports_directories_and_watched_subdirectories(backend: WatchBackend) {
let dir = tree(&[]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
let (watcher, rx) = watch(&project, backend);
fs::create_dir(dir.path().join("src")).unwrap();
wait_for(&rx, |e| {
*e == VfsEvent::Created {
@ -93,8 +131,7 @@ fn reports_directories_and_watched_subdirectories() {
assert_eq!(watcher.watched_dirs(), [PathBuf::new()]);
}
#[test]
fn ignored_hidden_and_excluded_directories_are_not_watched() {
fn ignored_hidden_and_excluded_directories_are_not_watched(backend: WatchBackend) {
let dir = tree(&[
(".gitignore", "target/\n"),
("target/", ""),
@ -103,7 +140,7 @@ fn ignored_hidden_and_excluded_directories_are_not_watched() {
("src/", ""),
]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
let (watcher, rx) = watch(&project, backend);
assert!(!watcher.watch_dir("target").unwrap());
assert!(!watcher.watch_dir("node_modules").unwrap());
assert!(!watcher.watch_dir(".git").unwrap());
@ -124,31 +161,26 @@ fn ignored_hidden_and_excluded_directories_are_not_watched() {
}
}
#[test]
fn changes_within_one_interval_are_coalesced() {
fn changes_within_one_interval_are_coalesced(backend: WatchBackend) {
let dir = tree(&[]);
let project = open(dir.path());
let (_watcher, rx) = project
.watch_with(WatchOptions {
interval: Duration::from_secs(2),
})
.unwrap();
let (_watcher, rx) = start(&project, backend, Duration::from_secs(2));
let start = Instant::now();
fs::write(dir.path().join("flash.tmp"), "x").unwrap();
fs::remove_file(dir.path().join("flash.tmp")).unwrap();
fs::write(dir.path().join("marker.php"), "x").unwrap();
let before = wait_for(&rx, is_created(Path::new("marker.php")));
if start.elapsed() < Duration::from_secs(2) {
let polled_once_at_most = start.elapsed() < Duration::from_secs(2);
if backend == WatchBackend::Auto || polled_once_at_most {
assert!(before.is_empty(), "flash.tmp leaked: {before:?}");
}
}
#[test]
fn losing_the_root_degrades_to_an_error_event() {
fn losing_the_root_degrades_to_an_error_event(backend: WatchBackend) {
let parent = tree(&[("project/a.php", "")]);
let root = parent.path().join("project");
let project = open(&root);
let (_watcher, rx) = project.watch().unwrap();
let (_watcher, rx) = watch(&project, backend);
fs::remove_dir_all(&root).unwrap();
wait_for(&rx, |e| matches!(e, VfsEvent::Failed(_)));
let deadline = Instant::now() + TIMEOUT;
@ -161,11 +193,10 @@ fn losing_the_root_degrades_to_an_error_event() {
}
}
#[test]
fn dropping_the_watcher_closes_the_channel() {
fn dropping_the_watcher_closes_the_channel(backend: WatchBackend) {
let dir = tree(&[]);
let project = open(dir.path());
let (watcher, rx) = project.watch().unwrap();
let (watcher, rx) = watch(&project, backend);
drop(watcher);
assert_eq!(
rx.recv_timeout(TIMEOUT),