Skip to content
TypeScript

fix(server): a failed worktree add no longer leaves its new branch behind (#28373)

Julius Marminge48c8cf787e75
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index aa98a53b127..ebfc4866c57 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -4317,6 +4317,38 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);
+ // regression: #28354 a failed worktree add left its new branch behind and blocked retries
+ it.effect("removes the new branch when the worktree cannot be added", () =>
+ Effect.gen(function* () {
+ const cwd = yield* makeTmpDir();
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
+ const path = yield* Path.Path;
+ const occupiedPath = yield* makeTmpDir("git-worktrees-");
+ yield* writeTextFile(occupiedPath, "existing.txt", "occupied\n");
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+
+ const failed = yield* driver
+ .createWorktree({
+ cwd,
+ path: occupiedPath,
+ refName: initialBranch,
+ newRefName: "t3/collide",
+ })
+ .pipe(Effect.result);
+
+ assert.isTrue(Result.isFailure(failed));
+ assert.equal(yield* git(cwd, ["branch", "--list", "t3/collide"]), "");
+ const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "retry");
+ yield* driver.createWorktree({
+ cwd,
+ path: worktreePath,
+ refName: initialBranch,
+ newRefName: "t3/collide",
+ });
+ assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "t3/collide");
+ }),
+ );
+
it.effect("creates parallel worktrees in one repository without racing on its config", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 6cc5266ff15..dbccb877553 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -4188,15 +4188,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
worktreePath = path.join(parentDir, repoName, directoryName);
}
- if (input.newRefName) {
+ const newRefName = input.newRefName;
+ if (newRefName) {
// Kept apart from the checkout so the shared-config lock only covers the tracking write.
yield* createBranchAt(
"GitVcsDriver.createWorktree.createBranch",
- { cwd: input.cwd, branch: input.newRefName, startPoint: input.refName },
+ { cwd: input.cwd, branch: newRefName, startPoint: input.refName },
"git worktree add failed",
);
}
- const args = ["worktree", "add", "--", worktreePath, input.newRefName ?? input.refName];
+ const args = ["worktree", "add", "--", worktreePath, newRefName ?? input.refName];
const progress = options?.progress;
const onCheckoutProgress = progress?.onCheckoutProgress;
@@ -4234,6 +4235,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
: {}),
},
+ ).pipe(
+ Effect.onError(() =>
+ newRefName
+ ? deleteLocalBranch({ cwd: input.cwd, refName: newRefName, force: true }).pipe(
+ Effect.catch((cause) =>
+ Effect.logWarning("could not remove the branch of a failed worktree", { cause }),
+ ),
+ )
+ : Effect.void,
+ ),
);
if (progress?.onWorktreeClaimed) {
@@ -4319,7 +4330,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
);
}
- const newRefName = input.newRefName;
if (newRefName && base) {
yield* Effect.gen(function* () {
yield* writeConfigValue({