From 077d66bee48f8fbb0823d573d73c4520685d305a Mon Sep 17 00:00:00 2001 From: Shivakumar Date: Fri, 24 Jul 2026 23:48:54 +0530 Subject: [PATCH] fix(worktree): clear genuine ACL denials on orphaned worktree cleanup remove_worktree_dir's existing retry loop and cmd /c rmdir fallback both only clear a transient in-use lock (e.g. item #302's rust-analyzer case). Neither touches a genuine ACL denial, which is item #267's actual failure mode: cargo's own target/*/.fingerprint/* files can end up ACL-restricted, not merely open, and no amount of retrying clears that. Adds one more fallback before giving up: icacls /grant :F /T resets ownership access recursively, then one final remove_dir_all attempt. No-op (and thus never destructive) when the real problem was actually an in-use lock the earlier retries already cleared. Considered switching to git-parsec's shared_cache symlink strategy instead (share target/ across worktrees so there's nothing per-worktree to get ACL-locked in the first place) -- rejected: this codebase already hit and fixed the exact correctness bug that would reintroduce (item #139, cargo #12516/#14053/#7740 -- a shared CARGO_TARGET_DIR's fingerprint hash omits the worktree path, so two worktrees of different branches silently reuse each other's stale local crate artifacts). isolate_worktree_target_dir's per-worktree isolation is deliberate, not an oversight; sccache (item #133) already covers the safe part of cross-worktree cache sharing (registry deps, hash-keyed). Agentflare-Agent: claude-code_2-1-219_agent Agentflare-Branch: task/267 Agentflare-Item: 267 --- crates/flare-git-core/src/worktree.rs | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/crates/flare-git-core/src/worktree.rs b/crates/flare-git-core/src/worktree.rs index 0a812f7f5..bcac80760 100644 --- a/crates/flare-git-core/src/worktree.rs +++ b/crates/flare-git-core/src/worktree.rs @@ -610,6 +610,34 @@ fn remove_worktree_dir(path: &Path, name: &str) -> bool { return true; } + // `rmdir`/`remove_dir_all` both fail identically against a genuine + // ACL denial, not just a transient in-use lock -- item #267's actual + // failure mode: cargo's own `target/*/.fingerprint/*` files can end + // up ACL-restricted (not merely open), which no amount of retrying + // or an in-use-lock-only tool like `rmdir` can clear. `icacls /grant + // ... /T` resets ownership access recursively before one final + // delete attempt; a no-op (and thus never destructive) if the real + // problem was actually an in-use lock the retries above already + // would have cleared. + if let Ok(user) = std::env::var("USERNAME") { + let icacls_args: Vec = vec![ + path.to_string_lossy().to_string(), + "/grant".to_string(), + format!("{user}:F"), + "/T".to_string(), + "/C".to_string(), + "/Q".to_string(), + ]; + let _ = std::process::Command::new("icacls") + .args(&icacls_args) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status(); + if std::fs::remove_dir_all(path).is_ok() { + return true; + } + } + for handle_exe in &["handle64.exe", "handle.exe"] { if let Ok(output) = std::process::Command::new(handle_exe) .args(["-accepteula", "-nobanner", &path.to_string_lossy()]) @@ -665,6 +693,38 @@ mod tests { assert!(!dir.exists()); } + #[cfg(windows)] + #[test] + fn remove_worktree_dir_clears_a_genuine_acl_denial() { + // item #267's actual failure mode: cargo's own target/*/.fingerprint/* + // files can end up ACL-restricted, not just transiently open. Neither + // the retry loop nor `cmd /c rmdir` clears a real ACL deny -- only + // `icacls /grant ... /T` does. + let tmp = TempDir::new().unwrap(); + let dir = tmp.path().join("wt"); + std::fs::create_dir_all(&dir).unwrap(); + let locked_file = dir.join("locked.txt"); + std::fs::write(&locked_file, b"hello").unwrap(); + + let user = std::env::var("USERNAME").unwrap(); + let deny_args: Vec = vec![ + locked_file.to_string_lossy().to_string(), + "/deny".to_string(), + format!("{user}:(D)"), + ]; + let status = std::process::Command::new("icacls") + .args(&deny_args) + .status() + .unwrap(); + assert!(status.success(), "test setup: icacls /deny must succeed"); + + assert!( + remove_worktree_dir(&dir, "test"), + "must clear the ACL denial via icacls /grant, not just retry" + ); + assert!(!dir.exists()); + } + #[test] fn remove_worktree_dir_retries_past_a_transient_lock() { // On Windows, an open file handle blocks remove_dir_all with