Skip to content

Commit 4553473

Browse files
committed
Fix watch startup build locking
Fix watch startup build locking
1 parent ad7bceb commit 4553473

1 file changed

Lines changed: 88 additions & 60 deletions

File tree

rewatch/src/watcher.rs

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ fn matches_filter(path_buf: &Path, filter: &Option<regex::Regex>) -> bool {
5858
filter.as_ref().map(|re| !re.is_match(&name)).unwrap_or(true)
5959
}
6060

61+
fn finish_successful_watch_compile(
62+
after_build: Option<String>,
63+
timing_total: Instant,
64+
show_progress: bool,
65+
plain_output: bool,
66+
finished_message: &str,
67+
) {
68+
if let Some(a) = after_build {
69+
cmd::run(a)
70+
}
71+
let timing_total_elapsed = timing_total.elapsed();
72+
if show_progress {
73+
if plain_output {
74+
println!("{finished_message}")
75+
} else {
76+
println!(
77+
"\n{}{}{} in {:.2}s\n",
78+
LINE_CLEAR,
79+
SPARKLES,
80+
finished_message,
81+
timing_total_elapsed.as_secs_f64()
82+
);
83+
}
84+
}
85+
}
86+
6187
/// Computes the list of paths to watch based on the build state.
6288
/// Returns tuples of (path, recursive_mode) for each watch target.
6389
fn compute_watch_paths(build_state: &BuildCommandState, root: &Path) -> Vec<(PathBuf, RecursiveMode)> {
@@ -206,7 +232,7 @@ async fn async_watch(
206232
}: AsyncWatchArgs<'_>,
207233
) -> Result<()> {
208234
let mut build_state = initial_build_state;
209-
let mut needs_compile_type = CompileType::Incremental;
235+
let mut needs_compile_type = CompileType::None;
210236
// create a mutex to capture if ctrl-c was pressed
211237
let ctrlc_pressed = Arc::new(Mutex::new(false));
212238
let ctrlc_pressed_clone = Arc::clone(&ctrlc_pressed);
@@ -218,8 +244,6 @@ async fn async_watch(
218244
})
219245
.expect("Error setting Ctrl-C handler");
220246

221-
let mut initial_build = true;
222-
223247
loop {
224248
if *ctrlc_pressed_clone.lock().unwrap() {
225249
if show_progress {
@@ -380,35 +404,23 @@ async fn async_watch(
380404
if build::incremental_build(
381405
&mut build_state,
382406
None,
383-
initial_build,
407+
false,
384408
show_progress,
385-
!initial_build,
409+
true,
386410
create_sourcedirs,
387411
plain_output,
388412
)
389413
.is_ok()
390414
{
391-
if let Some(a) = after_build.clone() {
392-
cmd::run(a)
393-
}
394-
let timing_total_elapsed = timing_total.elapsed();
395-
if show_progress {
396-
let compilation_type = if initial_build { "initial" } else { "incremental" };
397-
if plain_output {
398-
println!("Finished {compilation_type} compilation")
399-
} else {
400-
println!(
401-
"\n{}{}Finished {} compilation in {:.2}s\n",
402-
LINE_CLEAR,
403-
SPARKLES,
404-
compilation_type,
405-
timing_total_elapsed.as_secs_f64()
406-
);
407-
}
408-
}
415+
finish_successful_watch_compile(
416+
after_build.clone(),
417+
timing_total,
418+
show_progress,
419+
plain_output,
420+
"Finished incremental compilation",
421+
);
409422
}
410423
needs_compile_type = CompileType::None;
411-
initial_build = false;
412424
}
413425
CompileType::Full => {
414426
let timing_total = Instant::now();
@@ -439,7 +451,7 @@ async fn async_watch(
439451
let result = build::incremental_build_without_lock(
440452
&mut build_state,
441453
None,
442-
initial_build,
454+
false,
443455
show_progress,
444456
false,
445457
create_sourcedirs,
@@ -449,26 +461,15 @@ async fn async_watch(
449461
result
450462
});
451463
if result.is_ok() {
452-
if let Some(a) = after_build.clone() {
453-
cmd::run(a)
454-
}
455-
456-
let timing_total_elapsed = timing_total.elapsed();
457-
if show_progress {
458-
if plain_output {
459-
println!("Finished compilation")
460-
} else {
461-
println!(
462-
"\n{}{}Finished compilation in {:.2}s\n",
463-
LINE_CLEAR,
464-
SPARKLES,
465-
timing_total_elapsed.as_secs_f64()
466-
);
467-
}
468-
}
464+
finish_successful_watch_compile(
465+
after_build.clone(),
466+
timing_total,
467+
show_progress,
468+
plain_output,
469+
"Finished compilation",
470+
);
469471
}
470472
needs_compile_type = CompileType::None;
471-
initial_build = false;
472473
}
473474
CompileType::None => {
474475
// We want to sleep for a little while so the CPU can schedule other work. That way we end
@@ -499,23 +500,50 @@ pub fn start(
499500

500501
let path = Path::new(folder);
501502

502-
// Do an initial build to discover packages and source folders. Initialization can clean
503-
// previous build artifacts, so it has to be serialized with other build operations.
504-
let build_state: BuildCommandState = build::with_build_lock(path, || {
505-
build::initialize_build(
506-
None,
507-
filter,
508-
show_progress,
509-
path,
510-
plain_output,
511-
warn_error.clone(),
512-
)
513-
.with_context(|| "Could not initialize build")
514-
})?;
515-
516-
// Compute and register targeted watches based on source folders
517-
let current_watch_paths = compute_watch_paths(&build_state, path);
518-
register_watches(&mut watcher, &current_watch_paths);
503+
// Initialization can clean previous build artifacts, so it has to be serialized
504+
// with the initial compile too.
505+
let (build_state, current_watch_paths): (BuildCommandState, Vec<(PathBuf, RecursiveMode)>) =
506+
build::with_build_lock(path, || {
507+
let mut build_state = build::initialize_build(
508+
None,
509+
filter,
510+
show_progress,
511+
path,
512+
plain_output,
513+
warn_error.clone(),
514+
)
515+
.with_context(|| "Could not initialize build")?;
516+
517+
// Compute and register targeted watches based on source folders.
518+
let current_watch_paths = compute_watch_paths(&build_state, path);
519+
register_watches(&mut watcher, &current_watch_paths);
520+
521+
let timing_total = Instant::now();
522+
if build::incremental_build_without_lock(
523+
&mut build_state,
524+
None,
525+
true,
526+
show_progress,
527+
false,
528+
create_sourcedirs,
529+
plain_output,
530+
)
531+
.is_ok()
532+
{
533+
finish_successful_watch_compile(
534+
after_build.clone(),
535+
timing_total,
536+
show_progress,
537+
plain_output,
538+
"Finished initial compilation",
539+
);
540+
}
541+
542+
Ok::<(BuildCommandState, Vec<(PathBuf, RecursiveMode)>), anyhow::Error>((
543+
build_state,
544+
current_watch_paths,
545+
))
546+
})?;
519547

520548
async_watch(AsyncWatchArgs {
521549
watcher: &mut watcher,

0 commit comments

Comments
 (0)