Skip to content

Commit 20d96a4

Browse files
fix: resolve unique project IDs for git submodules
Submodules store their git-common-dir inside the parent repo's .git/modules/<name> directory. The existing worktree derivation (dirname of common-dir) collapsed every submodule to the same .git/modules path, causing all submodules under one parent to share a single project ID and mix their session histories. Detect submodules by checking whether the common-dir path contains .git/modules/ and, when true, use the submodule's own checkout directory as the worktree and its own common-dir for caching the project ID. Fixes anomalyco#6696 (submodule case) Fixes anomalyco#8836 (submodule case)
1 parent 98a31e3 commit 20d96a4

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎packages/opencode/src/project/project.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,27 @@ export namespace Project {
203203
vcs: fakeVcs,
204204
}
205205
}
206+
const common = resolveGitPath(sandbox, commonDir.text.trim())
207+
// Detect git submodules: their git-common-dir lives inside the
208+
// parent repo's `.git/modules/` directory. For submodules the
209+
// "worktree" concept doesn't apply the same way as for linked
210+
// worktrees -- each submodule is its own independent repo that
211+
// just happens to be stored inside the parent's `.git`. Using
212+
// `dirname(common)` collapses every submodule to the same
213+
// `.git/modules` path, causing project-ID collisions.
214+
const isSubmodule = /[/\\]\.git[/\\]modules[/\\]/.test(common)
206215
const worktree = (() => {
207-
const common = resolveGitPath(sandbox, commonDir.text.trim())
216+
if (isSubmodule) return sandbox
208217
return common === sandbox ? sandbox : pathSvc.dirname(common)
209218
})()
210219

220+
// For submodules, read/write the cached project ID inside the
221+
// submodule's own git-common-dir rather than in a shared parent
222+
// location that would be identical for every submodule.
223+
const cacheDir = isSubmodule ? common : pathSvc.join(worktree, ".git")
224+
211225
if (id == null) {
212-
id = yield* readCachedProjectId(pathSvc.join(worktree, ".git"))
226+
id = yield* readCachedProjectId(cacheDir)
213227
}
214228

215229
if (!id) {
@@ -222,7 +236,7 @@ export namespace Project {
222236

223237
id = roots[0] ? ProjectID.make(roots[0]) : undefined
224238
if (id) {
225-
yield* fsys.writeFileString(pathSvc.join(worktree, ".git", "opencode"), id).pipe(Effect.ignore)
239+
yield* fsys.writeFileString(pathSvc.join(cacheDir, "opencode"), id).pipe(Effect.ignore)
226240
}
227241
}
228242

‎packages/opencode/test/project/project.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,51 @@ describe("Project.fromDirectory with worktrees", () => {
242242
})
243243
})
244244

245+
describe("Project.fromDirectory with submodules", () => {
246+
test("submodules should get unique project IDs", async () => {
247+
await using parent = await tmpdir({ git: true })
248+
249+
// Create two independent "remote" repos to use as submodules
250+
await using remoteA = await tmpdir({ git: true })
251+
await using remoteB = await tmpdir({ git: true })
252+
253+
// Add them as submodules inside the parent repo
254+
await $`git -c protocol.file.allow=always submodule add ${remoteA.path} sub-a`.cwd(parent.path).quiet()
255+
await $`git -c protocol.file.allow=always submodule add ${remoteB.path} sub-b`.cwd(parent.path).quiet()
256+
await $`git commit -m "add submodules"`.cwd(parent.path).quiet()
257+
258+
const subA = path.join(parent.path, "sub-a")
259+
const subB = path.join(parent.path, "sub-b")
260+
261+
const { project: projA } = await Project.fromDirectory(subA)
262+
const { project: projB } = await Project.fromDirectory(subB)
263+
const { project: projParent } = await Project.fromDirectory(parent.path)
264+
265+
// Each submodule must get its own unique project ID
266+
expect(projA.id).not.toBe(projB.id)
267+
// Submodules must not share the parent's project ID
268+
expect(projA.id).not.toBe(projParent.id)
269+
expect(projB.id).not.toBe(projParent.id)
270+
// Each submodule's worktree should be its own checkout directory
271+
expect(projA.worktree).toBe(subA)
272+
expect(projB.worktree).toBe(subB)
273+
})
274+
275+
test("same submodule should return stable project ID", async () => {
276+
await using parent = await tmpdir({ git: true })
277+
await using remote = await tmpdir({ git: true })
278+
279+
await $`git -c protocol.file.allow=always submodule add ${remote.path} sub`.cwd(parent.path).quiet()
280+
await $`git commit -m "add submodule"`.cwd(parent.path).quiet()
281+
282+
const subPath = path.join(parent.path, "sub")
283+
const { project: first } = await Project.fromDirectory(subPath)
284+
const { project: second } = await Project.fromDirectory(subPath)
285+
286+
expect(second.id).toBe(first.id)
287+
})
288+
})
289+
245290
describe("Project.discover", () => {
246291
test("should discover favicon.png in root", async () => {
247292
await using tmp = await tmpdir({ git: true })

0 commit comments

Comments
 (0)