Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/clean-during-watch.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `rescript clean` refuses while `rescript watch` runs for the same project, instead of removing the artifacts the watcher keeps reading.
9 changes: 2 additions & 7 deletions rewatch-ocaml/build_lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
6 changes: 5 additions & 1 deletion rewatch-ocaml/build_lock.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions rewatch-ocaml/clean.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
19 changes: 19 additions & 0 deletions rewatch-ocaml/tests/check_command_validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion rewatch-ocaml/watcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading