Skip to content

Commit d76919a

Browse files
committed
fix(server): check out submodules in a new worktree
`git worktree add` leaves submodule paths empty, so a repository that keeps shared tooling in a submodule gets a worktree that is quietly missing it. When a project stores agent skills there, every thread started on a new worktree loses them and the agent reports the skill as unknown. Run `git submodule update --init --recursive` after creating a worktree, guarded on the checkout actually having a `.gitmodules`. It stays best-effort: the objects are normally already in the parent's `.git/modules` so this costs milliseconds, but a first-ever clone needs the network, and a submodule that cannot be fetched must not roll back the caller's thread. Model: Claude Opus 5 (1M) via Claude Code.
1 parent beab688 commit d76919a

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,92 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
13741374
}),
13751375
);
13761376

1377+
it.effect("checks out submodules in a new worktree", () =>
1378+
Effect.gen(function* () {
1379+
const fileSystem = yield* FileSystem.FileSystem;
1380+
const pathService = yield* Path.Path;
1381+
1382+
// Git refuses `file:` submodule transports by default (CVE-2022-39253)
1383+
// and ignores repo-level config for it, so a local fixture needs the
1384+
// env allowance. Real submodules are https/ssh and need none of this.
1385+
const previousAllowedProtocol = process.env.GIT_ALLOW_PROTOCOL;
1386+
process.env.GIT_ALLOW_PROTOCOL = "file";
1387+
yield* Effect.addFinalizer(() =>
1388+
Effect.sync(() => {
1389+
if (previousAllowedProtocol === undefined) {
1390+
delete process.env.GIT_ALLOW_PROTOCOL;
1391+
} else {
1392+
process.env.GIT_ALLOW_PROTOCOL = previousAllowedProtocol;
1393+
}
1394+
}),
1395+
);
1396+
1397+
// A real submodule: `git worktree add` leaves these empty, which is
1398+
// what silently strips shared tooling out of every new worktree.
1399+
const submoduleRepo = yield* makeTmpDir("git-submodule-");
1400+
yield* initRepoWithCommit(submoduleRepo);
1401+
yield* writeTextFile(submoduleRepo, "SHARED.md", "# shared\n");
1402+
yield* git(submoduleRepo, ["add", "."]);
1403+
yield* git(submoduleRepo, ["commit", "-m", "shared"]);
1404+
1405+
const cwd = yield* makeTmpDir();
1406+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1407+
yield* git(cwd, ["submodule", "add", submoduleRepo, "shared"]);
1408+
yield* git(cwd, ["commit", "-m", "add submodule"]);
1409+
1410+
const worktreePath = pathService.join(
1411+
yield* makeTmpDir("git-worktrees-"),
1412+
"submodule-worktree",
1413+
);
1414+
const driver = yield* GitVcsDriver.GitVcsDriver;
1415+
yield* driver.createWorktree({
1416+
cwd,
1417+
path: worktreePath,
1418+
refName: initialBranch,
1419+
newRefName: "feature/submodules",
1420+
});
1421+
1422+
assert.equal(
1423+
yield* fileSystem.exists(pathService.join(worktreePath, "shared", "SHARED.md")),
1424+
true,
1425+
);
1426+
}),
1427+
);
1428+
1429+
it.effect("still creates the worktree when submodule checkout fails", () =>
1430+
Effect.gen(function* () {
1431+
const fileSystem = yield* FileSystem.FileSystem;
1432+
const pathService = yield* Path.Path;
1433+
1434+
const cwd = yield* makeTmpDir();
1435+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1436+
// Points at a repository that does not exist, so the checkout fails the
1437+
// way an unreachable private remote would. Creation must still succeed.
1438+
yield* writeTextFile(
1439+
cwd,
1440+
".gitmodules",
1441+
'[submodule "missing"]\n\tpath = missing\n\turl = /nonexistent/repo.git\n',
1442+
);
1443+
yield* git(cwd, ["add", "."]);
1444+
yield* git(cwd, ["commit", "-m", "add unreachable submodule"]);
1445+
1446+
const worktreePath = pathService.join(
1447+
yield* makeTmpDir("git-worktrees-"),
1448+
"broken-submodule-worktree",
1449+
);
1450+
const driver = yield* GitVcsDriver.GitVcsDriver;
1451+
const created = yield* driver.createWorktree({
1452+
cwd,
1453+
path: worktreePath,
1454+
refName: initialBranch,
1455+
newRefName: "feature/broken-submodules",
1456+
});
1457+
1458+
assert.equal(created.worktree.path, worktreePath);
1459+
assert.equal(yield* fileSystem.exists(worktreePath), true);
1460+
}),
1461+
);
1462+
13771463
it.effect("creates and removes a worktree for a new refName", () =>
13781464
Effect.gen(function* () {
13791465
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,6 +2771,30 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
27712771
fallbackErrorDetail: "git worktree add failed",
27722772
});
27732773

2774+
// `git worktree add` leaves submodules empty, so a repo that keeps agent
2775+
// skills, tooling or source in one gets a worktree that is quietly missing
2776+
// them. Best-effort: the objects are usually already in the parent's
2777+
// `.git/modules`, but a first-ever clone needs the network, and failing to
2778+
// populate a submodule must not roll back the caller's thread.
2779+
const hasSubmodules = yield* fileSystem
2780+
.exists(path.join(worktreePath, ".gitmodules"))
2781+
.pipe(Effect.orElseSucceed(() => false));
2782+
if (hasSubmodules) {
2783+
yield* runGit("GitVcsDriver.createWorktree.updateSubmodules", worktreePath, [
2784+
"submodule",
2785+
"update",
2786+
"--init",
2787+
"--recursive",
2788+
]).pipe(
2789+
Effect.catch((cause) =>
2790+
Effect.logWarning("worktree submodule checkout failed; submodule paths are empty", {
2791+
worktreePath,
2792+
cause,
2793+
}),
2794+
),
2795+
);
2796+
}
2797+
27742798
if (input.newRefName && input.baseRefName) {
27752799
const remoteNames = yield* listRemoteNames(input.cwd).pipe(Effect.orElseSucceed(() => []));
27762800
const parsedBaseRef = parseRemoteRefWithRemoteNames(

0 commit comments

Comments
 (0)