summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrévis Morvany <63788850+tretre91@users.noreply.github.com>2023-07-10 12:21:59 +0200
committerGitHub <noreply@github.com>2023-07-10 12:21:59 +0200
commit0e8492eac1606edba86748c4de547a2c758cd232 (patch)
tree528103157eace68dc4b1b0297152d509c20e2547
parentdfe361ec6a4adfab4140bf480aae3a39b2683165 (diff)
Fix `typst watch` not working with some text editors (#1680)
-rw-r--r--crates/typst-cli/src/watch.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/crates/typst-cli/src/watch.rs b/crates/typst-cli/src/watch.rs
index 2ad73f85..cf9c05ba 100644
--- a/crates/typst-cli/src/watch.rs
+++ b/crates/typst-cli/src/watch.rs
@@ -34,6 +34,7 @@ pub fn watch(mut command: CompileCommand) -> StrResult<()> {
let timeout = std::time::Duration::from_millis(100);
let output = command.output();
loop {
+ let mut removed = HashSet::new();
let mut recompile = false;
for event in rx
.recv()
@@ -41,13 +42,32 @@ pub fn watch(mut command: CompileCommand) -> StrResult<()> {
.chain(std::iter::from_fn(|| rx.recv_timeout(timeout).ok()))
{
let event = event.map_err(|_| "failed to watch directory")?;
+
+ // Workaround for notify-rs' implicit unwatch on remove/rename
+ // (triggered by some editors when saving files) with the inotify
+ // backend. By keeping track of the removed files, we can allow
+ // those we still depend on to be watched again later on.
+ if matches!(
+ event.kind,
+ notify::EventKind::Remove(notify::event::RemoveKind::File)
+ ) {
+ let path = &event.paths[0];
+ removed.insert(path.clone());
+
+ // Remove the watch in case it still exists.
+ watcher.unwatch(path).ok();
+ }
+
recompile |= is_event_relevant(&event, &output);
}
if recompile {
// Retrieve the dependencies of the last compilation.
- let previous: HashSet<PathBuf> =
- world.dependencies().map(ToOwned::to_owned).collect();
+ let previous: HashSet<PathBuf> = world
+ .dependencies()
+ .filter(|path| !removed.contains(*path))
+ .map(ToOwned::to_owned)
+ .collect();
// Recompile.
compile_once(&mut world, &mut command, true)?;