-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typst watch
doesn't work when editing the watched file with (neo)vim
#1651
Comments
Workaround: $ echo main.typ | entr -c typst compile /_ Incidentally the availability and general usefulness of |
I am also having this issue with |
I guess for Typst it's a bit different than for SILE because built-in watch can profit from incremental compilation. If I understand the Unfortunate that the new approach has this issue because watching individual files instead of the root directory is much nicer in principle (less unnecessary recompilations for files that don't affect the document even though they are in the working dir and it correctly picks up imports from packages). |
Yes, this level of workaround does loose the benefit of incremental compilation. Entr can watch anything you tell it to. For example one approach is to tell it to watch everything tracked by git: |
this issue stems from the fact that vim create a new file and move it to replace the old one. |
Unfortunately I cannot currently test any potential fix as I'm on macOS and don't have a Linux machine available. If anybody on Linux wants to give fixing this a shot, I'd appreciate it. (It works for me in vim on macOS.) |
I have some issue using
My solutionfind . -type f -iname "*.typ" | entr make all: compile
compile:
@typst compile main.typ |
@ayoubeimhamdi By rights your Makefile should either by a Justfile (since you're just using it as a task runner) or maybe try including dependencies ;-) .PHONY: all
all: main.pdf
DEPS != find . -type f -iname "*.typ"
main.pdf: main.typ $(DEPS)
@typst compile $< Now Back to topic: @laurmaedje Given that many other tools are able to catch changes to files when Neovim and other apps that do write→rename sequences (including |
Removing the deleted files from the previous compilation's dependencies in the (I am not very familiar with rust, especially the borrow checker part, so there might be a better way to do this) loop {
let mut removed = HashSet::new(); // will contain paths to the removed files
let mut recompile = false;
for event in rx
.recv()
.into_iter()
.chain(std::iter::from_fn(|| rx.recv_timeout(timeout).ok()))
{
let event = event.map_err(|_| "failed to watch directory")?;
if matches!(event.kind, notify::EventKind::Remove(notify::event::RemoveKind::File)) {
let path = &event.paths[0];
removed.insert(path.clone());
// Tries to remove the watch if it still exists
// I don't know if watching the same file multiple times could be problematic on some
// platforms, so this may not be necessary
match watcher.unwatch(&path) {
Err(notify::Error { kind, .. }) => match kind {
notify::ErrorKind::WatchNotFound => {},
_ => return Err("failed to unwatch file".into()),
}
_ => {}
}
}
recompile |= is_event_relevant(&event, &output);
}
if recompile {
// Retrieve the dependencies of the last compilation.
let previous: HashSet<PathBuf> = world
.dependencies()
.filter(|path| !removed.contains(*path)) // remove the deleted files from the previous deps
.map(ToOwned::to_owned)
.collect();
// Recompile.
compile_once(&mut world, &mut command, true)?;
comemo::evict(10);
// Adjust the watching.
watch_dependencies(&mut world, &mut watcher, previous)?;
}
} |
@tretre91 Nice, that seems like a good solution! |
Description
The watch command does not seem to work with (neo)vim anymore.
The issue seems to come from the fact that when saving a file, vim will rename the old file and write a new file with the old name. However, for notify-rs this causes a
Remove
event to be triggered on that file, and removed files are implicitly unwatched on some platforms according to their docs:When running the code through gdb, after saving a first time, the program just waits for events there, and saving the file again does not trigger events.
Note: the oldest commit where this saving behavior caused an issue was e1d7696, trying to unwatch the dependencies, some of which were already unwatched, caused a
failed to unwatch something.typ
error to happen.steps to reproduce
test.typ
typst watch test.typ
:w
), this one should workVim's saving method can be changed to copy the new file into the old file by executing the command
:set backupcopy=yes
, in this case thetypst watch
command works as expected.Reproduction URL
No response
Operating system
Linux
Typst version
The text was updated successfully, but these errors were encountered: