Skip to content

Commit 344b395

Browse files
SunkenInTimeclaude
andcommitted
fix(server): clean up the same worktree path Git removed
Git also accepts a worktree's short name and matches it against registered worktrees, while the leftover cleanup resolved the argument against cwd. With a worktree named `short` elsewhere and an unrelated `short` folder in the repository, Git removed the worktree and cleanup then unlinked the unrelated folder's links. removeWorktree now resolves the path once and gives Git and the cleanup the same absolute path. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 77ff621 commit 344b395

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,33 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
29212921
}),
29222922
);
29232923

2924+
it.effect("leaves a folder alone that only shares the worktree's short name", () =>
2925+
Effect.gen(function* () {
2926+
const cwd = yield* makeTmpDir();
2927+
const { initialBranch } = yield* initRepoWithCommit(cwd);
2928+
const pathService = yield* Path.Path;
2929+
const fileSystem = yield* FileSystem.FileSystem;
2930+
const driver = yield* GitVcsDriver.GitVcsDriver;
2931+
const worktreePath = pathService.join(yield* makeTmpDir("git-worktrees-"), "short");
2932+
yield* driver.createWorktree({
2933+
cwd,
2934+
path: worktreePath,
2935+
refName: initialBranch,
2936+
newRefName: "feature/short",
2937+
});
2938+
// Unrelated to the worktree, but resolving "short" against cwd lands here.
2939+
const outside = yield* makeTmpDir("git-worktree-link-target-");
2940+
const unrelated = pathService.join(cwd, "short");
2941+
NodeFS.mkdirSync(unrelated);
2942+
NodeFS.symlinkSync(outside, pathService.join(unrelated, "linked"), "junction");
2943+
2944+
yield* Effect.result(driver.removeWorktree({ cwd, path: "short" }));
2945+
2946+
assert.isTrue(NodeFS.lstatSync(pathService.join(unrelated, "linked")).isSymbolicLink());
2947+
assert.equal(yield* fileSystem.exists(worktreePath), true);
2948+
}),
2949+
);
2950+
29242951
it.effect("allows worktree removal to run longer than the default command timeout", () =>
29252952
Effect.gen(function* () {
29262953
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,11 +3672,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36723672
const removeWorktree: GitVcsDriver.GitVcsDriver["Service"]["removeWorktree"] = Effect.fn(
36733673
"removeWorktree",
36743674
)(function* (input) {
3675+
// Git also accepts a worktree's short name, which can match a different
3676+
// folder than the path resolved against `input.cwd`. Resolve it once so Git
3677+
// and the leftover cleanup below act on the same directory.
3678+
const target = path.resolve(input.cwd, input.path);
36753679
const args = ["worktree", "remove"];
36763680
if (input.force) {
36773681
args.push("--force");
36783682
}
3679-
args.push(input.path);
3683+
args.push(target);
36803684
const result = yield* executeGitWithStableDiagnostics(
36813685
"GitVcsDriver.removeWorktree",
36823686
input.cwd,
@@ -3696,20 +3700,19 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36963700
// recreating its checkout. Git has already unregistered the worktree, so
36973701
// anything left here is logged rather than returned: a retry could never
36983702
// succeed.
3699-
const leftover = path.resolve(input.cwd, input.path);
3700-
if (yield* fileSystem.exists(leftover).pipe(Effect.orElseSucceed(() => false))) {
3701-
const removed = yield* removeLeftoverLinks(leftover).pipe(
3703+
if (yield* fileSystem.exists(target).pipe(Effect.orElseSucceed(() => false))) {
3704+
const removed = yield* removeLeftoverLinks(target).pipe(
37023705
Effect.catch((error) =>
37033706
Effect.logWarning("GitVcsDriver.removeWorktree: failed to delete leftover links", {
3704-
path: leftover,
3707+
path: target,
37053708
error,
37063709
}).pipe(Effect.as(true)),
37073710
),
37083711
);
37093712
if (!removed) {
37103713
yield* Effect.logWarning(
37113714
"GitVcsDriver.removeWorktree: kept files written after git removed the worktree",
3712-
{ path: leftover },
3715+
{ path: target },
37133716
);
37143717
}
37153718
}

0 commit comments

Comments
 (0)