From 323c05facfc50b9cf4232c52bc1cf7338729a4a5 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Nov 2026 20:31:00 +0000 Subject: [PATCH] Refuse to clean while a watcher of the project runs Why: rescript clean took only the build lock, which a watcher holds only while it builds, so a clean during rescript watch removed the artifacts the watcher kept reading, and every later rebuild failed (#9241). Diagnostic added (rescript clean while rescript watch runs): Before (text): Cleaning x, exit 0; the watcher then fails with Sys_error("/lib/rescript/A.ast: No such file or directory") After (diagnostic): Could not clean while rescript watch or another clean is running. The process ID (PID) is 4242 After (text): exit 1; the artifacts and the watcher are left as they were Clean now takes the watch lock of the folder it cleans, as rescript watch does, before the build lock. Review: Does a clean of a workspace see a watcher of one of its members? No: each takes the watch lock of the folder it was given, and a watcher takes it before it reads the configuration, which a test requires. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGBR9g3DD93vrWbYzU Signed-off-by: Christoph Knittel --- changes/clean-during-watch.fix.md | 1 + rewatch-ocaml/build_lock.ml | 9 ++------- rewatch-ocaml/build_lock.mli | 6 +++++- rewatch-ocaml/clean.ml | 8 ++++++++ .../tests/check_command_validation.sh | 19 +++++++++++++++++++ rewatch-ocaml/watcher.ml | 6 +++++- 6 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 changes/clean-during-watch.fix.md diff --git a/changes/clean-during-watch.fix.md b/changes/clean-during-watch.fix.md new file mode 100644 index 0000000000..4184c81d59 --- /dev/null +++ b/changes/clean-during-watch.fix.md @@ -0,0 +1 @@ +- `rescript clean` refuses while `rescript watch` runs for the same project, instead of removing the artifacts the watcher keeps reading. diff --git a/rewatch-ocaml/build_lock.ml b/rewatch-ocaml/build_lock.ml index 3d5a9f3398..064907ed82 100644 --- a/rewatch-ocaml/build_lock.ml +++ b/rewatch-ocaml/build_lock.ml @@ -164,17 +164,12 @@ let with_build ?(poll = fun () -> ()) ?(attempts = 1200) root action = root (fun lock -> action ~release:(fun () -> release lock)) -let with_watch root action = +let with_watch ~running root action = acquire ~name:"watch.lock" ~prefix:".watch-lock-" ~attempts:1000 ~delay:(fun () -> ignore (Unix.select [] [] [] 0.01)) ~timeout:"Timed out recovering a stale ReScript watch lock" ~on_live_owner:(fun ~first_attempt:_ owner -> - raise - (Project_context.Error - (Printf.sprintf - "Could not start Rescript build: A ReScript build is already \ - running. The process ID (PID) is %s" - owner))) + raise (Project_context.Error (running owner))) root (fun (lock : owned_lock) -> action {path = lock.path; pid = lock.pid}) diff --git a/rewatch-ocaml/build_lock.mli b/rewatch-ocaml/build_lock.mli index 28d4095673..421e3422ad 100644 --- a/rewatch-ocaml/build_lock.mli +++ b/rewatch-ocaml/build_lock.mli @@ -12,7 +12,11 @@ val with_build : (** Waits for another build's lock, by default for up to a minute ([attempts] times 50 ms). *) -val with_watch : string -> (watch -> 'a) -> 'a +val with_watch : running:(string -> string) -> string -> (watch -> 'a) -> 'a +(** [with_watch ~running root action] runs [action] holding the watch lock of + [root], which [rescript watch] holds while it runs and [rescript clean] + while it cleans. When a live process holds it, it fails with the message + [running pid]. *) val is_owned : watch -> bool diff --git a/rewatch-ocaml/clean.ml b/rewatch-ocaml/clean.ml index e8c1a2a3f2..68005b34e7 100644 --- a/rewatch-ocaml/clean.ml +++ b/rewatch-ocaml/clean.ml @@ -80,6 +80,14 @@ let run ~poll ~verbosity ~folder ~prod = (Output.cleaned_command_message ~color:colors ~step ~target ~seconds:(Unix.gettimeofday () -. started_at)) in + (* A watcher of this project would keep reading the artifacts removed + here. *) + Build_lock.with_watch root + ~running: + (Printf.sprintf + "Could not clean while rescript watch or another clean is running. \ + The process ID (PID) is %s") + @@ fun _watch_lock -> let lock_root = Project_context.workspace_lock_root root in Build_lock.with_build ~poll lock_root (fun ~release:_ -> poll (); diff --git a/rewatch-ocaml/tests/check_command_validation.sh b/rewatch-ocaml/tests/check_command_validation.sh index f1a01a56ba..edc840ee75 100755 --- a/rewatch-ocaml/tests/check_command_validation.sh +++ b/rewatch-ocaml/tests/check_command_validation.sh @@ -823,6 +823,25 @@ if [ "$(classify "$rescript_status")" != reject ] || \ dump_output exit 1 fi +checked=$((checked + 1)) +# Clean refuses while a watcher of the project runs, which would otherwise +# keep reading the removed artifacts. +run_rescript clean "$owner_dir" +if [ "$(classify "$rescript_status")" != reject ] || \ + ! grep -F 'Could not clean while rescript watch' "$work/rescript.err" \ + >/dev/null || \ + [ ! -f "$owner_dir/lib/rescript/A.cmt" ]; then + echo "clean-during-watch: clean did not refuse while a watcher runs" >&2 + dump_output + exit 1 +fi +printf 'let value = 2\n' >"$owner_dir/src/A.res" +wait_for_text "$owner_dir/watch.out" "Finished incremental compilation" +if grep -F 'No such file' "$owner_dir/watch.out" "$owner_dir/watch.err" >/dev/null; then + echo "clean-during-watch: the watcher lost its artifacts" >&2 + cat "$owner_dir/watch.out" "$owner_dir/watch.err" >&2 + exit 1 +fi kill -TERM "$watch_lock_owner_pid" wait "$watch_lock_owner_pid" 2>/dev/null || true checked=$((checked + 1)) diff --git a/rewatch-ocaml/watcher.ml b/rewatch-ocaml/watcher.ml index 8e2a0f0cee..0c3831aca4 100644 --- a/rewatch-ocaml/watcher.ml +++ b/rewatch-ocaml/watcher.ml @@ -309,7 +309,11 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features let run_with_native_create ~native_create ~report_native_fallback ~root ~prod ~features ~filter ~clear_screen ~show_progress ~verbosity ~build = - Build_lock.with_watch root (fun watch_lock -> + Build_lock.with_watch root + ~running: + (Printf.sprintf + "Could not start Rescript build: A ReScript build is already running. \ + The process ID (PID) is %s") (fun watch_lock -> ignore (Config.load_root root); run_locked ~native_create ~report_native_fallback ~root ~prod ~features ~filter ~clear_screen ~show_progress ~verbosity ~build ~watch_lock)