Skip to content

Commit fb85e7b

Browse files
cknittclaude
andauthored
Fix watcher missing watch.lock removal during full rebuilds (#8730)
* Fix watcher missing watch.lock removal during full rebuilds On macOS, notify's FSEvents backend stops and recreates its event stream on every watch/unwatch call, and events in between are lost. Full rebuilds unwatched and re-watched every path, so a watch.lock removal right after a source file removal (as in the rewatch watch tests) could be dropped, leaving the watcher running. Only unwatch/watch the paths that actually changed, and check that watch.lock still exists after a full rebuild. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Add PR link to CHANGELOG Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Retry watch paths that failed to register Only record successfully registered watch paths, so that a path whose registration failed is retried on the next full rebuild instead of being treated as already watched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Re-register watches of source dirs replaced at the same path inotify ties a watch to the directory rather than its path, so a source directory that is deleted and recreated at the same path is no longer watched. Record each watched directory's identity (device and inode) and re-register watches whose directory changed on the next full rebuild. Platforms without stable file identities always re-register. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Replace replaced-source-dir watch test with a unit test The integration test only exercised the identity check on Linux while adding another timing-sensitive watch test on every platform. Test the identity check directly instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Clarify watch.lock check comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 04af7f5 commit fb85e7b

2 files changed

Lines changed: 131 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Fix the formatter dropping a `@res.ternary` attribute written in source, e.g. `let b = @res.ternary x`, and print `@JSX` and `@res.ternary` with the same parens as other attributes, e.g. `(@JSX x) + 1`. https://github.com/rescript-lang/rescript/pull/8740
2929
- Respect `@warning` attributes on the module in `module type of`, e.g. `module type of @warning("-3") DeprecatedModule`, which still reported the warning. https://github.com/rescript-lang/rescript/pull/8738
3030
- Fix the formatter dropping or moving attributes and `await` on module expressions, such as `module M = @attr F(X)`, and printing invalid code for module constraints and functors in some positions, such as `module M: T = (X: S)`. https://github.com/rescript-lang/rescript/pull/8735
31+
- Fix `rescript watch` sometimes not exiting on macOS when `lib/watch.lock` is removed during a full rebuild, and stop re-registering unchanged watch paths on full rebuilds. https://github.com/rescript-lang/rescript/pull/8730
3132
- Make rewatch compile independent modules after an unrelated failure and recompile blocked dependents when a changed interface survives a failed implementation, including across full watcher rebuilds. https://github.com/rescript-lang/rescript/pull/8667
3233
- Fix `RescriptTools.binaryPath` failing with `ERR_PACKAGE_IMPORT_NOT_DEFINED` when used from a user project. https://github.com/rescript-lang/rescript/pull/8694
3334
- GenType: type inline-record cases of untagged variants as objects instead of their single field's type. https://github.com/rescript-lang/rescript/pull/8693

‎rewatch/src/watcher.rs‎

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum CompileType {
2828
type WatchPaths = Vec<(PathBuf, RecursiveMode)>;
2929
type StartupBuildResult = (
3030
BuildCommandState,
31-
WatchPaths,
31+
Vec<RegisteredWatch>,
3232
Option<(Instant, build::CompilationOutcome)>,
3333
);
3434

@@ -150,26 +150,103 @@ fn compute_watch_paths(build_state: &BuildCommandState, root: &Path) -> Vec<(Pat
150150
watch_paths.into_iter().collect()
151151
}
152152

153-
/// Registers all watch paths with the given watcher.
154-
fn register_watches(watcher: &mut RecommendedWatcher, watch_paths: &[(PathBuf, RecursiveMode)]) {
153+
/// Identity of a watched directory, used to notice when it was replaced at the same path.
154+
type DirIdentity = (u64, u64);
155+
156+
#[cfg(unix)]
157+
fn dir_identity(path: &Path) -> Option<DirIdentity> {
158+
use std::os::unix::fs::MetadataExt;
159+
path.metadata()
160+
.ok()
161+
.map(|metadata| (metadata.dev(), metadata.ino()))
162+
}
163+
164+
// Stable file identities are not available on other platforms, so their watches are always
165+
// re-registered on full rebuilds.
166+
#[cfg(not(unix))]
167+
fn dir_identity(_path: &Path) -> Option<DirIdentity> {
168+
None
169+
}
170+
171+
/// A watch that was registered successfully.
172+
struct RegisteredWatch {
173+
path: PathBuf,
174+
mode: RecursiveMode,
175+
/// The identity of the directory when the watch was registered. Watches can be tied to the
176+
/// directory rather than its path (inotify drops the watch when the directory is deleted), so
177+
/// a watch is only still valid while the path refers to the same directory.
178+
identity: Option<DirIdentity>,
179+
}
180+
181+
impl RegisteredWatch {
182+
fn is_still_valid_for(&self, path: &Path, mode: RecursiveMode) -> bool {
183+
self.path == path
184+
&& self.mode == mode
185+
&& self.identity.is_some()
186+
&& self.identity == dir_identity(path)
187+
}
188+
}
189+
190+
/// Registers the watch paths with the given watcher and returns the ones that were registered
191+
/// successfully. Failed paths are left out so that the next full rebuild retries them.
192+
fn register_watches(
193+
watcher: &mut RecommendedWatcher,
194+
watch_paths: &[(PathBuf, RecursiveMode)],
195+
) -> Vec<RegisteredWatch> {
196+
let mut registered = Vec::with_capacity(watch_paths.len());
155197
for (path, mode) in watch_paths {
156198
let mode_str = if *mode == RecursiveMode::Recursive {
157199
"recursive"
158200
} else {
159201
"non-recursive"
160202
};
161203
log::debug!(" watching ({mode_str}): {}", path.display());
162-
if let Err(e) = watcher.watch(path, *mode) {
163-
log::error!("Could not watch {}: {}", path.display(), e);
204+
// Read the identity before registering: if the directory is replaced in between, the
205+
// recorded identity is stale and the next full rebuild registers the watch again.
206+
let identity = dir_identity(path);
207+
match watcher.watch(path, *mode) {
208+
Ok(()) => registered.push(RegisteredWatch {
209+
path: path.clone(),
210+
mode: *mode,
211+
identity,
212+
}),
213+
Err(e) => log::error!("Could not watch {}: {}", path.display(), e),
164214
}
165215
}
216+
registered
166217
}
167218

168-
/// Unregisters all watch paths from the given watcher.
169-
fn unregister_watches(watcher: &mut RecommendedWatcher, watch_paths: &[(PathBuf, RecursiveMode)]) {
170-
for (path, _) in watch_paths {
171-
let _ = watcher.unwatch(path);
219+
/// Brings the registered watches from `current` to `next`, touching only the paths that changed.
220+
///
221+
/// Some backends restart their event stream on every `watch`/`unwatch` call (FSEvents on macOS
222+
/// does), and events that happen during the restart are lost. Leaving unchanged watches alone
223+
/// avoids those gaps entirely in the common case where a full rebuild keeps the same paths.
224+
/// Watches whose directory was replaced since registration are registered again.
225+
///
226+
/// Returns the watches that are registered afterwards, which excludes paths that failed to register.
227+
fn update_watches(
228+
watcher: &mut RecommendedWatcher,
229+
current: Vec<RegisteredWatch>,
230+
next: &[(PathBuf, RecursiveMode)],
231+
) -> Vec<RegisteredWatch> {
232+
let (mut kept, stale): (Vec<_>, Vec<_>) = current.into_iter().partition(|watch| {
233+
next.iter()
234+
.any(|(path, mode)| watch.is_still_valid_for(path, *mode))
235+
});
236+
for watch in stale {
237+
let _ = watcher.unwatch(&watch.path);
172238
}
239+
let added: WatchPaths = next
240+
.iter()
241+
.filter(|(path, mode)| {
242+
!kept
243+
.iter()
244+
.any(|watch| watch.path == *path && watch.mode == *mode)
245+
})
246+
.cloned()
247+
.collect();
248+
kept.extend(register_watches(watcher, &added));
249+
kept
173250
}
174251

175252
fn carry_forward_compile_state(previous: &BuildCommandState, next: &mut BuildCommandState) {
@@ -250,7 +327,7 @@ fn cleanup_before_watch_exit(
250327

251328
struct AsyncWatchArgs<'a> {
252329
watcher: &'a mut RecommendedWatcher,
253-
current_watch_paths: Vec<(PathBuf, RecursiveMode)>,
330+
current_watches: Vec<RegisteredWatch>,
254331
initial_build_state: BuildCommandState,
255332
q: Arc<FifoQueue<Result<Event, Error>>>,
256333
ctrlc_pressed: Arc<AtomicBool>,
@@ -268,7 +345,7 @@ struct AsyncWatchArgs<'a> {
268345
async fn async_watch(
269346
AsyncWatchArgs {
270347
watcher,
271-
mut current_watch_paths,
348+
mut current_watches,
272349
initial_build_state,
273350
q,
274351
ctrlc_pressed,
@@ -512,9 +589,9 @@ async fn async_watch(
512589
build_state = next_build_state;
513590

514591
// Re-register watches based on the new build state
515-
unregister_watches(watcher, &current_watch_paths);
516-
current_watch_paths = compute_watch_paths(&build_state, path);
517-
register_watches(watcher, &current_watch_paths);
592+
let next_watch_paths = compute_watch_paths(&build_state, path);
593+
current_watches =
594+
update_watches(watcher, std::mem::take(&mut current_watches), &next_watch_paths);
518595

519596
let result = build::incremental_build_without_lock(
520597
&mut build_state,
@@ -547,6 +624,18 @@ async fn async_watch(
547624
}
548625
}
549626
needs_compile_type = CompileType::None;
627+
628+
// The removal event of watch.lock can be lost while watches are re-registered
629+
// (some backends restart their event stream), so check the file itself.
630+
if !path.join("lib").join(LockKind::Watch.file_name()).exists() {
631+
cleanup_before_watch_exit(
632+
path,
633+
&build_state,
634+
show_progress,
635+
"\nExiting... (lockfile removed)",
636+
);
637+
return Ok(());
638+
}
550639
}
551640
CompileType::None => {
552641
// We want to sleep for a little while so the CPU can schedule other work. That way we end
@@ -589,7 +678,7 @@ pub fn start(
589678

590679
// Initialization can clean previous build artifacts, so it has to be serialized
591680
// with the initial compile too.
592-
let (build_state, current_watch_paths, initial_compile_result): StartupBuildResult =
681+
let (build_state, current_watches, initial_compile_result): StartupBuildResult =
593682
build::with_build_lock(path, || {
594683
let mut build_state = build::initialize_build(
595684
None,
@@ -605,8 +694,8 @@ pub fn start(
605694
.with_context(|| "Could not initialize build")?;
606695

607696
// Compute and register targeted watches based on source folders.
608-
let current_watch_paths = compute_watch_paths(&build_state, path);
609-
register_watches(&mut watcher, &current_watch_paths);
697+
let current_watches =
698+
register_watches(&mut watcher, &compute_watch_paths(&build_state, path));
610699

611700
let timing_total = Instant::now();
612701
let initial_compile_result = build::incremental_build_without_lock(
@@ -623,7 +712,7 @@ pub fn start(
623712

624713
Ok::<StartupBuildResult, anyhow::Error>((
625714
build_state,
626-
current_watch_paths,
715+
current_watches,
627716
initial_compile_result,
628717
))
629718
})?;
@@ -648,7 +737,7 @@ pub fn start(
648737

649738
async_watch(AsyncWatchArgs {
650739
watcher: &mut watcher,
651-
current_watch_paths,
740+
current_watches,
652741
initial_build_state: build_state,
653742
q: consumer,
654743
ctrlc_pressed,
@@ -888,4 +977,26 @@ mod tests {
888977
carry_forward_compile_state(&previous, &mut different_source);
889978
assert!(!different_source.get_module("ModuleA").unwrap().compile_dirty);
890979
}
980+
981+
#[cfg(unix)]
982+
#[test]
983+
fn registered_watch_is_invalid_after_directory_is_replaced() {
984+
let temp_dir = tempfile::TempDir::new().expect("temp dir should be created");
985+
let dir = temp_dir.path().join("src");
986+
std::fs::create_dir(&dir).expect("dir should be created");
987+
988+
let watch = RegisteredWatch {
989+
path: dir.clone(),
990+
mode: RecursiveMode::Recursive,
991+
identity: dir_identity(&dir),
992+
};
993+
assert!(watch.is_still_valid_for(&dir, RecursiveMode::Recursive));
994+
assert!(!watch.is_still_valid_for(&dir, RecursiveMode::NonRecursive));
995+
996+
// Move the old directory away instead of deleting it, so that its inode stays in use and
997+
// cannot be reused for the new directory.
998+
std::fs::rename(&dir, temp_dir.path().join("src-old")).expect("dir should be renamed");
999+
std::fs::create_dir(&dir).expect("dir should be recreated");
1000+
assert!(!watch.is_still_valid_for(&dir, RecursiveMode::Recursive));
1001+
}
8911002
}

0 commit comments

Comments
 (0)