From 074a99cc915b965abd68e4fdce6bf778155a209d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 24 Nov 2026 07:47:41 +0000 Subject: [PATCH] Rebuild edits made between the watcher's two snapshots After a build, the native watcher took one snapshot of the sources to find what changed and then a second as the baseline for the next round. A file saved between the two was in the baseline but was never reported to a build, so watch mode ignored it until the file changed again. That is the flaky "watch-retained-graph" test on macOS: B.res, saved right after C.res, never got recompiled. The changes now come from the snapshot that becomes the baseline, as the polling watcher already does. A unit test injects the watcher's source discovery, which runs between the two snapshots, to edit a file exactly there, and checks that the next build reports it. It fails with the old snapshot order. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH Signed-off-by: Christoph Knittel --- changes/watch-edit-between-snapshots.fix.md | 1 + rewatch-ocaml/watcher.ml | 43 ++++++++++------ rewatch-ocaml/watcher.mli | 19 +++++++ .../rewatch_ounit_tests_main.ml | 1 + tests/rewatch_ounit_tests/watcher_tests.ml | 49 +++++++++++++++++++ 5 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 changes/watch-edit-between-snapshots.fix.md diff --git a/changes/watch-edit-between-snapshots.fix.md b/changes/watch-edit-between-snapshots.fix.md new file mode 100644 index 00000000000..1bbdc045d82 --- /dev/null +++ b/changes/watch-edit-between-snapshots.fix.md @@ -0,0 +1 @@ +- Fix watch mode sometimes ignoring a file saved right after a rebuild started. diff --git a/rewatch-ocaml/watcher.ml b/rewatch-ocaml/watcher.ml index de0fd5006c3..2b5c12761f9 100644 --- a/rewatch-ocaml/watcher.ml +++ b/rewatch-ocaml/watcher.ml @@ -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 = @@ -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 @@ -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 = @@ -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 = @@ -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 @@ -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 @@ -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 diff --git a/rewatch-ocaml/watcher.mli b/rewatch-ocaml/watcher.mli index b28b18dfb6b..a2e5a17872d 100644 --- a/rewatch-ocaml/watcher.mli +++ b/rewatch-ocaml/watcher.mli @@ -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 : diff --git a/tests/rewatch_ounit_tests/rewatch_ounit_tests_main.ml b/tests/rewatch_ounit_tests/rewatch_ounit_tests_main.ml index 6c69a603835..a23da794e43 100644 --- a/tests/rewatch_ounit_tests/rewatch_ounit_tests_main.ml +++ b/tests/rewatch_ounit_tests/rewatch_ounit_tests_main.ml @@ -28,4 +28,5 @@ let () = Unit_tests.tests; Warning_state_tests.tests; Watcher_tests.tests; + Watcher_tests.edit_between_snapshots_tests; ]) diff --git a/tests/rewatch_ounit_tests/watcher_tests.ml b/tests/rewatch_ounit_tests/watcher_tests.ml index 96f0d989978..90d9b6f96f6 100644 --- a/tests/rewatch_ounit_tests/watcher_tests.ml +++ b/tests/rewatch_ounit_tests/watcher_tests.ml @@ -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")