Skip to content

fix(project): use git common dir for bare repo project cache#19054

Open
StevenTCramer wants to merge 2 commits intoanomalyco:devfrom
TimeWarpEngineering:Cramer/2026-03-25/bare-repo-bug
Open

fix(project): use git common dir for bare repo project cache#19054
StevenTCramer wants to merge 2 commits intoanomalyco:devfrom
TimeWarpEngineering:Cramer/2026-03-25/bare-repo-bug

Conversation

@StevenTCramer
Copy link
Copy Markdown
Contributor

@StevenTCramer StevenTCramer commented Mar 25, 2026

Issue for this PR

Closes #18045

Type of change

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

What does this PR do?

This fixes project ID cache placement for bare-repo-backed worktrees.

Before this change, cache read/write used worktree/.git/opencode. In bare repo setups, worktree resolves to the parent of the bare repo, so cache could be written to a shared parent path like <parent>/.git/opencode.

This PR reads and writes cache at git rev-parse --git-common-dir (common/opencode) instead. That is the correct git metadata location for normal repos, worktrees, and bare repos.

I also added regression tests for:

  • bare repo worktree cache path (must be in <bare>.git/opencode, not parent)
  • sibling bare repos under one parent (must not share IDs via parent cache)

How did you verify your code works?

  • Ran bun test --timeout 30000 test/project/ in packages/opencode
  • New bare-repo tests fail on old behavior and pass with this fix
  • Manually validated no cache is created at the incorrect parent .git/opencode path

Screenshots / recordings

N/A (no UI changes)

Checklist

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

@github-actions github-actions bot added contributor needs:compliance This means the issue will auto-close after 2 hours. labels Mar 25, 2026
@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 one potentially related PR that might be addressing similar concerns:

Related PR:

This PR appears related because it also deals with project IDs across worktrees, which is the same domain as the current PR. However, this is likely a different fix addressing a different aspect of the worktree/project identity issue.

No exact duplicate PRs found addressing the same bare repo cache placement issue described in PR #19054.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 25, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@StevenTCramer StevenTCramer force-pushed the Cramer/2026-03-25/bare-repo-bug branch from fafcb9a to 5fc5fbc Compare March 25, 2026 04:27
@jlongster
Copy link
Copy Markdown
Contributor

I think a better fix is to focus on the worktree variable and make that correct. We also want worktree to be correct in the database for the project row, and your fix only fixes the .git resolution to store the project id. If you fix the worktree variable, it will be both of these issues

I would improve this line:

          const worktree = common === sandbox ? sandbox : pathSvc.dirname(common)

Is there a way to detect a bare repo? I would try to detect that, and just keep worktree pointed at the bare repo itself if so

@StevenTCramer
Copy link
Copy Markdown
Contributor Author

Good point on fixing the worktree variable directly. I've updated the PR to detect bare repos and set worktree = common for them.

Regarding bare repo detection: I considered using git rev-parse --is-bare-repository but opted for a path-based heuristic instead:

const commonName = pathSvc.basename(common)
const isBareRepo = commonName.endsWith(".git") && commonName !== ".git"

This avoids spawning an extra git process (~50-100ms overhead) in a hot path. The heuristic correctly identifies bare repos (e.g., bare.git) while excluding regular .git directories. If edge cases emerge, we can add the git command as a fallback.

@StevenTCramer StevenTCramer force-pushed the Cramer/2026-03-25/bare-repo-bug branch 2 times, most recently from 725c405 to d462551 Compare March 27, 2026 01:56
@StevenTCramer
Copy link
Copy Markdown
Contributor Author

The e2e (windows) failure is a known flaky test (prompt-slash-terminal.spec.ts) that times out waiting for the terminal panel UI element. This test has failed intermittently on other recent PRs as well (e.g., runs 23625820101, 23625290137) and is unrelated to the project.ts changes in this PR.

@jlongster This is a pre-existing flaky E2E test, not a regression from this change.

@StevenTCramer StevenTCramer force-pushed the Cramer/2026-03-25/bare-repo-bug branch from d462551 to d462603 Compare March 27, 2026 02:57
Use git-common-dir as the project ID cache location so bare-repo-backed worktrees do not write to a shared parent .git path. Add regression tests that reproduce parent-path cache collisions across bare repos.
Detect bare repos by checking if git-common-dir basename ends with .git (excluding .git itself). This ensures both the worktree field in the database and the cache location are correct for bare-repo-backed worktrees.
@StevenTCramer StevenTCramer force-pushed the Cramer/2026-03-25/bare-repo-bug branch from d462603 to ce91176 Compare March 28, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: project_id cache collision across bare repos under shared parent directory

2 participants