Skip to content

Commit a531dc3

Browse files
cknittclaude
andauthored
Skip watch rebuilds for files whose content didn't change (#9305)
Why: macOS can report one write twice, the second time after the 50 ms drain window. The native watch loop re-read the file and rebuilt even when nothing differed from its snapshot, so a parse error was printed twice. This made the quiet-watch case of check_command_validation.sh fail on macOS in about 4% of CI runs. The test's filter for repeated reports had stopped working with the new report format in #9238. Diagnostic modified (repeated content event in watch mode): Example: let value = Before (diagnostic): rescript watch --quiet after one save on macOS Syntax error! <ROOT>/src/A.res:1:12-2:0 … This let-binding misses an expression Syntax error! <ROOT>/src/A.res:1:12-2:0 … This let-binding misses an expression After (diagnostic): Syntax error! <ROOT>/src/A.res:1:12-2:0 … This let-binding misses an expression Review: Does a read error still rebuild? Yes. update_entries counts a kept entry as a change, as before, so the build reports the problem. Review: Does touching a file still rebuild? Yes. The entry holds the modification time, as in the polling loop, so only an event for a file that is exactly as the snapshot recorded it is skipped. Claude-Session: https://claude.ai/code/session_01Kbf3gG8vKzkJGrU7AZ1CXQ Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c1927e6 commit a531dc3

6 files changed

Lines changed: 56 additions & 22 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Watch mode no longer rebuilds, and prints the same errors again, when one file change is reported twice.

‎rewatch-ocaml/tests/check_command_validation.sh‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,11 +1291,16 @@ if [ -s "$work/quiet-watch.out" ] || \
12911291
cat "$work/quiet-watch.out" "$work/quiet-watch.err" >&2
12921292
exit 1
12931293
fi
1294-
# macOS and Windows can report one write as several change events, and each
1295-
# rebuild reports the parse error again. Compare the distinct reports only.
1294+
# macOS and Windows can report one write as several change events. The
1295+
# watcher skips a rebuild when the file is unchanged, but a rebuild that sees
1296+
# the write half done would report the error again, so compare the distinct
1297+
# reports only. Each report starts with a blank line; the split is before it
1298+
# and must not cut a CRLF line ending.
12961299
node -e '
12971300
const fs = require("fs");
1298-
const blocks = fs.readFileSync(process.argv[1], "utf8").split(/^(?= Syntax error!)/m);
1301+
const blocks = fs
1302+
.readFileSync(process.argv[1], "utf8")
1303+
.split(/(?<=\n)(?=\r?\n Syntax error!\r?$)/m);
12991304
const distinct = blocks.filter((block, i) => block !== blocks[i - 1]);
13001305
fs.writeFileSync(process.argv[2], distinct.join(""));
13011306
' "$work/quiet-watch.err" "$work/quiet-watch.err.distinct"

‎rewatch-ocaml/watch_snapshot.ml‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,11 @@ let changes_between before after =
316316
(fun (first : change) second -> String.compare first.path second.path)
317317
!changes
318318
319+
type entries_update = Unchanged | Updated of entry list | Not_in_snapshot
320+
319321
let update_entries digest_cache previous changes =
320322
let changed = Hashtbl.create (List.length changes) in
323+
let any_changed = ref false in
321324
List.iter
322325
(fun (change : change) -> Hashtbl.replace changed change.path change.kind)
323326
changes;
@@ -329,13 +332,14 @@ let update_entries digest_cache previous changes =
329332
| Some Removed ->
330333
Hashtbl.remove changed entry.path;
331334
Hashtbl.remove digest_cache entry.path;
335+
any_changed := true;
332336
None
333337
| Some (Added | Modified) -> (
334338
Hashtbl.remove changed entry.path;
335339
try
336340
let stat = Unix.stat entry.path in
337341
let digest = hash_and_cache digest_cache entry.path stat in
338-
Some
342+
let updated =
339343
{
340344
path = entry.path;
341345
state =
@@ -346,17 +350,24 @@ let update_entries digest_cache previous changes =
346350
digest;
347351
};
348352
}
353+
in
354+
if not (entry_equal updated entry) then any_changed := true;
355+
Some updated
349356
with
350357
| Unix.Unix_error ((Unix.ENOENT | Unix.ENOTDIR), _, _) ->
351358
Hashtbl.remove digest_cache entry.path;
359+
any_changed := true;
352360
None
353361
| Unix.Unix_error _ | Sys_error _ ->
354362
(* Preserve the old baseline so the caller still runs the build.
355363
Source diagnostics then remain inside the watcher's recoverable
356364
build boundary instead of terminating the watch loop here. *)
365+
any_changed := true;
357366
Some entry))
358367
in
359-
if Hashtbl.length changed = 0 then Some updated else None
368+
if Hashtbl.length changed > 0 then Not_in_snapshot
369+
else if !any_changed then Updated updated
370+
else Unchanged
360371
361372
let changes_are_incremental changes =
362373
changes <> []

‎rewatch-ocaml/watch_snapshot.mli‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ val create_with_symlink_paths : digest_cache -> Watch_scope.t -> registration
4747

4848
val changes_between : entry list -> entry list -> change list
4949

50-
val update_entries :
51-
digest_cache -> entry list -> change list -> entry list option
50+
(* The result of re-reading the files a native watcher reported. A platform
51+
can report one write more than once (macOS FSEvents may repeat it after
52+
the drain window), so [Unchanged] means every file still matches its
53+
entry and no build is needed. [Not_in_snapshot] means a reported path has
54+
no entry and the snapshot must be reconciled. *)
55+
type entries_update = Unchanged | Updated of entry list | Not_in_snapshot
56+
57+
val update_entries : digest_cache -> entry list -> change list -> entries_update
5258

5359
val changes_are_incremental : change list -> bool
5460

‎rewatch-ocaml/watcher.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,13 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features
223223
| Some [] -> native_loop watcher scope symlink_targets previous
224224
| Some changes -> (
225225
match Watch_snapshot.update_entries digest_cache previous changes with
226-
| Some before_build ->
226+
| Updated before_build ->
227227
Output.debug ~verbosity "doing Incremental";
228228
begin_rebuild Incremental;
229229
build ~poll ~changes:(Some changes) |> finish_rebuild;
230230
native_loop watcher scope symlink_targets before_build
231-
| None -> native_reconcile watcher scope previous)
231+
| Unchanged -> native_loop watcher scope symlink_targets previous
232+
| Not_in_snapshot -> native_reconcile watcher scope previous)
232233
| None -> native_reconcile watcher scope previous)
233234
and native_reconcile watcher (scope : Watch_scope.t) previous =
234235
let current = Watch_snapshot.create digest_cache scope in

‎tests/rewatch_ounit_tests/watcher_tests.ml‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,36 @@ let tests =
172172
};
173173
]
174174
in
175-
Test_support.write_file source "let value = 2\n";
175+
let modified = [Watch_snapshot.{path = source; kind = Modified}] in
176176
check
177-
(Option.is_some
178-
(Watch_snapshot.update_entries digest_cache previous
179-
[Watch_snapshot.{path = source; kind = Modified}]))
177+
(Watch_snapshot.update_entries digest_cache previous modified
178+
= Unchanged)
179+
"a repeated event for unchanged content needs no rebuild";
180+
Test_support.write_file source "let value = 2\n";
181+
let updated =
182+
match Watch_snapshot.update_entries digest_cache previous modified with
183+
| Updated entries -> Some entries
184+
| Unchanged | Not_in_snapshot -> None
185+
in
186+
check (Option.is_some updated)
180187
"known content events update the retained snapshot";
181188
check
182-
(Option.is_none
183-
(Watch_snapshot.update_entries digest_cache previous
184-
[
185-
Watch_snapshot.
186-
{path = Filename.concat root "B.res"; kind = Added};
187-
]))
189+
(match updated with
190+
| Some entries ->
191+
Watch_snapshot.update_entries digest_cache entries modified
192+
= Unchanged
193+
| None -> false)
194+
"a repeated event after the update needs no rebuild";
195+
check
196+
(Watch_snapshot.update_entries digest_cache previous
197+
[Watch_snapshot.{path = Filename.concat root "B.res"; kind = Added}]
198+
= Not_in_snapshot)
188199
"unknown content events request structural reconciliation";
189200
Sys.remove source;
190201
Unix.mkdir source 0o755;
191202
check
192-
(Option.is_some
193-
(Watch_snapshot.update_entries digest_cache previous
194-
[Watch_snapshot.{path = source; kind = Modified}]))
203+
(Watch_snapshot.update_entries digest_cache previous modified
204+
= Updated previous)
195205
"a transient snapshot read failure preserves the rebuild baseline");
196206
Test_support.with_temp_dir "rewatch-snapshot-racy-" (fun root ->
197207
let root = Unix.realpath root in

0 commit comments

Comments
 (0)