From 9541356ce3f3bb1f3b1282360740e17d464491e1 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 6 Nov 2026 13:08:52 +0100 Subject: [PATCH 1/2] fix: a folder's own "modified" event is no change; __pycache__ restarts nothing (#463) On Windows ReadDirectoryChangesW reports a folder as modified when an entry is added to or removed from it, beside the entry's own event. Python creating `__pycache__` in `shared` therefore restarted every dev server with `restart_on = ["shared"]`, although `__pycache__` itself never counts. The watcher now drops a modification event whose path is a directory (a rename keeps its own kind and still counts); what changed in the folder comes with its own path. Bug-Class: an event about a container taken as a change of its contents, bypassing the filter on the contents Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 +++++ crates/devstack-daemon/src/watch.rs | 60 +++++++++++++++++++++-- crates/devstack/tests/daemon_lifecycle.rs | 18 ++++++- docs/stack-file.md | 3 +- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 353b2358..e1909e06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,19 @@ and listed with their assets on the file is named as such. `-f` is taken as given, and `devstack init` still writes into the current folder. +### Fixed + +- **Python creating `__pycache__` no longer restarts a dev server** (#463). + On Windows the OS reports a folder as modified when an entry appears in + it, beside the entry's own event; with a folder in `restart_on` + (`restart_on = ["shared"]`), the first start of a Python service, which + creates `__pycache__` in each imported package, restarted every dev + server naming the folder once or twice, and a job they wait for ran + again. A folder's own "modified" event no longer counts as a change: what + appeared or went in it is reported by its own path, and a generated + directory never counts. The same held for `watch` (a candidate) and a + lane's copy. + ## [0.41.0] - 2026-11-05 A stack's daemon now ends with its last tree (ADR-0095): every tree, diff --git a/crates/devstack-daemon/src/watch.rs b/crates/devstack-daemon/src/watch.rs index 916a843d..c0366ab2 100644 --- a/crates/devstack-daemon/src/watch.rs +++ b/crates/devstack-daemon/src/watch.rs @@ -13,7 +13,9 @@ //! `.git`, `.devstack`, and the caches of frontend tools and Python: //! `.angular`, `.vite`, `.next`, `__pycache__`, ...): a build in the //! repository, or a dev server as it starts, would otherwise see its own -//! output as a change. They are not even looked at: an event is judged on its +//! output as a change, nor does the folder that holds one (Windows reports the +//! folder as modified when an entry appears in it, #463). They are not even +//! looked at: an event is judged on its //! path alone, before the file system is asked anything about it. A package //! manager creates thousands of directories in `node_modules` in a burst, and //! a watcher that opens each one as it appears races the tool: on Windows @@ -33,6 +35,7 @@ use crate::supervisor::Supervisor; use devstack_config::model::Kind; use globset::GlobSet; +use notify::event::ModifyKind; use notify::{EventKind, RecursiveMode, Watcher as _}; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; @@ -59,7 +62,8 @@ fn set(patterns: &[String]) -> Option { /// stamps changed) for some creations and deletions, depending on the tool /// that made them, and never for a saved file. So the restart came with /// whichever later event happened to name the folder: a second, minutes or -/// the next change later (#221). +/// the next change later (#221). That event of the folder itself counts for +/// nothing now ([`event_paths`], #463). /// /// For `restart_on` only: a restart reads the working tree, so a folder means /// what is in it. `watch` also selects a build's input files @@ -239,11 +243,22 @@ fn event_paths(repo: &Path, ev: notify::Event, fs: &dyn Fs, list_new_dirs: bool) return Vec::new(); } let list = list_new_dirs && matches!(ev.kind, EventKind::Create(_)); + // A folder "modified": Windows says so when an entry is added or removed + // (its time stamps change), beside the entry's own event. That event says + // what changed, and an entry that does not count (`__pycache__`) must + // not count through its folder (#463). A rename keeps its own kind. + let touched = matches!( + ev.kind, + EventKind::Modify(ModifyKind::Any | ModifyKind::Data(_) | ModifyKind::Metadata(_)) + ); let mut out = Vec::new(); for path in ev.paths { let Some(rel) = counted(repo, &path) else { continue; }; + if touched && fs.is_dir(&path) { + continue; + } out.push(rel); if list && fs.is_dir(&path) { let mut dirs = vec![path]; @@ -1081,7 +1096,6 @@ mod tests { fn only_a_creation_lists_a_directory() { let fs = Spy::new(&[("/r/src/new", &[("/r/src/new/a.cs", false)])]); for kind in [ - EventKind::Modify(ModifyKind::Any), EventKind::Modify(ModifyKind::Name(RenameMode::To)), EventKind::Remove(RemoveKind::Folder), ] { @@ -1093,6 +1107,46 @@ mod tests { assert!(fs.asked.borrow().is_empty(), "{:?}", fs.asked); } + /// Windows reports a folder as modified when an entry is added to it or + /// removed from it, beside the entry's own event (#463: Python creating + /// `__pycache__` in `shared` restarted every dev server whose + /// `restart_on` named `shared`). The folder's event is no change of its + /// own; the entry's says what changed, and a generated one does not + /// count. A file's modification, and a folder created, renamed in or + /// removed, still count. + #[test] + fn a_folder_reported_as_modified_is_no_change_of_its_own() { + let fs = Spy::new(&[("/r/shared", &[("/r/shared/__pycache__", true)])]); + for kind in [ + ModifyKind::Any, + ModifyKind::Metadata(notify::event::MetadataKind::WriteTime), + ModifyKind::Data(notify::event::DataChange::Any), + ] { + let ev = event(EventKind::Modify(kind), "/r/shared"); + assert!( + event_paths(Path::new("/r"), ev, &fs, false).is_empty(), + "{kind:?}" + ); + let ev = event(EventKind::Modify(kind), "/r/shared/a.py"); + assert_eq!( + event_paths(Path::new("/r"), ev, &fs, false), + ["shared/a.py"] + ); + } + for kind in [ + EventKind::Create(CreateKind::Any), + EventKind::Modify(ModifyKind::Name(RenameMode::To)), + EventKind::Modify(ModifyKind::Name(RenameMode::From)), + EventKind::Remove(RemoveKind::Any), + ] { + let got = event_paths(Path::new("/r"), event(kind, "/r/shared"), &fs, false); + assert_eq!(got, ["shared"], "{kind:?}"); + } + // The entry itself, the other half of what Windows reports. + let ev = event(EventKind::Create(CreateKind::Any), "/r/shared/__pycache__"); + assert!(event_paths(Path::new("/r"), ev, &fs, false).is_empty()); + } + /// A link to the directory `target`, at `link`: a symlink, or a junction /// on Windows (which needs no privilege, and is what `pnpm` makes). fn link_dir(target: &Path, link: &Path) { diff --git a/crates/devstack/tests/daemon_lifecycle.rs b/crates/devstack/tests/daemon_lifecycle.rs index e3e56a56..8dc38670 100644 --- a/crates/devstack/tests/daemon_lifecycle.rs +++ b/crates/devstack/tests/daemon_lifecycle.rs @@ -2113,6 +2113,7 @@ fn restart_on_a_folder_restarts_for_each_file_in_it() { let s = Stack::new(false); let server = s.dir.path().join("src/server"); std::fs::create_dir_all(server.join("__pycache__")).unwrap(); + std::fs::create_dir_all(server.join("pkg")).unwrap(); std::fs::write( s.file(), format!( @@ -2188,11 +2189,24 @@ fn restart_on_a_folder_restarts_for_each_file_in_it() { "a change during a restart restarted it again\n{}", s.diag() ); - assert!(up(old), "{}", s.diag()); + // Up again after that restart, not the one before it: a change while it + // runs would wait for it and fire after the check below. + let restarted = pid(); + assert!(up(restarted), "{}", s.diag()); let before = fired().len(); std::fs::write(server.join("__pycache__/app.cpython-313.pyc"), "x").unwrap(); + // A new `__pycache__`, as a first start of Python makes it: Windows + // reports its folder as modified as well (#463). + let pkg = server.join("pkg"); + std::fs::create_dir(pkg.join("__pycache__")).unwrap(); + std::fs::write(pkg.join("__pycache__/mod.cpython-313.pyc"), "x").unwrap(); std::thread::sleep(Duration::from_secs(4)); - assert_eq!(fired().len(), before, "a compiled module restarts nothing"); + assert_eq!( + fired().len(), + before, + "a compiled module, or a new __pycache__, restarts nothing\n{}", + s.diag() + ); s.ok(&["down"]); } diff --git a/docs/stack-file.md b/docs/stack-file.md index d478151d..98d71d70 100644 --- a/docs/stack-file.md +++ b/docs/stack-file.md @@ -352,7 +352,8 @@ Generated directories never count as changes: `bin`, `obj`, `node_modules`, `target`, `.git`, `.devstack`, the caches of frontend tools (`.angular`, `.vite`, `.next`, `.nuxt`, `.turbo`, `.cache`, `.svelte-kit`, `.parcel-cache`) and of Python (`__pycache__`, `.pytest_cache`, -`.mypy_cache`, `.ruff_cache`), even where `.gitignore` does not list them. The watcher +`.mypy_cache`, `.ruff_cache`), even where `.gitignore` does not list them, +and neither does the folder that holds one when one is created in it. The watcher does not open them either, so a package install running in one is never held up by devstack. From ef3f975e9a63cdcc167bb6bcf95359e1e5606e21 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 6 Nov 2026 13:26:33 +0100 Subject: [PATCH 2/2] test: wait for the last restart by the log, not by a pid read too late The pid read after the change during a restart could already be the restarted process; up() then waited 60 s for one that never came (seen on macos-15). One start, then one run per restart: counted in the daemon log. Co-Authored-By: Claude Opus 5.5 --- crates/devstack/tests/daemon_lifecycle.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/devstack/tests/daemon_lifecycle.rs b/crates/devstack/tests/daemon_lifecycle.rs index 8dc38670..ee426896 100644 --- a/crates/devstack/tests/daemon_lifecycle.rs +++ b/crates/devstack/tests/daemon_lifecycle.rs @@ -2190,9 +2190,23 @@ fn restart_on_a_folder_restarts_for_each_file_in_it() { s.diag() ); // Up again after that restart, not the one before it: a change while it - // runs would wait for it and fire after the check below. - let restarted = pid(); - assert!(up(restarted), "{}", s.diag()); + // runs would wait for it and fire after the check below. Counted in the + // log (one start, then one run per restart): a pid read here may already + // be the restarted one. + let runs = || { + s.daemon_log() + .lines() + .filter(|l| l.contains("\"event\":\"service\"") && l.contains("\"state\":\"running\"")) + .count() + }; + assert!( + wait_for( + || runs() > fired().len() && s.state("dev") == "running", + Duration::from_secs(60) + ), + "{}", + s.diag() + ); let before = fired().len(); std::fs::write(server.join("__pycache__/app.cpython-313.pyc"), "x").unwrap(); // A new `__pycache__`, as a first start of Python makes it: Windows