fix(server): refresh linked checkouts after Git metadata replacement

A replacement .git directory can exist before reconciliation runs while linked checkouts still point at its missing former worktree entries. Detect missing administrative paths on the dependent checkout so live submodules keep their own scope.
This commit is contained in:
Mohamed Boudra
2026-11-26 02:37:19 +01:00
parent d97d0ba1c0
commit 3936137993
2 changed files with 84 additions and 75 deletions
@@ -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(
@@ -127,9 +127,14 @@ interface CachedCheckoutRead {
checkout: Promise<ProjectCheckoutLitePayload>;
}
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<CheckoutDirectory>) => Promise<DirectoryInspection>,
): Promise<Set<string>> {
const resolved = new Map<string, Promise<string>>();
const resolved = new Map<string, Promise<ResolvedPath>>();
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 =