Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(project-group): keep a group editable after its folder is renamed externally

liuzixiang130ab04b94a7
diff --git a/apps/desktop/electron/main/ipc/workspace-ipc.ts b/apps/desktop/electron/main/ipc/workspace-ipc.ts
index f2a6e26cb..0c4526614 100644
--- a/apps/desktop/electron/main/ipc/workspace-ipc.ts
+++ b/apps/desktop/electron/main/ipc/workspace-ipc.ts
@@ -326,11 +326,10 @@ export function registerWorkspaceIpc({
.map((path) => resolve(path)),
),
];
- if (safeFolders.some((path) => !existsSync(path) || !statSync(path).isDirectory())) {
- throw Object.assign(new Error("project group folders must be directories"), {
- errorCode: ErrorCodes.INVALID_ARGUMENT,
- });
- }
+ // No blanket existence pre-check here: the host validates the submitted
+ // roots (folders new to the group must exist on disk) while roots the
+ // group already owns may have been renamed or unmounted externally and
+ // must stay editable so the group can be healed (#1869).
// The promoted primary must be one of the folders this call keeps.
const safePrimary = primary ? resolve(primary) : "";
if (safePrimary && !safeFolders.includes(safePrimary)) {
diff --git a/apps/desktop/test/project-edit.test.mjs b/apps/desktop/test/project-edit.test.mjs
index b8c5980b6..e1af2d0ae 100644
--- a/apps/desktop/test/project-edit.test.mjs
+++ b/apps/desktop/test/project-edit.test.mjs
@@ -84,6 +84,23 @@ test("project group update is available through the host RPC and preload API", (
assert.match(ipcSource, /\.\.\.\(safePrimary \? \{ primary: safePrimary \} : \{\}\)/);
});
+test("only project group creation pre-checks folder existence here", () => {
+ // #1869: an update must reach the host even when a stored root is missing
+ // on disk (externally renamed/unmounted); the host validates new roots and
+ // exempts roots the group already owns. Creation still pre-checks because
+ // every submitted folder is new there.
+ const updateHandler = ipcSource.slice(
+ ipcSource.indexOf("IPC.invoke.projectGroupUpdate,"),
+ ipcSource.indexOf("IPC.invoke.projectGroupRename,"),
+ );
+ const createHandler = ipcSource.slice(
+ ipcSource.indexOf("IPC.invoke.projectGroupCreate,"),
+ ipcSource.indexOf("IPC.invoke.projectGroupUpdate,"),
+ );
+ assert.doesNotMatch(updateHandler, /must be directories/);
+ assert.match(createHandler, /project group folders must be directories/);
+});
+
for (const [locale, source] of [["en", enSource], ["zh-CN", zhSource], ["tr", trSource]]) {
test(`project edit labels are present in ${locale}`, () => {
for (const key of [
diff --git a/crates/host-core/src/db/project_groups.rs b/crates/host-core/src/db/project_groups.rs
index 0be9c6f74..5de81f6f2 100644
--- a/crates/host-core/src/db/project_groups.rs
+++ b/crates/host-core/src/db/project_groups.rs
@@ -367,7 +367,12 @@ impl Database {
for raw in paths {
let path = canonical_project_path(raw)
.ok_or_else(|| anyhow!("project group root path must not be blank"))?;
- if !Path::new(&path).is_dir() {
+ // A root this group already owns may have been renamed or unmounted
+ // externally since it was stored; editing the group must still be
+ // able to keep, reorder, or drop it (#1869). Only folders that are
+ // new to the group must exist on disk.
+ let is_current_root = current.roots.iter().any(|root| root.path == path);
+ if !is_current_root && !Path::new(&path).is_dir() {
return Err(anyhow!("project group root is not a directory: {path}"));
}
if selected.iter().any(|candidate| candidate == &path) {
diff --git a/crates/host-core/src/db/tests.rs b/crates/host-core/src/db/tests.rs
index 74e57989a..f710de61e 100644
--- a/crates/host-core/src/db/tests.rs
+++ b/crates/host-core/src/db/tests.rs
@@ -1820,6 +1820,76 @@ fn project_group_update_switches_the_primary_root() {
assert_eq!(unchanged.primary_path, canonical_member);
}
+#[test]
+fn project_group_update_allows_a_root_renamed_away_externally() {
+ // #1869: when a group folder is renamed (or unmounted) outside the app,
+ // editing the group must still be able to keep the stale root while its
+ // replacement is added, and must be able to drop the stale root afterwards.
+ let dir = tempfile::tempdir().unwrap();
+ let stale = dir.path().join("stale");
+ let replacement = dir.path().join("replacement");
+ std::fs::create_dir_all(&stale).unwrap();
+ std::fs::create_dir_all(&replacement).unwrap();
+ let db = Database::open(&dir.path().join("pi.sqlite")).unwrap();
+ let group = db
+ .create_project_group("Renamed away", &[stale.to_string_lossy().into()])
+ .unwrap();
+ let canonical_stale = crate::db::canonical_project_path(&stale.to_string_lossy()).unwrap();
+ let canonical_replacement =
+ crate::db::canonical_project_path(&replacement.to_string_lossy()).unwrap();
+
+ // The folder is renamed externally after the group stored it.
+ std::fs::remove_dir_all(&stale).unwrap();
+
+ // Keeping the stale root while adding and promoting its replacement must
+ // not be blocked by the on-disk directory check. The submission uses the
+ // stored spelling, exactly as the edit dialog echoes the root back.
+ let updated = db
+ .update_project_group(
+ &group.id,
+ "Renamed away",
+ &[
+ replacement.to_string_lossy().into(),
+ canonical_stale.clone(),
+ ],
+ Some(&canonical_replacement),
+ )
+ .expect("a root the group already owns may be missing on disk");
+ assert_eq!(updated.primary_path, canonical_replacement);
+ assert_eq!(updated.roots.len(), 2);
+ assert_eq!(updated.roots[0].path, canonical_replacement);
+ assert_eq!(updated.roots[1].path, canonical_stale);
+
+ // A later edit can drop the stale root entirely, healing the group.
+ let healed = db
+ .update_project_group(
+ &group.id,
+ "Renamed away",
+ &[replacement.to_string_lossy().into()],
+ Some(&canonical_replacement),
+ )
+ .expect("the stale root can be dropped from the group");
+ assert_eq!(healed.roots.len(), 1);
+ assert_eq!(healed.roots[0].path, canonical_replacement);
+ assert!(healed.detached_paths.contains(&canonical_stale));
+
+ // A folder that is new to the group must still exist on disk.
+ let missing = dir.path().join("missing");
+ assert!(db
+ .update_project_group(
+ &group.id,
+ "Renamed away",
+ &[
+ replacement.to_string_lossy().into(),
+ missing.to_string_lossy().into(),
+ ],
+ Some(&canonical_replacement),
+ )
+ .unwrap_err()
+ .to_string()
+ .contains("not a directory"));
+}
+
#[test]
fn remove_project_from_group_detaches_even_with_chats_and_may_move_primary() {
let dir = tempfile::tempdir().unwrap();