Skip to content

Worktrees resolve to different project IDs due to per-worktree cache lookup #16995

@afkent27

Description

@afkent27

Summary

When OpenCode is opened from a git worktree, it looks for the .git/opencode cache file in the per-worktree git directory (.git/worktrees/<name>/opencode) rather than the common git directory (.git/opencode). This causes each worktree to compute a potentially different project ID, resulting in separate project records and empty session lists.

Steps to Reproduce

  1. Have a repo with a cached project ID in .git/opencode
  2. Create a git worktree: git worktree add ../my-worktree feature-branch
  3. Open OpenCode from the worktree directory
  4. Open the session list — it shows "No results found"

Root Cause

In src/project/project.ts, the fromDirectory() function resolves the .git path and looks for an opencode file there. For worktrees, .git is a file (not a directory) pointing to .git/worktrees/<name>/, so the cache lookup goes to .git/worktrees/<name>/opencode instead of the main .git/opencode.

Since the per-worktree cache doesn't exist, OpenCode falls back to git rev-list --max-parents=0 --all. This command can return different results depending on stash state (e.g., git stash -u creates orphan root commits), leading to a different project ID being computed and cached per-worktree.

Expected Behavior

All worktrees of the same repository should resolve to the same project ID and share sessions. The cache file should be read from and written to the common git directory (obtainable via git rev-parse --git-common-dir), not the per-worktree git directory.

Workaround

Manually create the cache file in each worktree's git directory:

echo "<project-id>" > .git/worktrees/<worktree-name>/opencode

This must be repeated for every new worktree.

Suggested Fix

In fromDirectory(), use git rev-parse --git-common-dir (which is already being called later in the function) to determine where to read/write the opencode cache file, instead of using the dotgit path which varies per worktree.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions