use crate::guard::is_hidden;
use anyhow::Result;
use file_id::FileId;
use notify_debouncer_full::notify::ErrorKind as WatchError;
use notify_debouncer_full::notify::event::{AccessKind, AccessMode};
use notify_debouncer_full::notify::{
EventKind, RecommendedWatcher, RecursiveMode, event::ModifyKind,
};
use notify_debouncer_full::{
DebounceEventResult, DebouncedEvent, Debouncer, RecommendedCache, new_debouncer,
};
use std::path::{Component, Path, PathBuf};
use std::sync::mpsc::{self, Receiver};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tower_livereload::Reloader;
const DEBOUNCE_DELAY: Duration = Duration::from_millis(200);
const CHECK_INTERVAL: Duration = Duration::from_millis(100);
const SAME_REBUILD: Duration = Duration::from_millis(300);
const IGNORED_DIRS: &[&str] = &["target", "node_modules"];
const IGNORED_SUFFIXES: &[&str] = &["~", ".tmp", ".swp", ".swx", ".swo"];
const IGNORED_NAMES: &[&str] = &["4913"];
type Watcher = Debouncer<RecommendedWatcher, RecommendedCache>;
enum Rewatch {
Replaced,
WatchFailed,
}
pub(crate) fn start(root: &Path, reloader: Reloader) -> Result<()> {
let (failed, failures) = mpsc::channel();
let announced = Arc::new(Mutex::new(None::<Instant>));
let watched_root = root.to_path_buf();
let same_rebuild = Arc::clone(&announced);
let changed = reloader.clone();
let mut debouncer = new_debouncer(DEBOUNCE_DELAY, None, move |result: DebounceEventResult| {
match result {
Ok(events) => {
if events.iter().any(|event| is_change(&watched_root, event)) {
if !just_announced(&same_rebuild) {
println!(" File changed, reloading...");
}
changed.reload();
}
}
Err(errors) => {
for error in errors {
eprintln!(" Cannot watch for changes: {}", cannot_watch(&error));
}
let _ = failed.send(());
}
}
})
.map_err(|error| cannot_watch_here(root, &error))?;
let first_look = Watched::at(root);
debouncer
.watch(root, RecursiveMode::Recursive)
.map_err(|error| cannot_watch_here(root, &error))?;
let root = root.to_path_buf();
std::thread::spawn(move || {
supervise(debouncer, root, failures, first_look, announced, reloader)
});
Ok(())
}
fn supervise(
mut debouncer: Watcher,
root: PathBuf,
failures: Receiver<()>,
mut watched: Option<Watched>,
announced: Arc<Mutex<Option<Instant>>>,
reloader: Reloader,
) {
loop {
let reason = match failures.recv_timeout(CHECK_INTERVAL) {
Ok(()) => Rewatch::WatchFailed,
Err(mpsc::RecvTimeoutError::Disconnected) => return,
Err(mpsc::RecvTimeoutError::Timeout) => {
let Some(now) = directory_id(&root) else {
continue;
};
match watched.as_ref().map(|it| it.id) {
Some(before) if before != now => Rewatch::Replaced,
Some(_) => continue,
None => {
watched = watch_again(&mut debouncer, &root, &failures);
continue;
}
}
}
};
watched = watch_again(&mut debouncer, &root, &failures);
match reason {
Rewatch::Replaced => {
if let Ok(mut at) = announced.lock() {
*at = Some(Instant::now());
}
println!(" Directory replaced, reloading...");
}
Rewatch::WatchFailed => println!(" Watching again, reloading..."),
}
reloader.reload();
}
}
fn watch_again(debouncer: &mut Watcher, root: &Path, failures: &Receiver<()>) -> Option<Watched> {
loop {
if !root.is_dir() {
std::thread::sleep(CHECK_INTERVAL);
continue;
}
let _ = debouncer.unwatch(root);
while failures.try_recv().is_ok() {}
let looked_at = Watched::at(root);
if debouncer.watch(root, RecursiveMode::Recursive).is_ok() {
return looked_at;
}
std::thread::sleep(CHECK_INTERVAL);
}
}
fn is_change(root: &Path, event: &DebouncedEvent) -> bool {
let written = match event.kind {
EventKind::Create(_) | EventKind::Remove(_) | EventKind::Any => true,
EventKind::Modify(ModifyKind::Metadata(_)) => false,
EventKind::Modify(_) => true,
EventKind::Access(AccessKind::Close(AccessMode::Write)) => true,
EventKind::Access(_) | EventKind::Other => false,
};
written && event.paths.iter().any(|path| !is_ignored(root, path))
}
struct Watched {
#[cfg(unix)]
_open: Option<std::fs::File>,
id: FileId,
}
impl Watched {
#[cfg(unix)]
fn at(path: &Path) -> Option<Watched> {
let open = std::fs::File::open(path).ok();
Some(Watched {
_open: open,
id: directory_id(path)?,
})
}
#[cfg(not(unix))]
fn at(path: &Path) -> Option<Watched> {
Some(Watched {
id: directory_id(path)?,
})
}
}
fn directory_id(path: &Path) -> Option<FileId> {
file_id::get_file_id(path).ok()
}
fn just_announced(at: &Mutex<Option<Instant>>) -> bool {
let Ok(at) = at.lock() else {
return false;
};
at.is_some_and(|when| when.elapsed() < SAME_REBUILD)
}
fn is_ignored(root: &Path, path: &Path) -> bool {
let Ok(relative) = path.strip_prefix(root) else {
return false;
};
let unservable = relative.components().any(|component| match component {
Component::Normal(part) => {
let part = part.to_string_lossy();
is_hidden(&part) || IGNORED_DIRS.contains(&part.as_ref())
}
_ => false,
});
if unservable {
return true;
}
let Some(name) = relative.file_name().map(|name| name.to_string_lossy()) else {
return false;
};
IGNORED_SUFFIXES.iter().any(|suffix| name.ends_with(*suffix))
|| IGNORED_NAMES.iter().any(|ignored| name == *ignored)
|| (name.starts_with('#') && name.ends_with('#')) || name.contains("___jb_") }
fn cannot_watch(error: ¬ify_debouncer_full::notify::Error) -> String {
match &error.kind {
WatchError::MaxFilesWatch => "the system has no file watches left — serve the build \
output rather than the whole project, or raise the limit"
.to_string(),
WatchError::PathNotFound => "there is no such directory".to_string(),
WatchError::Io(io) => crate::errors::why(io),
_ => error.to_string(),
}
}
fn cannot_watch_here(root: &Path, error: ¬ify_debouncer_full::notify::Error) -> anyhow::Error {
anyhow::Error::msg(cannot_watch(error))
.context(format!("cannot watch {} for changes", root.display()))
}
#[cfg(test)]
mod tests {
use super::*;
use notify_debouncer_full::notify::Event;
use notify_debouncer_full::notify::event::{CreateKind, DataChange, MetadataKind, RemoveKind};
fn event(kind: EventKind, paths: &[&str]) -> DebouncedEvent {
let event = paths
.iter()
.fold(Event::new(kind), |event, path| event.add_path(path.into()));
DebouncedEvent::new(event, Instant::now())
}
fn written(paths: &[&str]) -> DebouncedEvent {
event(EventKind::Modify(ModifyKind::Data(DataChange::Any)), paths)
}
const ROOT: &str = "/site";
#[test]
fn build_directories_are_ignored() {
for path in [
"/site/.git/HEAD",
"/site/node_modules/left-pad/index.js",
"/site/target/debug/app",
] {
assert!(is_ignored(Path::new(ROOT), Path::new(path)), "{path}");
}
}
#[test]
fn editor_scratch_files_are_ignored() {
for path in [
"/site/index.html.swp",
"/site/index.html~",
"/site/draft.tmp",
"/site/.#index.html",
"/site/#index.html#",
"/site/index.html___jb_tmp___",
"/site/4913",
"/site/.DS_Store",
] {
assert!(is_ignored(Path::new(ROOT), Path::new(path)), "{path}");
}
}
#[test]
fn ordinary_files_are_not_ignored() {
for path in ["/site/index.html", "/site/assets/app.css", "/site/a~b/c.js"] {
assert!(!is_ignored(Path::new(ROOT), Path::new(path)), "{path}");
}
}
#[test]
fn only_the_part_below_the_root_is_judged() {
let root = Path::new("/project/target/site");
assert!(!is_ignored(root, Path::new("/project/target/site/app.css")));
assert!(is_ignored(root, Path::new("/project/target/site/target/x")));
}
#[test]
fn a_path_from_outside_the_root_is_left_alone() {
assert!(!is_ignored(
Path::new(ROOT),
Path::new("/elsewhere/app.css")
));
}
#[test]
fn files_the_server_will_not_send_are_ignored() {
for path in [
"/site/.env",
"/site/.idea/workspace.xml",
"/site/.vscode/settings.json",
] {
assert!(is_ignored(Path::new(ROOT), Path::new(path)), "{path}");
}
}
#[test]
fn changes_the_web_can_reach_are_not_ignored() {
assert!(!is_ignored(
Path::new(ROOT),
Path::new("/site/.well-known/token")
));
}
#[test]
fn reading_a_file_is_not_a_change() {
let read = event(
EventKind::Access(AccessKind::Open(AccessMode::Any)),
&["/site/index.html"],
);
assert!(!is_change(Path::new(ROOT), &read));
}
#[test]
fn a_finished_save_is_a_change() {
let saved = event(
EventKind::Access(AccessKind::Close(AccessMode::Write)),
&["/site/index.html"],
);
assert!(is_change(Path::new(ROOT), &saved));
}
#[test]
fn permissions_and_timestamps_are_not_changes() {
let touched = event(
EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)),
&["/site/index.html"],
);
assert!(!is_change(Path::new(ROOT), &touched));
}
#[test]
fn writing_creating_and_deleting_are_changes() {
let root = Path::new(ROOT);
assert!(is_change(root, &written(&["/site/app.css"])));
assert!(is_change(
root,
&event(EventKind::Create(CreateKind::File), &["/site/new.css"])
));
assert!(is_change(
root,
&event(EventKind::Remove(RemoveKind::File), &["/site/old.css"])
));
}
#[test]
fn writing_an_ignored_file_is_not_a_change() {
assert!(!is_change(
Path::new(ROOT),
&written(&["/site/app.css.swp"])
));
}
#[test]
fn one_real_file_among_ignored_ones_is_a_change() {
let mixed = written(&["/site/app.css.swp", "/site/app.css"]);
assert!(is_change(Path::new(ROOT), &mixed));
}
#[cfg(any(unix, windows))]
#[test]
fn a_new_directory_of_the_same_name_is_a_different_directory() {
let path = std::env::temp_dir().join(format!("servio-id-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).unwrap();
let watched = Watched::at(&path).expect("could not look at the directory");
assert_eq!(
directory_id(&path),
Some(watched.id),
"the same directory read twice"
);
std::fs::remove_dir_all(&path).unwrap();
assert_eq!(directory_id(&path), None, "there is no directory to read");
std::fs::create_dir_all(&path).unwrap();
assert_ne!(
directory_id(&path),
Some(watched.id),
"a rebuilt directory read as the old one"
);
std::fs::remove_dir_all(&path).unwrap();
}
}