From f88f75b9cd32e974b2cbbeaba4dd7efb3c1c1f1d Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Thu, 26 Nov 2026 15:53:33 +0100 Subject: [PATCH] fix(build): a directory gone from the worktree goes from the lane's copy (#628) The snapshot's sync removed the files of a directory renamed or removed in the worktree, but kept the directory, empty: a service that reads one folder per entry saw a phantom one. Now a directory the removal leaves empty, and the source no longer has, goes too; one that holds anything else (a build's output) stays. Covers a lane here, a lane on a node and the snapshot builds run in. Bug-Class: stale-copy Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 ++++ crates/devstack-build/src/snapshot.rs | 52 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0bb563e..4e1c56d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,13 @@ and listed with their assets on the ### Fixed +- **A directory renamed or removed in a worktree goes from the lane's copy** + (#628). The copy removed the files but kept their directories, empty: a + service that reads one folder per entry saw a phantom one. Now a directory + the removal leaves empty, and the worktree no longer has, goes too; one + that holds anything else (a build's output) stays. The same holds for a + lane on a node and for the snapshot builds run in. Empty directories a + copy kept before this release stay until the copy is made anew. - **A stopped stack no longer shows the memory of its containers.** When the last container stopped, the daemon kept its last load, so a group and the lane card showed gigabytes after `down` (and the lane's estimate for diff --git a/crates/devstack-build/src/snapshot.rs b/crates/devstack-build/src/snapshot.rs index a1ef290f..3a14ea50 100644 --- a/crates/devstack-build/src/snapshot.rs +++ b/crates/devstack-build/src/snapshot.rs @@ -148,6 +148,7 @@ impl Snapshot { .filter(|k| !listed.contains(k.as_str())) { let _ = fs::remove_file(self.dir.join(gone)); + self.remove_emptied(gone); removed += 1; } // A first sync has no watcher to wait for: nothing runs from the @@ -221,6 +222,21 @@ impl Snapshot { }) } + /// The directories of `rel`, a file just removed from the copy, that it + /// left empty and the source no longer has (#628): a directory renamed or + /// removed in the worktree goes from the copy too, where a service that + /// reads one folder per entry would see it still. One that holds + /// anything else (a build's output) stays. + fn remove_emptied(&self, rel: &str) { + let mut parent = Path::new(rel).parent(); + while let Some(dir) = parent.filter(|d| !d.as_os_str().is_empty()) { + if self.source.join(dir).is_dir() || fs::remove_dir(self.dir.join(dir)).is_err() { + return; + } + parent = dir.parent(); + } + } + /// Clear the way for the copy of `rel`: a file where one of its parent /// directories goes, or a directory where the file goes, is what an /// earlier sync left of a path that changed its type. @@ -413,6 +429,42 @@ mod tests { assert_eq!(snap.list().unwrap(), vec!["devstack.toml", "src/a/x.cs"]); } + /// #628: a directory renamed or removed in the worktree goes from the + /// copy too; a service that reads one folder per entry saw it still. + /// What the copy holds beyond the snapshot (a build's output) stays. + #[test] + fn a_directory_gone_from_the_source_goes_from_the_copy() { + let src = tempfile::tempdir().unwrap(); + let a = src.path().join("org/agents/a/v1"); + std::fs::create_dir_all(&a).unwrap(); + std::fs::write(a.join("agent.yaml"), "a").unwrap(); + std::fs::write(a.join("prompt.md"), "a").unwrap(); + std::fs::create_dir_all(src.path().join("org/tools")).unwrap(); + std::fs::write(src.path().join("org/tools/t.yaml"), "t").unwrap(); + let state = tempfile::tempdir().unwrap(); + let mut snap = super::Snapshot::open(src.path(), state.path()).unwrap(); + snap.sync().unwrap(); + // Written in the copy by a build: not the snapshot's. + std::fs::create_dir_all(snap.dir.join("org/tools/bin")).unwrap(); + std::fs::write(snap.dir.join("org/tools/bin/out.dll"), "built").unwrap(); + + // `git mv org/agents/a org/agents/b`, and the tools' file removed. + std::fs::rename( + src.path().join("org/agents/a"), + src.path().join("org/agents/b"), + ) + .unwrap(); + std::fs::remove_file(src.path().join("org/tools/t.yaml")).unwrap(); + snap.sync().unwrap(); + + assert!(snap.dir.join("org/agents/b/v1/agent.yaml").is_file()); + assert!( + !snap.dir.join("org/agents/a").exists(), + "the renamed directory stayed in the copy" + ); + assert!(snap.dir.join("org/tools/bin/out.dll").is_file()); + } + /// A watcher like `@parcel/watcher` on Linux (Angular's dev server, /// #507): inotify on each directory, a new directory watched when its /// creation is seen in a watched one, never by reading it. Every