Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 57 additions & 3 deletions crates/devstack-daemon/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
Expand All @@ -59,7 +62,8 @@ fn set(patterns: &[String]) -> Option<GlobSet> {
/// 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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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),
] {
Expand All @@ -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) {
Expand Down
32 changes: 30 additions & 2 deletions crates/devstack/tests/daemon_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -2188,11 +2189,38 @@ 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. 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
// 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"]);
}

Expand Down
3 changes: 2 additions & 1 deletion docs/stack-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down