From e1d76960eb67443fbe58e5af0cc6da2facee26a1 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sat, 24 Jun 2023 14:24:43 +0200 Subject: [PATCH] Watch dependencies that are outside of root (#1523) --- cli/src/main.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index af509a92c..19edd6d70 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -237,6 +237,16 @@ fn compile(mut command: CompileSettings) -> StrResult<()> { .map_err(|_| "failed to watch root directory")?; } + // Watch all the files that are used in the input file and its dependencies + let mut dependencies = world.dependencies(); + + for dep in &dependencies { + tracing::debug!("Watching {:?}", dep); + watcher + .watch(dep, RecursiveMode::NonRecursive) + .map_err(|_| format!("failed to watch {:?}", dep))?; + } + // Handle events. let timeout = std::time::Duration::from_millis(100); loop { @@ -262,6 +272,20 @@ fn compile(mut command: CompileSettings) -> StrResult<()> { let ok = compile_once(&mut world, &command)?; comemo::evict(30); + // Unwatch all the previous dependencies before watching the new dependencies + for dep in &dependencies { + watcher + .unwatch(dep) + .map_err(|_| format!("failed to unwatch {:?}", dep))?; + } + dependencies = world.dependencies(); + for dep in &dependencies { + tracing::debug!("Watching {:?}", dep); + watcher + .watch(dep, RecursiveMode::NonRecursive) + .map_err(|_| format!("failed to watch {:?}", dep))?; + } + // Ipen the file if requested, this must be done on the first // **successful** compilation if ok { @@ -506,6 +530,7 @@ struct SystemWorld { sources: FrozenVec>, today: Cell>, main: SourceId, + dependencies: RefCell>, } /// Holds details about the location of a font and lazily the font itself. @@ -537,6 +562,7 @@ impl SystemWorld { sources: FrozenVec::new(), today: Cell::new(None), main: SourceId::detached(), + dependencies: RefCell::default(), } } } @@ -567,6 +593,7 @@ impl World for SystemWorld { // Assume UTF-8 String::from_utf8(buf)? }; + self.dependencies.borrow_mut().push(path.to_owned()); Ok(self.insert(path, text)) }) .clone() @@ -593,7 +620,10 @@ impl World for SystemWorld { fn file(&self, path: &Path) -> FileResult { self.slot(path)? .buffer - .get_or_init(|| read(path).map(Buffer::from)) + .get_or_init(|| { + self.dependencies.borrow_mut().push(path.to_owned()); + read(path).map(Buffer::from) + }) .clone() } @@ -675,6 +705,12 @@ impl SystemWorld { self.hashes.borrow_mut().clear(); self.paths.borrow_mut().clear(); self.today.set(None); + self.dependencies.borrow_mut().clear(); + } + + // Return a list of files the document depends on + fn dependencies(&self) -> Vec { + self.dependencies.borrow().clone() } }