@@ -4019,6 +4019,57 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
40194019 }),
40204020 );
40214021
4022+ it.effect.each([false, true])(
4023+ "cleans up links left by successful worktree removal, preserving ordinary files: %s",
4024+ (keepFile) =>
4025+ Effect.gen(function* () {
4026+ const cwd = yield* makeTmpDir();
4027+ const fileSystem = yield* FileSystem.FileSystem;
4028+ const pathService = yield* Path.Path;
4029+ const outside = yield* makeTmpDir("git-worktree-link-target-");
4030+ yield* writeTextFile(outside, "keep.txt", "external target\n");
4031+ const worktreePath = pathService.join(cwd, "leftover");
4032+ const nested = pathService.join(worktreePath, "node_modules");
4033+ yield* fileSystem.makeDirectory(nested, { recursive: true });
4034+ yield* fileSystem.makeDirectory(pathService.join(worktreePath, "empty"));
4035+ NodeFS.symlinkSync(outside, pathService.join(worktreePath, "linked"), "junction");
4036+ NodeFS.symlinkSync(outside, pathService.join(nested, "pkg"), "junction");
4037+ if (keepFile) {
4038+ yield* writeTextFile(worktreePath, "notes/draft.txt", "new work\n");
4039+ }
4040+
4041+ // Git removes symlinks on Unix; simulate its successful Windows removal
4042+ // leaving junctions behind so the cleanup is exercised on every platform.
4043+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
4044+ const spawner = ChildProcessSpawner.make((command) =>
4045+ ChildProcess.isStandardCommand(command) &&
4046+ command.args.join(" ").includes("worktree remove")
4047+ ? Effect.succeed(makeSuccessfulHandle(""))
4048+ : delegate.spawn(command),
4049+ );
4050+ const driver = yield* makeGitVcsDriverCore().pipe(
4051+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4052+ Effect.provide(layerServerConfig),
4053+ );
4054+
4055+ yield* driver.removeWorktree({ cwd, path: worktreePath });
4056+
4057+ assert.equal(
4058+ yield* fileSystem.readFileString(pathService.join(outside, "keep.txt")),
4059+ "external target\n",
4060+ );
4061+ if (keepFile) {
4062+ assert.deepEqual(yield* fileSystem.readDirectory(worktreePath), ["notes"]);
4063+ assert.equal(
4064+ yield* fileSystem.readFileString(pathService.join(worktreePath, "notes/draft.txt")),
4065+ "new work\n",
4066+ );
4067+ } else {
4068+ assert.equal(yield* fileSystem.exists(worktreePath), false);
4069+ }
4070+ }),
4071+ );
4072+
40224073 it.effect("keeps a worktree whose untracked files status is configured to hide", () =>
40234074 Effect.gen(function* () {
40244075 const cwd = yield* makeTmpDir();
@@ -4047,6 +4098,33 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
40474098 }),
40484099 );
40494100
4101+ it.effect("leaves a folder alone that only shares the worktree's short name", () =>
4102+ Effect.gen(function* () {
4103+ const cwd = yield* makeTmpDir();
4104+ const { initialBranch } = yield* initRepoWithCommit(cwd);
4105+ const pathService = yield* Path.Path;
4106+ const fileSystem = yield* FileSystem.FileSystem;
4107+ const driver = yield* GitVcsDriver.GitVcsDriver;
4108+ const worktreePath = pathService.join(yield* makeTmpDir("git-worktrees-"), "short");
4109+ yield* driver.createWorktree({
4110+ cwd,
4111+ path: worktreePath,
4112+ refName: initialBranch,
4113+ newRefName: "feature/short",
4114+ });
4115+ // Unrelated to the worktree, but resolving "short" against cwd lands here.
4116+ const outside = yield* makeTmpDir("git-worktree-link-target-");
4117+ const unrelated = pathService.join(cwd, "short");
4118+ NodeFS.mkdirSync(unrelated);
4119+ NodeFS.symlinkSync(outside, pathService.join(unrelated, "linked"), "junction");
4120+
4121+ yield* Effect.result(driver.removeWorktree({ cwd, path: "short" }));
4122+
4123+ assert.isTrue(NodeFS.lstatSync(pathService.join(unrelated, "linked")).isSymbolicLink());
4124+ assert.equal(yield* fileSystem.exists(worktreePath), true);
4125+ }),
4126+ );
4127+
40504128 it.effect("allows worktree removal to run longer than the default command timeout", () =>
40514129 Effect.gen(function* () {
40524130 const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
0 commit comments