Skip to content

Commit cafcb17

Browse files
authored
Improve watchexec handling (#1362)
Fix up watchexec handling to not multi-spawn jobs, and handle multiple jobs correctly
1 parent ecd0074 commit cafcb17

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

src/bin/tectonic/v2cli/commands/watch.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ impl WatchCommand {
107107
[],
108108
[],
109109
)
110-
.await
111-
.unwrap();
110+
.await?;
112111

113112
async fn end_task(end: Ticket, job: Job) {
114113
end.await;
@@ -136,18 +135,18 @@ impl WatchCommand {
136135
Box::new(async move {
137136
// When we spawn a job it doesn't immediately become available. So we chain it
138137
// with existing jobs.
139-
let mut new_job = None;
140-
141-
if action.get_job(cmds[0].0).is_none() {
142-
for (id, cmd) in &*cmds {
143-
let job = action.get_or_create_job(*id, || Arc::clone(cmd));
144-
job.set_spawn_hook(|_, ctx| {
145-
println!("[Running `{}`]", ctx.command);
146-
});
147-
new_job = Some((*id, job));
148-
}
138+
let mut new_jobs = Vec::new();
139+
140+
for (id, cmd) in &*cmds {
141+
let job = action.get_or_create_job(*id, || Arc::clone(cmd));
142+
job.set_spawn_hook(|_, ctx| {
143+
println!("[Running `{}`]", ctx.command);
144+
});
145+
new_jobs.push((*id, job));
149146
}
150147

148+
let mut is_path = false;
149+
151150
for event in &*action.events {
152151
let is_kill = event.signals().any(|signal| {
153152
matches!(
@@ -165,16 +164,19 @@ impl WatchCommand {
165164
return action;
166165
}
167166

168-
let paths = event.paths().collect::<Vec<_>>();
169-
if !paths.is_empty() {
170-
for (_, job) in action.list_jobs().chain(new_job) {
167+
is_path |= event.paths().next().is_some();
168+
}
169+
170+
if is_path {
171+
for (_, job) in action.list_jobs().chain(new_jobs) {
172+
if !job.is_running() {
171173
job.start().await;
172174
let end = job.to_wait();
173175
tokio::spawn(end_task(end, job));
174176
}
175-
return action;
176177
}
177178
}
179+
178180
action
179181
})
180182
});
@@ -193,7 +195,7 @@ impl WatchCommand {
193195
.config
194196
.pathset([current_dir])
195197
.filterer(Arc::new(filter));
196-
exec_handler.main().await.unwrap().unwrap();
198+
exec_handler.main().await??;
197199
Ok(0)
198200
}
199201
}
@@ -204,10 +206,7 @@ impl TectonicCommand for WatchCommand {
204206
fn customize(&self, _cc: &mut CommandCustomizations) {}
205207

206208
fn execute(self, _config: PersistentConfig, status: &mut dyn StatusBackend) -> Result<i32> {
207-
let rt = runtime::Builder::new_multi_thread()
208-
.enable_all()
209-
.build()
210-
.unwrap();
209+
let rt = runtime::Builder::new_multi_thread().enable_all().build()?;
211210
rt.block_on(self.execute_inner(status))
212211
}
213212
}

0 commit comments

Comments
 (0)