Skip to content

fix: adopt new worktree when a project repository is moved - #42933

Closed
jonathangn wants to merge 1 commit into
anomalyco:devfrom
jonathangn:fix/moved-repo-worktree
Closed

fix: adopt new worktree when a project repository is moved#42933
jonathangn wants to merge 1 commit into
anomalyco:devfrom
jonathangn:fix/moved-repo-worktree

Conversation

@jonathangn

Copy link
Copy Markdown

Problem

When a project's repository is moved or renamed on disk (e.g. the folder is moved to a parent directory), opencode keeps using the old worktree path:

  • Project.fromDirectory always keeps existing.worktree for existing (non-global) projects, even when that path no longer exists. The resolved new checkout is only ever added to sandboxes, never adopted as the project's worktree.
  • InstanceStore.boot uses the requested directory verbatim as the instance/session directory. If a client passes a stale path (as the desktop does from its persisted project state), the instance boots with a non-existent directory.

The result: "New conversation" fails with ENOENT / FileSystem.realPath errors until the user manually edits the database. Repros happen whenever the repo root is moved up to its parent (e.g. G:/.../AlgoTrading/EA Admirals -> G:/.../AlgoTrading), so the old worktree is now a dead child of the new git root.

Fix

Two complementary server-side changes:

  1. Project.fromDirectory (packages/opencode/src/project/project.ts): if the project's canonical worktree no longer exists on disk and the repository still resolves to a valid checkout, adopt the resolved git worktree (data.directory) as the new canonical worktree. Existing sessions rooted at the old worktree are re-pointed to the new directory, so old conversations keep working.
  2. InstanceStore.boot (packages/opencode/src/project/instance-store.ts): when the requested directory does not exist but fromDirectory resolved it to a non-global git project, boot the instance in the resolved worktree (result.sandbox) instead of the stale path. This makes both new sessions and loading old sessions succeed even when a client passes a path that no longer exists.

Tests

  • Project.fromDirectory with worktrees > adopts a moved checkout as the project worktree when the original is gone — moves a repo and asserts the project adopts the new worktree.
  • InstanceStore > boots into the resolved worktree when the requested directory no longer exists — loads an instance at a non-existent child of a git root and asserts it boots into the resolved worktree.

Both new tests pass; the existing project and instance-store suites pass (47/47). tsgo --noEmit and oxlint are clean on the touched files.

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

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

Found potential related PRs:

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

  2. PR fix (core): Multiple clones of same repo are different projects #35311 - fix (core): Multiple clones of same repo are different projects

I recommend checking if PR #38584 was a previous attempt at this issue or if there's overlap in the solution approach.

@jonathangn

Copy link
Copy Markdown
Author

Closing this in favor of existing work: this duplicates #38584 (same fix, same files, more thorough) and #35311 (the maintainer-preferred approach). Nothing lost — the regression tests and analysis are preserved in this branch (fix/moved-repo-worktree) and in the PR history if anyone wants to reuse them.

@jonathangn jonathangn closed this Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:compliance This means the issue will auto-close after 2 hours. needs:issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant