From d97d0ba1c0e8b10bca6015bf80d70b7b978b97d2 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Thu, 26 Nov 2026 02:32:05 +0100 Subject: [PATCH] fix(server): keep submodules out of parent Git rescans Use Git admin path containment when a watched Git directory is missing. An intact parent and its submodules keep separate common directories even though their administrative files share an ancestor. --- ...rkspace-reconciliation-observation.test.ts | 50 +++++++++++++++++++ .../workspace-reconciliation-service.ts | 15 ++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/server/src/server/workspace-reconciliation-observation.test.ts b/packages/server/src/server/workspace-reconciliation-observation.test.ts index ffda7e2b1c..9e94a3839b 100644 --- a/packages/server/src/server/workspace-reconciliation-observation.test.ts +++ b/packages/server/src/server/workspace-reconciliation-observation.test.ts @@ -726,6 +726,56 @@ describe("observed workspace placement", () => { } }); + test("skips a submodule and its linked checkout when the parent Git directory is intact", async () => { + const observed = new ObservedPlacements( + [{ id: "outer", root: "outer", workspaces: [{ id: "outer-ws", cwd: "outer" }] }], + true, + ); + try { + await observed.start(); + await observed.add({ id: "source", root: "source" }); + observed.git("source", "init", "-b", "main"); + observed.git( + "source", + "-c", + "user.name=Test", + "-c", + "user.email=test@example.com", + "-c", + "commit.gpgsign=false", + "commit", + "--allow-empty", + "-m", + "initial", + ); + observed.git("outer", "init", "-b", "main"); + observed.git( + "outer", + "-c", + "protocol.file.allow=always", + "submodule", + "add", + "../source", + "sub", + ); + observed.git("outer/sub", "worktree", "add", "-b", "feature", "../../sub-linked"); + await observed.add({ id: "sub", root: "outer/sub" }); + await observed.add({ id: "sub-linked", root: "sub-linked" }); + + observed.change("outer", ".git"); + await observed.advanceBy(DEBOUNCE_MS); + + expect(observed.takeCheckoutReads()).toEqual(["outer"]); + observed.rename("outer/.git", "outer/.git-backup"); + observed.change("outer", ".git"); + await observed.advanceBy(DEBOUNCE_MS); + + expect(observed.takeCheckoutReads()).toEqual(["outer", "outer/sub", "sub-linked"]); + } finally { + await observed.disposeGit(); + } + }); + test("turns Git changes under several roots into one read per changed root", async () => { const observed = new ObservedPlacements(threeProjects(1)); await observed.start(); diff --git a/packages/server/src/server/workspace-reconciliation-service.ts b/packages/server/src/server/workspace-reconciliation-service.ts index 0e462d15a0..b1333eb53f 100644 --- a/packages/server/src/server/workspace-reconciliation-service.ts +++ b/packages/server/src/server/workspace-reconciliation-service.ts @@ -789,10 +789,14 @@ async function findAffectedCheckoutDirectories( const commonDirs = dirty.flatMap((entry) => typeof entry !== "string" && entry.repository ? [entry.repository.commonDir] : [], ); - // If a main checkout loses .git, surviving linked checkouts still point at - // their missing admin directories inside it. - const gitEntries = createPathContainmentMatcher({ - roots: roots.map((root) => join(root, ".git")), + // 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")] + : [], + ), compareAsWindows: process.platform === "win32", }); const repositories = createPathContainmentMatcher({ @@ -806,7 +810,8 @@ async function findAffectedCheckoutDirectories( const repository = typeof entry === "string" ? null : entry.repository; const sharesMetadata = repository && - (repositories.matchesExact(repository.commonDir) || gitEntries.matches(repository.commonDir)); + (repositories.matchesExact(repository.commonDir) || + missingGitDirectories.matches(repository.commonDir)); // A surviving nested repository provides its own metadata even if its // parent gains or loses a .git entry. const hasIndependentNestedRepository =