Skip to content

fix(core): update stale project worktree when directory is moved - #38682

Closed
cyrasafia wants to merge 2 commits into
anomalyco:devfrom
cyrasafia:stale-project-path
Closed

fix(core): update stale project worktree when directory is moved#38682
cyrasafia wants to merge 2 commits into
anomalyco:devfrom
cyrasafia:stale-project-path

Conversation

@cyrasafia

Copy link
Copy Markdown

Issue for this PR

Closes #35240
Related #30697 #30005 #30015 #23248

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a project directory is moved or renamed on disk, Project.fromDirectory kept the stale worktree path in the project table forever. The root cause is that the upsert in fromDirectory unconditionally reused existing.worktree for non-global projects:

worktree: projectID === ProjectV2.ID.global ? worktree : existing.worktree,

This meant reopening the project from its new location never updated the stored path, causing the desktop app and TUI to display and navigate to the old (now-dead) path.

Fix: Before reusing the stored worktree, check whether it still exists on disk via fs.existsSafe. If the path is gone (project was moved/renamed), use the freshly resolved path instead. If the path still exists, the existing value is preserved — this is important for the linked-worktree accumulation behavior verified by the "should accumulate multiple worktrees in sandboxes" test, where opening a second worktree must NOT overwrite the primary worktree.

How did you verify your code works?

  • Added a new test "updates worktree when project directory is moved" that creates a git repo, moves the directory via mv, reopens from the new path, and asserts worktree is updated while the project ID stays the same.
  • Verified the existing linked-worktree accumulation test still passes (both paths exist on disk, so the stored worktree is preserved).
  • Ran bun typecheck and bun test test/project/project.test.ts test/project/project-directory.test.ts test/project/migrate-global.test.ts — all 49 tests pass.

Screenshots / recordings

N/A — server-side data layer change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

付晨昱 and others added 2 commits July 24, 2026 19:06
fromDirectory now checks whether the stored worktree path still exists
on disk before reusing it. When the path is gone (project moved or
renamed) the freshly resolved path takes over, while preserving the
existing value for linked-worktree accumulation.
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several related PRs that address similar issues with moved/stale project paths:

Potentially Related PRs:

  1. PR fix(opencode): recover projects moved to a new path #38584 - fix(opencode): recover projects moved to a new path

    • Directly addresses recovering projects when they're moved to a new path
  2. PR fix(opencode): handle stale session.directory when project moves #35492 - fix(opencode): handle stale session.directory when project moves

    • Handles stale directory paths when projects move
  3. PR fix(desktop): handle moved projects and deleted paths #34137 - fix(desktop): handle moved projects and deleted paths

    • Desktop app handling of moved projects and deleted paths
  4. PR fix(core): relocate moved project checkouts #34142 - fix(core): relocate moved project checkouts

    • Core-level relocation of moved project checkouts

These PRs are working on overlapping concerns around project relocation and stale paths. PR #38584 appears to be the most directly related, as it specifically focuses on recovering projects moved to new paths. You may want to check if there's any overlap in approach or if these should be coordinated.

@cyrasafia cyrasafia closed this Jul 27, 2026
@cyrasafia
cyrasafia deleted the stale-project-path branch July 27, 2026 06:05
@cyrasafia
cyrasafia restored the stale-project-path branch July 27, 2026 06:07
@cyrasafia cyrasafia reopened this Jul 27, 2026
@cyrasafia cyrasafia closed this Jul 27, 2026
@cyrasafia
cyrasafia deleted the stale-project-path branch July 27, 2026 06:09
@cyrasafia

Copy link
Copy Markdown
Author

Closing in favor of #39056 (same change, head branch renamed to fix/stale-project-path).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server keeps stale project.worktree after project folder is renamed on disk; remote clients keep reopening the dead path

1 participant