Skip to content

Commit 8195530

Browse files
authored
fix(server): let slow worktree submodule checkouts finish (#28482)
1 parent 2101c6f commit 8195530

2 files changed

Lines changed: 157 additions & 21 deletions

File tree

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

Lines changed: 146 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,61 @@ it.effect("lets a worktree checkout that is still working run past five minutes"
13811381
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
13821382
);
13831383

1384+
it.effect("cancels a slow submodule checkout without reporting best-effort success", () =>
1385+
Effect.gen(function* () {
1386+
const updateStarted = yield* Deferred.make<void>();
1387+
const updateStopped = yield* Deferred.make<void>();
1388+
const finished = yield* Ref.make<ReadonlyArray<{ ok: boolean; detail: string | null }>>([]);
1389+
const claimed = yield* Ref.make<string | null>(null);
1390+
const spawner = ChildProcessSpawner.make((command) => {
1391+
if (
1392+
ChildProcess.isStandardCommand(command) &&
1393+
command.args[0] === "submodule" &&
1394+
command.args[1] === "update"
1395+
) {
1396+
return Effect.acquireRelease(
1397+
Effect.succeed(
1398+
ChildProcessSpawner.makeHandle({
1399+
...makeSuccessfulHandle(""),
1400+
exitCode: Deferred.succeed(updateStarted, undefined).pipe(
1401+
Effect.andThen(Effect.never),
1402+
),
1403+
}),
1404+
),
1405+
() => Deferred.succeed(updateStopped, undefined),
1406+
);
1407+
}
1408+
return Effect.succeed(makeSuccessfulHandle(""));
1409+
});
1410+
const driver = yield* makeGitVcsDriverCore().pipe(
1411+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1412+
);
1413+
const cwd = yield* makeTmpDir();
1414+
const path = yield* Path.Path;
1415+
const worktreePath = path.join(cwd, "worktree");
1416+
yield* writeTextFile(worktreePath, ".gitmodules", "");
1417+
const creation = yield* driver
1418+
.createWorktree(
1419+
{ cwd, refName: "main", newRefName: "bugfix/cancel-submodules", path: worktreePath },
1420+
{
1421+
progress: {
1422+
onWorktreeClaimed: (value) => Ref.set(claimed, value),
1423+
onSubmodulesFinished: (result) => Ref.update(finished, (items) => [...items, result]),
1424+
},
1425+
},
1426+
)
1427+
.pipe(Effect.forkChild);
1428+
yield* Deferred.await(updateStarted);
1429+
yield* TestClock.adjust("6 minutes");
1430+
yield* Fiber.interrupt(creation);
1431+
const exit = yield* Fiber.await(creation);
1432+
assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
1433+
assert.isTrue(yield* Deferred.isDone(updateStopped));
1434+
assert.equal(yield* Ref.get(claimed), worktreePath);
1435+
assert.isEmpty(yield* Ref.get(finished));
1436+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
1437+
);
1438+
13841439
describe("rejected git refs", () => {
13851440
const remoteUrl = "https://user:[email protected]/org/secret-repo.git";
13861441
const failedPush = `error: failed to push some refs to '${remoteUrl}'`;
@@ -4145,7 +4200,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
41454200
}),
41464201
);
41474202

4148-
it.effect("checks out submodules in a new worktree", () =>
4203+
it.effect.each([
4204+
["recursive", false],
4205+
["recursive", true],
4206+
["top-level", false],
4207+
["top-level", true],
4208+
] as const)("finishes a slow submodule checkout (%s, progress: %s)", ([submodules, progress]) =>
41494209
Effect.gen(function* () {
41504210
const fileSystem = yield* FileSystem.FileSystem;
41514211
const pathService = yield* Path.Path;
@@ -4165,6 +4225,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
41654225

41664226
const submoduleRepo = yield* makeTmpDir("git-submodule-");
41674227
yield* initRepoWithCommit(submoduleRepo);
4228+
yield* writeTextFile(submoduleRepo, ".gitattributes", "* text eol=lf\n");
41684229
yield* writeTextFile(submoduleRepo, "SHARED.md", "# shared\n");
41694230
yield* git(submoduleRepo, ["add", "."]);
41704231
yield* git(submoduleRepo, ["commit", "-m", "shared"]);
@@ -4173,23 +4234,80 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
41734234
const { initialBranch } = yield* initRepoWithCommit(cwd);
41744235
yield* git(cwd, ["submodule", "add", submoduleRepo, "shared"]);
41754236
yield* git(cwd, ["commit", "-m", "add submodule"]);
4237+
const recordedHead = yield* git(submoduleRepo, ["rev-parse", "HEAD"]);
4238+
yield* writeTextFile(submoduleRepo, "SHARED.md", "# newer remote default\n");
4239+
yield* git(submoduleRepo, ["commit", "-am", "advance remote default"]);
4240+
4241+
const updateStarted = yield* Deferred.make<void>();
4242+
const releaseUpdate = yield* Deferred.make<void>();
4243+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
4244+
const spawner = ChildProcessSpawner.make((command) =>
4245+
Effect.gen(function* () {
4246+
if (
4247+
ChildProcess.isStandardCommand(command) &&
4248+
command.args[0] === "submodule" &&
4249+
command.args[1] === "update"
4250+
) {
4251+
yield* Deferred.succeed(updateStarted, undefined);
4252+
yield* Deferred.await(releaseUpdate);
4253+
}
4254+
return yield* delegate.spawn(command);
4255+
}),
4256+
);
4257+
const driver = yield* makeGitVcsDriverCore().pipe(
4258+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4259+
Effect.provide(layerServerConfig),
4260+
);
4261+
const finished = yield* Ref.make<ReadonlyArray<{ ok: boolean; detail: string | null }>>([]);
4262+
const lines = yield* Ref.make<ReadonlyArray<string>>([]);
41764263

41774264
const worktreePath = pathService.join(
41784265
yield* makeTmpDir("git-worktrees-"),
41794266
"submodule-worktree",
41804267
);
4181-
const driver = yield* GitVcsDriver.GitVcsDriver;
4182-
yield* driver.createWorktree({
4183-
cwd,
4184-
path: worktreePath,
4185-
refName: initialBranch,
4186-
newRefName: "feature/submodules",
4187-
});
4268+
const creation = yield* driver
4269+
.createWorktree(
4270+
{
4271+
cwd,
4272+
path: worktreePath,
4273+
refName: initialBranch,
4274+
newRefName: "feature/submodules",
4275+
},
4276+
{
4277+
submodules,
4278+
progress: {
4279+
onSubmodulesFinished: (result) =>
4280+
Ref.update(finished, (items) => [...items, result]),
4281+
...(progress
4282+
? {
4283+
onSubmoduleLine: (line: string) =>
4284+
Ref.update(lines, (items) => [...items, line]),
4285+
}
4286+
: {}),
4287+
},
4288+
},
4289+
)
4290+
.pipe(Effect.forkChild);
4291+
yield* Deferred.await(updateStarted);
4292+
yield* TestClock.adjust("6 minutes");
4293+
yield* Deferred.succeed(releaseUpdate, undefined);
4294+
yield* Fiber.join(creation);
41884295

4296+
assert.deepEqual(yield* Ref.get(finished), [{ ok: true, detail: null }]);
41894297
assert.equal(
41904298
yield* fileSystem.exists(pathService.join(worktreePath, "shared", "SHARED.md")),
41914299
true,
41924300
);
4301+
assert.equal(
4302+
yield* git(pathService.join(worktreePath, "shared"), ["rev-parse", "HEAD"]),
4303+
recordedHead,
4304+
);
4305+
assert.equal(yield* git(worktreePath, ["status", "--porcelain"]), "");
4306+
assert.equal(
4307+
yield* git(pathService.join(worktreePath, "shared"), ["status", "--porcelain"]),
4308+
"",
4309+
);
4310+
if (progress) assert.isNotEmpty(yield* Ref.get(lines));
41934311
}),
41944312
);
41954313

@@ -4206,22 +4324,36 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
42064324
'[submodule "missing"]\n\tpath = missing\n\turl = /nonexistent/repo.git\n',
42074325
);
42084326
yield* git(cwd, ["add", "."]);
4327+
const gitlink = yield* git(cwd, ["rev-parse", "HEAD"]);
4328+
yield* git(cwd, ["update-index", "--add", "--cacheinfo", `160000,${gitlink},missing`]);
42094329
yield* git(cwd, ["commit", "-m", "add unreachable submodule"]);
42104330

42114331
const worktreePath = pathService.join(
42124332
yield* makeTmpDir("git-worktrees-"),
42134333
"broken-submodule-worktree",
42144334
);
42154335
const driver = yield* GitVcsDriver.GitVcsDriver;
4216-
const created = yield* driver.createWorktree({
4217-
cwd,
4218-
path: worktreePath,
4219-
refName: initialBranch,
4220-
newRefName: "feature/broken-submodules",
4221-
});
4336+
const finished = yield* Ref.make<ReadonlyArray<{ ok: boolean; detail: string | null }>>([]);
4337+
const created = yield* driver.createWorktree(
4338+
{
4339+
cwd,
4340+
path: worktreePath,
4341+
refName: initialBranch,
4342+
newRefName: "feature/broken-submodules",
4343+
},
4344+
{
4345+
progress: {
4346+
onSubmodulesFinished: (result) => Ref.update(finished, (items) => [...items, result]),
4347+
},
4348+
},
4349+
);
42224350

42234351
assert.equal(created.worktree.path, worktreePath);
42244352
assert.equal(yield* fileSystem.exists(worktreePath), true);
4353+
const results = yield* Ref.get(finished);
4354+
assert.lengthOf(results, 1);
4355+
assert.isFalse(results[0]!.ok);
4356+
assert.include(results[0]!.detail, "GitVcsDriver.createWorktree.updateSubmodules");
42254357
}),
42264358
);
42274359

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,17 +4239,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
42394239
submoduleMode.value === "recursive"
42404240
? ["submodule", "update", "--init", "--recursive"]
42414241
: ["submodule", "update", "--init"],
4242-
onSubmoduleLine
4243-
? {
4244-
env: { LC_ALL: "C" },
4245-
progress: { onStdoutLine: onSubmoduleLine, onStderrLine: onSubmoduleLine },
4246-
}
4247-
: {},
4242+
{
4243+
// Cloning scales with remote size; let it finish or be cancelled, like worktree add.
4244+
timeoutMs: null,
4245+
...(onSubmoduleLine
4246+
? {
4247+
env: { LC_ALL: "C" },
4248+
progress: { onStdoutLine: onSubmoduleLine, onStderrLine: onSubmoduleLine },
4249+
}
4250+
: {}),
4251+
},
42484252
),
42494253
),
42504254
Effect.matchEffect({
42514255
onFailure: (cause) =>
4252-
Effect.logWarning("worktree submodule checkout failed; submodule paths are empty", {
4256+
Effect.logWarning("worktree submodule checkout failed; submodules may be incomplete", {
42534257
worktreePath,
42544258
cause,
42554259
}).pipe(

0 commit comments

Comments
 (0)