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/watch-edit-between-snapshots.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix watch mode sometimes ignoring a file saved right after a rebuild started.
43 changes: 27 additions & 16 deletions rewatch-ocaml/watcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type fallback = {
snapshot: Watch_snapshot.entry list;
}

let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
~filter ~clear_screen ~show_progress ~verbosity ~build ~watch_lock =
let run_locked ~native_create ~discover ~report_native_fallback ~root ~prod
~features ~filter ~clear_screen ~show_progress ~verbosity ~build ~watch_lock
=
(* The exit status of the signal that requested the stop, or 0. *)
let stop_requested = Atomic.make 0 in
let stop signal =
Expand Down Expand Up @@ -179,12 +180,12 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
if keep_running () then
let current = Watch_snapshot.create digest_cache scope in
if not (Watch_snapshot.equal current previous) then (
let build_scope = Watch_scope.discover ~root ~prod ~features ~filter in
let build_scope = discover ~root ~prod ~features ~filter in
let before_build = Watch_snapshot.create digest_cache build_scope in
let changes = Watch_snapshot.changes_between previous before_build in
start_rebuild changes;
build ~poll ~changes:(Some changes) |> finish_rebuild;
let new_scope = Watch_scope.discover ~root ~prod ~features ~filter in
let new_scope = discover ~root ~prod ~features ~filter in
let after_build = Watch_snapshot.create digest_cache new_scope in
let baseline =
Watch_snapshot.reconciliation_baseline ~old_scope:build_scope
Expand All @@ -204,7 +205,7 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
let fallback_to_polling message snapshot =
report_native_fallback message;
ignore (build ~poll ~changes:None);
let scope = Watch_scope.discover ~root ~prod ~features ~filter in
let scope = discover ~root ~prod ~features ~filter in
polling_loop scope snapshot
in
let rec native_loop watcher (scope : Watch_scope.t) symlink_targets previous =
Expand Down Expand Up @@ -234,15 +235,18 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
and native_reconcile watcher (scope : Watch_scope.t) previous =
let current = Watch_snapshot.create digest_cache scope in
if not (Watch_snapshot.equal current previous) then (
let changes = Watch_snapshot.changes_between previous current in
start_rebuild changes;
let build_scope = Watch_scope.discover ~root ~prod ~features ~filter in
let build_scope = discover ~root ~prod ~features ~filter in
let before_build = Watch_snapshot.create digest_cache build_scope in
(* Report the changes up to the snapshot that becomes the next baseline.
An edit landing after [current] was taken is already part of
[before_build], so it would otherwise never be reported to a build. *)
let changes = Watch_snapshot.changes_between previous before_build in
start_rebuild changes;
build ~poll ~changes:(Some changes) |> finish_rebuild;
reconcile_scope watcher ~old_scope:build_scope ~before:before_build)
else reconcile_scope watcher ~old_scope:scope ~before:current
and reconcile_scope watcher ~(old_scope : Watch_scope.t) ~before =
let new_scope = Watch_scope.discover ~root ~prod ~features ~filter in
let new_scope = discover ~root ~prod ~features ~filter in
match Watch_snapshot.create_with_symlink_paths digest_cache new_scope with
| Watch_snapshot.Registration_failed {snapshot; message} ->
let snapshot =
Expand Down Expand Up @@ -271,7 +275,7 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
else "\nExiting... (lockfile removed)")
in
let watch () =
let scope = Watch_scope.discover ~root ~prod ~features ~filter in
let scope = discover ~root ~prod ~features ~filter in
match Watch_snapshot.create_with_symlink_paths digest_cache scope with
| Watch_snapshot.Registration_failed {snapshot; message} ->
fallback_to_polling message snapshot
Expand Down Expand Up @@ -317,16 +321,19 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
report_exit ();
stopped ())

let run_with_native_create ~native_create ~report_native_fallback ~root ~prod
~features ~filter ~clear_screen ~show_progress ~verbosity ~build =
(* [native_create] and [discover] are replaced in tests *)
let run_with_native_create ~native_create ~discover ~report_native_fallback
~root ~prod ~features ~filter ~clear_screen ~show_progress ~verbosity ~build
=
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)
run_locked ~native_create ~discover ~report_native_fallback ~root ~prod
~features ~filter ~clear_screen ~show_progress ~verbosity ~build
~watch_lock)

let report_native_fallback message =
prerr_endline
Expand All @@ -335,11 +342,15 @@ let report_native_fallback message =

let run =
run_with_native_create ~native_create:Native_watcher.create
~report_native_fallback
~discover:Watch_scope.discover ~report_native_fallback

module For_test = struct
let run_with_native_failure ~message ~on_fallback =
run_with_native_create
~native_create:(fun ~paths:_ -> Error message)
~report_native_fallback:on_fallback
~discover:Watch_scope.discover ~report_native_fallback:on_fallback

let run_with_discover ~discover =
run_with_native_create ~native_create:Native_watcher.create ~discover
~report_native_fallback
end
19 changes: 19 additions & 0 deletions rewatch-ocaml/watcher.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ module For_test : sig
verbosity:int ->
build:(poll:(unit -> unit) -> changes:change list option -> build_result) ->
unit

val run_with_discover :
discover:
(root:string ->
prod:bool ->
features:string list option ->
filter:Source_filter.t option ->
Watch_scope.t) ->
root:string ->
prod:bool ->
features:string list option ->
filter:Source_filter.t option ->
clear_screen:bool ->
show_progress:bool ->
verbosity:int ->
build:(poll:(unit -> unit) -> changes:change list option -> build_result) ->
unit
(** {!run} with [discover] in place of [Watch_scope.discover], to change
files at the moments the watcher looks for them. *)
end

val run :
Expand Down
1 change: 1 addition & 0 deletions tests/rewatch_ounit_tests/rewatch_ounit_tests_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ let () =
Unit_tests.tests;
Warning_state_tests.tests;
Watcher_tests.tests;
Watcher_tests.edit_between_snapshots_tests;
])
49 changes: 49 additions & 0 deletions tests/rewatch_ounit_tests/watcher_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,52 @@ let tests =
check
(signal_is_ignored Sys.sigterm)
"SIGTERM handler is restored after polling fallback failure")

(* After a build, the native watcher snapshots the sources to find what changed
and then snapshots them again as the baseline for the next round. An edit
between the two must still reach a build. The injected [discover] runs
between them and edits B after the initial build edited C. *)
let edit_between_snapshots_tests =
"watcher_edit_between_snapshots" >:: fun _context ->
Test_support.with_temp_dir "rewatch-watch-edit-between-snapshots-"
(fun root ->
Test_support.write_file
(Filename.concat root "rescript.json")
{|{"name":"edit-between-snapshots","sources":"src"}|};
let source name = Filename.concat (Filename.concat root "src") name in
let b = source "B.res" and c = source "C.res" in
Test_support.write_file b "let value = 1\n";
Test_support.write_file c "let value = 1\n";
let edit_b = ref false in
let discover ~root ~prod ~features ~filter =
if !edit_b then (
edit_b := false;
Test_support.write_file b "let value = 22\n");
Watch_scope.discover ~root ~prod ~features ~filter
in
let rebuilt = ref None in
(try
Watcher.For_test.run_with_discover ~discover ~root ~prod:false
~features:None ~filter:None ~clear_screen:false ~show_progress:false
~verbosity:0 ~build:(fun ~poll:_ ~changes ->
match changes with
| None ->
Test_support.write_file c "let value = 22\n";
edit_b := true;
Watcher.Succeeded
| Some changes ->
rebuilt := Some changes;
raise Exit)
with Exit -> ());
match !rebuilt with
| None -> assert_failure "the watcher never rebuilt"
| Some changes ->
let modified path =
List.exists
(fun (change : Watcher.change) ->
change.path = path && change.kind = Modified)
changes
in
check (modified c) "the rebuild reports the edit of C";
check (modified b)
"the rebuild reports the edit of B made between the snapshots")
Loading