diff --git a/packages/server/src/server/workspace-reconciliation-observation.test.ts b/packages/server/src/server/workspace-reconciliation-observation.test.ts index 9e94a3839b..469e0db199 100644 --- a/packages/server/src/server/workspace-reconciliation-observation.test.ts +++ b/packages/server/src/server/workspace-reconciliation-observation.test.ts @@ -600,70 +600,74 @@ describe("observed workspace placement", () => { } }); - test("reads a separately registered linked worktree alongside its main checkout and skips unrelated Git repos", async () => { - const observed = new ObservedPlacements( - [ - { id: "repo", root: "repo", workspaces: [{ id: "main-ws", cwd: "repo" }] }, - { - id: "unrelated", - root: "repo-other", - workspaces: [{ id: "other-ws", cwd: "repo-other" }], - }, - ], - true, - ); - try { - await observed.start(); - observed.git("repo", "init", "-b", "main"); - observed.git( - "repo", - "-c", - "user.name=Test", - "-c", - "user.email=test@example.com", - "commit", - "--allow-empty", - "-m", - "initial", + test.each(["removal", "replacement"])( + "updates a separately registered linked worktree after updates and metadata %s", + async (change) => { + const observed = new ObservedPlacements( + [ + { id: "repo", root: "repo", workspaces: [{ id: "main-ws", cwd: "repo" }] }, + { + id: "unrelated", + root: "repo-other", + workspaces: [{ id: "other-ws", cwd: "repo-other" }], + }, + ], + true, ); - observed.git("repo", "remote", "add", "origin", "https://github.com/acme/old.git"); - observed.git("repo", "worktree", "add", "-b", "feature", "../linked"); - await observed.add({ - id: "linked", - root: "linked", - workspaces: [{ id: "linked-ws", cwd: "linked" }], - }); - observed.git("repo-other", "init", "-b", "main"); - observed.change("linked", ".git"); - await observed.advanceBy(DEBOUNCE_MS); - expect(await observed.placement("linked-ws")).toMatchObject({ branch: "feature" }); - const oldKey = (await observed.project("linked"))?.projectKey; - observed.takeCheckoutReads(); + try { + await observed.start(); + observed.git("repo", "init", "-b", "main"); + observed.git( + "repo", + "-c", + "user.name=Test", + "-c", + "user.email=test@example.com", + "commit", + "--allow-empty", + "-m", + "initial", + ); + observed.git("repo", "remote", "add", "origin", "https://github.com/acme/old.git"); + observed.git("repo", "worktree", "add", "-b", "feature", "../linked"); + await observed.add({ + id: "linked", + root: "linked", + workspaces: [{ id: "linked-ws", cwd: "linked" }], + }); + observed.git("repo-other", "init", "-b", "main"); + observed.change("linked", ".git"); + await observed.advanceBy(DEBOUNCE_MS); + expect(await observed.placement("linked-ws")).toMatchObject({ branch: "feature" }); + const oldKey = (await observed.project("linked"))?.projectKey; + observed.takeCheckoutReads(); - observed.git("repo", "remote", "set-url", "origin", "https://github.com/acme/new.git"); - observed.git("repo", "branch", "-m", "feature", "renamed"); - observed.change("repo", ".git"); - await observed.advanceBy(DEBOUNCE_MS); + observed.git("repo", "remote", "set-url", "origin", "https://github.com/acme/new.git"); + observed.git("repo", "branch", "-m", "feature", "renamed"); + observed.change("repo", ".git"); + await observed.advanceBy(DEBOUNCE_MS); - expect(await observed.placement("linked-ws")).toMatchObject({ branch: "renamed" }); - const linkedProject = await observed.project("linked"); - expect(linkedProject?.projectKey).toEqual((await observed.project("repo"))?.projectKey); - expect(linkedProject?.projectKey).not.toEqual(oldKey); - expect(observed.takeCheckoutReads()).toEqual(["linked", "repo"]); - expect(await observed.project("unrelated")).toMatchObject({ kind: "non_git" }); - expect(await observed.placement("other-ws")).toMatchObject({ kind: "directory" }); + expect(await observed.placement("linked-ws")).toMatchObject({ branch: "renamed" }); + const linkedProject = await observed.project("linked"); + expect(linkedProject?.projectKey).toEqual((await observed.project("repo"))?.projectKey); + expect(linkedProject?.projectKey).not.toEqual(oldKey); + expect(observed.takeCheckoutReads()).toEqual(["linked", "repo"]); + expect(await observed.project("unrelated")).toMatchObject({ kind: "non_git" }); + expect(await observed.placement("other-ws")).toMatchObject({ kind: "directory" }); - observed.rename("repo/.git", "repo/.git-backup"); - observed.change("repo", ".git"); - await observed.advanceBy(DEBOUNCE_MS); + observed.rename("repo/.git", "repo/.git-backup"); + if (change === "replacement") observed.git("repo", "init", "-b", "main"); + observed.change("repo", ".git"); + await observed.advanceBy(DEBOUNCE_MS); - expect(await observed.project("linked")).toMatchObject({ kind: "non_git" }); - expect(await observed.placement("linked-ws")).toMatchObject({ kind: "directory" }); - expect(observed.takeCheckoutReads()).toEqual(["linked", "repo"]); - } finally { - await observed.disposeGit(); - } - }); + expect(await observed.project("linked")).toMatchObject({ kind: "non_git" }); + expect(await observed.placement("linked-ws")).toMatchObject({ kind: "directory" }); + expect(observed.takeCheckoutReads()).toEqual(["linked", "repo"]); + } finally { + await observed.disposeGit(); + } + }, + ); test("skips independent nested repositories and workspaces attached from other repositories", async () => { const observed = new ObservedPlacements( diff --git a/packages/server/src/server/workspace-reconciliation-service.ts b/packages/server/src/server/workspace-reconciliation-service.ts index b1333eb53f..0f41824551 100644 --- a/packages/server/src/server/workspace-reconciliation-service.ts +++ b/packages/server/src/server/workspace-reconciliation-service.ts @@ -127,9 +127,14 @@ interface CachedCheckoutRead { checkout: Promise; } +interface ResolvedPath { + path: string; + missing: boolean; +} + interface CheckoutDirectory { path: string; - repository: { root: string; commonDir: string } | null; + repository: { root: string; commonDir: ResolvedPath } | null; } type DirectoryInspection = DirectoryState | CheckoutDirectory; @@ -748,13 +753,16 @@ async function findAffectedCheckoutDirectories( candidates: string[], inspect: (path: string, read: () => Promise) => Promise, ): Promise> { - const resolved = new Map>(); + const resolved = new Map>(); const resolvePath = (path: string) => { let result = resolved.get(path); if (!result) { result = resolveRealpath(path) .catch(() => resolveNativeRealpath(path)) - .catch(() => path); + .then( + (path) => ({ path, missing: false }), + (error) => ({ path, missing: isMissingPathError(error) }), + ); resolved.set(path, result); } return result; @@ -775,7 +783,7 @@ async function findAffectedCheckoutDirectories( const inspectCheckoutDirectory = (directory: string) => inspect(directory, async () => { - const path = await resolvePath(directory); + const { path } = await resolvePath(directory); return { path, repository: await findRepository(path) }; }); const dirty = await mapBounded(changedRoots, inspectCheckoutDirectory); @@ -787,16 +795,10 @@ async function findAffectedCheckoutDirectories( compareAsWindows: process.platform === "win32", }); const commonDirs = dirty.flatMap((entry) => - typeof entry !== "string" && entry.repository ? [entry.repository.commonDir] : [], + typeof entry !== "string" && entry.repository ? [entry.repository.commonDir.path] : [], ); - // A checkout can still point into a missing .git directory. For intact - // repositories, match common directories exactly so submodules stay independent. - const missingGitDirectories = createPathContainmentMatcher({ - roots: dirty.flatMap((entry) => - typeof entry !== "string" && entry.repository?.root !== entry.path - ? [join(entry.path, ".git")] - : [], - ), + const gitEntries = createPathContainmentMatcher({ + roots: roots.map((root) => join(root, ".git")), compareAsWindows: process.platform === "win32", }); const repositories = createPathContainmentMatcher({ @@ -808,10 +810,13 @@ async function findAffectedCheckoutDirectories( const entry = await inspectCheckoutDirectory(candidate); const path = typeof entry === "string" ? candidate : entry.path; const repository = typeof entry === "string" ? null : entry.repository; + const commonDir = repository?.commonDir; + // A broken checkout can still point beneath the changed .git entry after + // removal or replacement. Existing submodule metadata has its own identity. const sharesMetadata = - repository && - (repositories.matchesExact(repository.commonDir) || - missingGitDirectories.matches(repository.commonDir)); + commonDir && + (repositories.matchesExact(commonDir.path) || + (commonDir.missing && gitEntries.matches(commonDir.path))); // A surviving nested repository provides its own metadata even if its // parent gains or loses a .git entry. const hasIndependentNestedRepository =