Skip to content

Commit 010c1ae

Browse files
cknittclaude
andcommitted
Re-register watches dropped by an overlapping unwatch
On Linux, unwatching a recursive source folder also dropped separately watched folders below it, so rewatch stopped watching them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BC5F5QPeQh5cHBjGBnvJ2T Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 8d51b9c commit 010c1ae

2 files changed

Lines changed: 217 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Rewatch: keep watching a source folder on Linux when an overlapping recursive source folder is removed from `rescript.json`. https://github.com/rescript-lang/rescript/pull/XXXX

‎rewatch/src/watcher.rs‎

Lines changed: 216 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,40 @@ fn register_watches(
221221
/// Some backends restart their event stream on every `watch`/`unwatch` call (FSEvents on macOS
222222
/// does), and events that happen during the restart are lost. Leaving unchanged watches alone
223223
/// 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.
224+
/// Watches whose directory was replaced since registration are registered again, and so are
225+
/// watches that unwatching a stale watch can drop (see `unwatch_can_drop`).
225226
///
226227
/// Returns the watches that are registered afterwards, which excludes paths that failed to register.
227228
fn update_watches(
228229
watcher: &mut RecommendedWatcher,
229230
current: Vec<RegisteredWatch>,
230231
next: &[(PathBuf, RecursiveMode)],
231232
) -> 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-
});
233+
let mut is_stale: Vec<bool> = current
234+
.iter()
235+
.map(|watch| {
236+
!next
237+
.iter()
238+
.any(|(path, mode)| watch.is_still_valid_for(path, *mode))
239+
})
240+
.collect();
241+
let watched: Vec<(&Path, RecursiveMode)> = current
242+
.iter()
243+
.map(|watch| (watch.path.as_path(), watch.mode))
244+
.collect();
245+
mark_watches_dropped_by_unwatch(&watched, &mut is_stale);
246+
247+
let mut kept = Vec::with_capacity(current.len());
248+
let mut stale = Vec::new();
249+
for (watch, is_stale) in current.into_iter().zip(is_stale) {
250+
if is_stale {
251+
stale.push(watch);
252+
} else {
253+
kept.push(watch);
254+
}
255+
}
256+
// Unwatch everything stale before registering anything, so that no unwatch drops a watch
257+
// registered in this update.
236258
for watch in stale {
237259
let _ = watcher.unwatch(&watch.path);
238260
}
@@ -249,6 +271,43 @@ fn update_watches(
249271
kept
250272
}
251273

274+
/// Whether unwatching `removed` can also drop the watch of `other`.
275+
///
276+
/// Watches of overlapping paths are not independent. The inotify backend on Linux (notify 5)
277+
/// keeps one watch per directory, so a recursive watch of `src` and a separate watch of
278+
/// `src/retained` share the watch of `src/retained`:
279+
/// - Unwatching a recursive path drops every watch at or below it, including separately
280+
/// registered ones. Whether notify considers a path recursive also depends on the order in
281+
/// which overlapping paths were registered, so any watch below `removed` counts as dropped.
282+
/// - Unwatching a path inside a recursive watch drops that part of the recursive watch.
283+
fn unwatch_can_drop(removed: &Path, other: &Path, other_mode: RecursiveMode) -> bool {
284+
other.starts_with(removed) || (other_mode == RecursiveMode::Recursive && removed.starts_with(other))
285+
}
286+
287+
/// Marks every watch that unwatching the stale watches can drop as stale as well, so that it is
288+
/// unwatched and registered again. Repeats until nothing changes, because unwatching a newly
289+
/// marked watch can drop further ones.
290+
fn mark_watches_dropped_by_unwatch(watches: &[(&Path, RecursiveMode)], is_stale: &mut [bool]) {
291+
let mut changed = true;
292+
while changed {
293+
changed = false;
294+
for i in 0..watches.len() {
295+
if is_stale[i] {
296+
continue;
297+
}
298+
let (path, mode) = watches[i];
299+
let dropped = watches
300+
.iter()
301+
.zip(is_stale.iter())
302+
.any(|((removed, _), stale)| *stale && unwatch_can_drop(removed, path, mode));
303+
if dropped {
304+
is_stale[i] = true;
305+
changed = true;
306+
}
307+
}
308+
}
309+
}
310+
252311
fn carry_forward_compile_state(previous: &BuildCommandState, next: &mut BuildCommandState) {
253312
for (module_name, next_module) in next.build_state.modules.iter_mut() {
254313
let Some(previous_module) = previous.build_state.modules.get(module_name) else {
@@ -999,4 +1058,156 @@ mod tests {
9991058
std::fs::create_dir(&dir).expect("dir should be recreated");
10001059
assert!(!watch.is_still_valid_for(&dir, RecursiveMode::Recursive));
10011060
}
1061+
1062+
#[test]
1063+
fn marks_watches_that_an_unwatch_can_drop_as_stale() {
1064+
use RecursiveMode::{NonRecursive, Recursive};
1065+
1066+
// A registered watch with whether it is stale before propagation.
1067+
type Watch = (&'static str, RecursiveMode, bool);
1068+
// Each case lists the registered watches and the expected stale flags after propagation.
1069+
let cases: Vec<(Vec<Watch>, Vec<bool>)> = vec![
1070+
// Kept non-recursive child of a removed recursive parent.
1071+
(
1072+
vec![
1073+
("/p/src", Recursive, true),
1074+
("/p/src/retained", NonRecursive, false),
1075+
],
1076+
vec![true, true],
1077+
),
1078+
// Kept recursive child of a removed recursive parent.
1079+
(
1080+
vec![("/p/src", Recursive, true), ("/p/src/retained", Recursive, false)],
1081+
vec![true, true],
1082+
),
1083+
// Removed child of a kept recursive parent.
1084+
(
1085+
vec![
1086+
("/p/src", Recursive, false),
1087+
("/p/src/retained", NonRecursive, true),
1088+
],
1089+
vec![true, true],
1090+
),
1091+
// Removed child of a kept non-recursive parent (the package root) leaves it alone.
1092+
(
1093+
vec![("/p", NonRecursive, false), ("/p/src", Recursive, true)],
1094+
vec![false, true],
1095+
),
1096+
// The package root turning from recursive to non-recursive drops everything below it.
1097+
(
1098+
vec![
1099+
("/p", Recursive, true),
1100+
("/p/src", NonRecursive, false),
1101+
("/p/lib", NonRecursive, false),
1102+
],
1103+
vec![true, true, true],
1104+
),
1105+
// Unrelated and sibling paths are not affected, even with a common name prefix.
1106+
(
1107+
vec![
1108+
("/p/src", Recursive, true),
1109+
("/p/src2", NonRecursive, false),
1110+
("/p/test", Recursive, false),
1111+
],
1112+
vec![true, false, false],
1113+
),
1114+
// Propagates: src/a is re-registered because src/a/b is inside it, and unwatching
1115+
// src/a drops src/a/c as well.
1116+
(
1117+
vec![
1118+
("/p/src/a/b", NonRecursive, true),
1119+
("/p/src/a/c", NonRecursive, false),
1120+
("/p/src/a", Recursive, false),
1121+
],
1122+
vec![true, true, true],
1123+
),
1124+
];
1125+
1126+
for (watches, expected) in cases {
1127+
let paths: Vec<(&Path, RecursiveMode)> = watches
1128+
.iter()
1129+
.map(|(path, mode, _)| (Path::new(*path), *mode))
1130+
.collect();
1131+
let mut is_stale: Vec<bool> = watches.iter().map(|(_, _, stale)| *stale).collect();
1132+
mark_watches_dropped_by_unwatch(&paths, &mut is_stale);
1133+
assert_eq!(is_stale, expected, "watches: {watches:?}");
1134+
}
1135+
}
1136+
1137+
/// Registers `before` with a real watcher, updates the watches to `after`, and checks that a
1138+
/// file written to `write_dir` afterwards is reported. Paths are relative to a temp dir, and
1139+
/// `""` is the temp dir itself.
1140+
#[cfg(target_os = "linux")]
1141+
fn assert_update_keeps_watching(
1142+
before: &[(&str, RecursiveMode)],
1143+
after: &[(&str, RecursiveMode)],
1144+
write_dir: &str,
1145+
) {
1146+
use std::sync::mpsc;
1147+
1148+
let temp_dir = tempfile::TempDir::new().expect("temp dir should be created");
1149+
let root = temp_dir.path();
1150+
let to_watch_paths = |paths: &[(&str, RecursiveMode)]| -> WatchPaths {
1151+
paths
1152+
.iter()
1153+
.map(|(path, mode)| (root.join(path), *mode))
1154+
.collect()
1155+
};
1156+
for (path, _) in to_watch_paths(before).iter().chain(&to_watch_paths(after)) {
1157+
std::fs::create_dir_all(path).expect("dir should be created");
1158+
}
1159+
std::fs::create_dir_all(root.join(write_dir)).expect("dir should be created");
1160+
1161+
let (tx, rx) = mpsc::channel();
1162+
let mut watcher = RecommendedWatcher::new(tx, Config::default()).expect("watcher should start");
1163+
let current = register_watches(&mut watcher, &to_watch_paths(before));
1164+
let registered = update_watches(&mut watcher, current, &to_watch_paths(after));
1165+
assert_eq!(registered.len(), after.len());
1166+
1167+
let file = root.join(write_dir).join("Demo.res");
1168+
std::fs::write(&file, "let x = 2").expect("file should be written");
1169+
let deadline = Instant::now() + Duration::from_secs(5);
1170+
loop {
1171+
let remaining = deadline.saturating_duration_since(Instant::now());
1172+
match rx.recv_timeout(remaining) {
1173+
Ok(Ok(event)) if event.paths.contains(&file) => return,
1174+
Ok(_) => continue,
1175+
Err(_) => panic!(
1176+
"no event for {} after updating watches from {before:?} to {after:?}",
1177+
file.display()
1178+
),
1179+
}
1180+
}
1181+
}
1182+
1183+
#[cfg(target_os = "linux")]
1184+
#[test]
1185+
fn keeps_watching_after_an_overlapping_watch_is_removed() {
1186+
use RecursiveMode::{NonRecursive, Recursive};
1187+
1188+
assert_update_keeps_watching(
1189+
&[
1190+
("", NonRecursive),
1191+
("src", Recursive),
1192+
("src/retained", NonRecursive),
1193+
],
1194+
&[("", NonRecursive), ("src/retained", NonRecursive)],
1195+
"src/retained",
1196+
);
1197+
assert_update_keeps_watching(
1198+
&[("src", Recursive), ("src/retained", Recursive)],
1199+
&[("src/retained", Recursive)],
1200+
"src/retained/nested",
1201+
);
1202+
assert_update_keeps_watching(
1203+
&[("src", Recursive), ("src/retained", NonRecursive)],
1204+
&[("src", Recursive)],
1205+
"src/retained",
1206+
);
1207+
assert_update_keeps_watching(
1208+
&[("", Recursive), ("src", NonRecursive)],
1209+
&[("", NonRecursive), ("src", NonRecursive)],
1210+
"src",
1211+
);
1212+
}
10021213
}

0 commit comments

Comments
 (0)