Skip to content

fix(opencode): preserve subdirectory for worktree workspaces#26098

Open
JGoP-L wants to merge 1 commit intoanomalyco:devfrom
JGoP-L:fix-workspace-subdir-target-26002
Open

fix(opencode): preserve subdirectory for worktree workspaces#26098
JGoP-L wants to merge 1 commit intoanomalyco:devfrom
JGoP-L:fix-workspace-subdir-target-26002

Conversation

@JGoP-L
Copy link
Copy Markdown

@JGoP-L JGoP-L commented May 7, 2026

Issue for this PR

Closes #26002

Type of change

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

What does this PR do?

Worktree workspaces currently lose the opened subdirectory when opencode is started from inside a git repo subfolder.

For example, if opencode is opened from:

/repo/apps/web

creating a worktree workspace stores the workspace directory as the new worktree root instead of:

<new-worktree>/apps/web

This PR keeps those two paths separate:

  • the real git worktree root is still used for git worktree add and removal
  • the workspace target directory keeps the original project-relative subdirectory
  • the worktree root is stored in workspace extra.root only when it differs from the target directory

That keeps monorepo workspaces opening in the same nested project while preserving the existing worktree lifecycle behavior.

I also added a regression test that creates a real git repo with apps/web, creates a worktree workspace from that subdirectory, and verifies the workspace target points to the matching subdirectory in the new worktree.

How did you verify your code works?

I first reproduced the bug with a failing test in test/server/httpapi-workspace.test.ts.

Then I applied the fix and ran:

  • bun test test/server/httpapi-workspace.test.ts
  • bun test test/project/worktree.test.ts test/control-plane/workspace.test.ts
  • bun typecheck
  • git diff --check

Results:

  • 9 pass, 0 fail for workspace HTTP API tests
  • 41 pass, 0 fail for worktree/control-plane workspace tests
  • bun typecheck completed successfully
  • git diff --check completed successfully

Screenshots / recordings

N/A. This is workspace path behavior and does not change UI.

Checklist

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

Copilot AI review requested due to automatic review settings May 7, 2026 00:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

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

No duplicate PRs found.

@JGoP-L JGoP-L force-pushed the fix-workspace-subdir-target-26002 branch from f814fb5 to 404dca0 Compare May 7, 2026 00:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes workspace creation for Git worktrees when OpenCode is launched from a repository subdirectory (e.g. monorepo app folder), ensuring the created workspace opens at the corresponding subdirectory inside the new worktree while still managing the worktree lifecycle at the real worktree root.

Changes:

  • Add a regression test that creates a real Git repo with a nested apps/web project and verifies a worktree workspace targets the matching nested directory.
  • Extend the worktree creation flow to distinguish between the worktree root (directory) and the workspace target path (target) and bootstrap/load using the target.
  • Persist the worktree root in workspace.extra.root only when it differs from the workspace directory, and use it for correct removal.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/opencode/test/server/httpapi-workspace.test.ts Adds an end-to-end regression test for creating a worktree workspace from a Git subdirectory and verifying the resulting workspace directory.
packages/opencode/src/worktree/index.ts Introduces optional target to bootstrap/load and run start scripts from the intended workspace subdirectory.
packages/opencode/src/control-plane/adapters/worktree.ts Computes subdirectory-relative targets during configure, stores the worktree root in extra.root when needed, and uses root vs target appropriately for create/remove.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Workspace created from Git subdirectory opens at repository root

2 participants