use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc::RecvTimeoutError;
use std::time::{Duration, Instant};
use notify::RecursiveMode;
use notify_debouncer_full::new_debouncer;
use crate::classify::is_dependency_dir;
use crate::delete::{Guard, Removal};
use crate::error::{Error, Result};
use crate::report::RunResult;
use crate::run::{RunOptions, run};
#[derive(Debug, Clone)]
pub struct WatchOptions {
pub debounce: Duration,
pub quiet_period: Duration,
}
impl Default for WatchOptions {
fn default() -> Self {
Self {
debounce: Duration::from_secs(5),
quiet_period: Duration::from_secs(60),
}
}
}
#[derive(Debug, Default)]
pub struct QuietTracker {
touched: HashMap<PathBuf, Instant>,
}
impl QuietTracker {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn touch(&mut self, path: &Path, at: Instant) {
let mut current = Some(path);
while let Some(dir) = current {
self.touched.insert(dir.to_path_buf(), at);
current = dir.parent();
}
}
#[must_use]
pub fn is_quiet(&self, path: &Path, quiet_period: Duration, now: Instant) -> bool {
let Some(last) = self.newest_under(path) else {
return true;
};
now.duration_since(last) >= quiet_period
}
fn newest_under(&self, path: &Path) -> Option<Instant> {
self.touched
.iter()
.filter(|(touched, _)| touched.starts_with(path))
.map(|(_, at)| *at)
.max()
}
pub fn forget(&mut self, path: &Path) {
self.touched.retain(|touched, _| !touched.starts_with(path));
}
#[must_use]
pub fn len(&self) -> usize {
self.touched.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.touched.is_empty()
}
pub fn prune(&mut self, quiet_period: Duration, now: Instant) {
self.touched.retain(|_, at| now.duration_since(*at) < quiet_period);
}
}
#[must_use]
pub fn should_watch(path: &Path) -> bool {
let Some(name) = path.file_name() else { return true };
let vcs = crate::scan::NEVER_DESCEND
.iter()
.any(|never| name == std::ffi::OsStr::new(never));
!(is_dependency_dir(name) || vcs)
}
fn seed_baseline(options: &RunOptions, tracker: &mut QuietTracker) -> Result<()> {
let baseline = run(&RunOptions {
dry_run: true,
..options.clone()
})?;
let started = Instant::now();
for entry in &baseline.entries {
tracker.touch(&entry.path, started);
}
Ok(())
}
fn register_roots(roots: &[PathBuf], mut watch: impl FnMut(&Path) -> notify::Result<()>) -> Result<()> {
let mut unwatched = Vec::new();
for root in roots {
if let Err(error) = watch(root) {
unwatched.push(format!("{}: {error}", root.display()));
}
}
if !unwatched.is_empty() {
return Err(Error::Watch {
reason: format!("could not watch {}", unwatched.join("; ")),
});
}
Ok(())
}
fn remove_quiet(
candidates: &mut RunResult,
roots: &[PathBuf],
options: &RunOptions,
tracker: &mut QuietTracker,
) -> Result<()> {
let guard = Guard::new(roots, options.one_file_system)?;
let removal = Removal {
dry_run: options.dry_run,
force: false,
};
for entry in &mut candidates.entries {
entry.outcome = guard.remove(&entry.path, entry.bytes, removal);
if entry.outcome.is_reclaimed() {
tracker.forget(&entry.path);
}
}
Ok(())
}
pub fn watch(
options: &RunOptions,
watch_options: &WatchOptions,
mut on_prune: impl FnMut(&RunResult) -> io::Result<()>,
) -> Result<()> {
let roots = options.roots.clone();
let mut tracker = QuietTracker::new();
seed_baseline(options, &mut tracker)?;
let (sender, receiver) = std::sync::mpsc::channel();
let mut debouncer = new_debouncer(watch_options.debounce, None, sender).map_err(|error| Error::Watch {
reason: error.to_string(),
})?;
register_roots(&roots, |root| debouncer.watch(root, RecursiveMode::Recursive))?;
let tick = (watch_options.quiet_period / 2).max(Duration::from_secs(1));
loop {
match receiver.recv_timeout(tick) {
Ok(Ok(events)) => {
let now = Instant::now();
for path in events.iter().flat_map(|event| &event.paths) {
if should_watch(path) {
tracker.touch(path, now);
}
}
}
Ok(Err(errors)) => {
if let Some(error) = errors.into_iter().next() {
return Err(Error::Watch {
reason: error.to_string(),
});
}
}
Err(RecvTimeoutError::Timeout) => {
if tracker.is_empty() {
continue;
}
}
Err(RecvTimeoutError::Disconnected) => break,
}
let now = Instant::now();
let mut candidates = run(&RunOptions {
dry_run: true,
..options.clone()
})?;
candidates.entries.retain(|entry| {
entry.outcome.is_reclaimed() && tracker.is_quiet(&entry.path, watch_options.quiet_period, now)
});
if candidates.entries.is_empty() {
tracker.prune(watch_options.quiet_period, now);
continue;
}
remove_quiet(&mut candidates, &roots, options, &mut tracker)?;
candidates.dry_run = options.dry_run;
candidates.skips.clear();
candidates.skipped_count = 0;
on_prune(&candidates).map_err(|error| Error::Watch {
reason: error.to_string(),
})?;
tracker.prune(watch_options.quiet_period, now);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn at(base: Instant, seconds: u64) -> Instant {
base + Duration::from_secs(seconds)
}
#[test]
fn should_hold_a_subtree_that_was_touched_recently() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
let quiet = Duration::from_secs(60);
let target = Path::new("/project/target");
tracker.touch(Path::new("/project/target/debug/app"), base);
assert!(
!tracker.is_quiet(target, quiet, at(base, 10)),
"a build from 10s ago holds it"
);
assert!(
!tracker.is_quiet(target, quiet, at(base, 59)),
"still within the window"
);
assert!(
tracker.is_quiet(target, quiet, at(base, 60)),
"released once the window passes"
);
}
#[test]
fn should_treat_activity_deep_inside_as_activity_on_the_artifact() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
tracker.touch(Path::new("/project/target/debug/incremental/foo/bar.o"), base);
assert!(!tracker.is_quiet(Path::new("/project/target"), Duration::from_secs(60), at(base, 5)));
}
#[test]
fn should_never_release_a_subtree_while_it_is_being_written_to() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
let quiet = Duration::from_secs(60);
let target = Path::new("/project/target");
for second in 0..600 {
tracker.touch(Path::new("/project/target/debug/app"), at(base, second));
assert!(
!tracker.is_quiet(target, quiet, at(base, second)),
"an actively written subtree must never look idle"
);
}
}
#[test]
fn should_treat_an_untouched_subtree_as_quiet() {
let tracker = QuietTracker::new();
assert!(tracker.is_quiet(Path::new("/project/target"), Duration::from_secs(60), Instant::now()));
}
#[test]
fn should_not_let_one_project_hold_another() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
tracker.touch(Path::new("/a/target/debug/app"), base);
assert!(!tracker.is_quiet(Path::new("/a/target"), Duration::from_secs(60), at(base, 1)));
assert!(tracker.is_quiet(Path::new("/b/target"), Duration::from_secs(60), at(base, 1)));
}
#[test]
fn should_forget_a_subtree_once_it_has_been_removed() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
tracker.touch(Path::new("/project/target/debug/app"), base);
assert!(!tracker.is_empty());
tracker.forget(Path::new("/project/target"));
assert!(tracker.is_quiet(Path::new("/project/target"), Duration::from_secs(60), at(base, 1)));
}
#[test]
fn should_prune_entries_that_can_no_longer_change_an_answer() {
let base = Instant::now();
let mut tracker = QuietTracker::new();
for index in 0..100 {
tracker.touch(&PathBuf::from(format!("/project/{index}/file")), base);
}
assert!(tracker.len() > 100);
tracker.prune(Duration::from_secs(60), at(base, 120));
assert_eq!(
tracker.len(),
0,
"entries older than the quiet period cannot hold anything"
);
}
#[test]
fn should_not_watch_dependency_caches_or_vcs_directories() {
assert!(!should_watch(Path::new("/project/node_modules")));
assert!(!should_watch(Path::new("/project/vendor")));
assert!(!should_watch(Path::new("/project/.git")));
assert!(should_watch(Path::new("/project/src")));
assert!(should_watch(Path::new("/project/target")));
}
#[test]
fn default_windows_match_the_documented_ones() {
let options = WatchOptions::default();
assert_eq!(options.debounce, Duration::from_secs(5));
assert_eq!(options.quiet_period, Duration::from_secs(60));
}
#[test]
fn should_let_a_write_inside_a_dependency_directory_hold_its_ancestors() {
let deep = Path::new("/projects/app/node_modules/left-pad/index.js");
assert!(
should_watch(deep),
"a write inside a dependency directory has to reach the tracker"
);
assert!(
!should_watch(Path::new("/projects/app/node_modules")),
"an event on the directory itself still does not"
);
let mut tracker = QuietTracker::default();
let now = Instant::now();
tracker.touch(deep, now);
assert!(
!tracker.is_quiet(Path::new("/projects/app/node_modules"), Duration::from_secs(5), now),
"and the dependency directory is held while the write is recent"
);
}
}