Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,61 @@ effectIt.effect("restores a missing worktree before a send only under the worksp
}).pipe(Effect.provide(layerRestoreLeaseTest)),
);

effectIt.effect("restores into a leftover worktree folder that cannot itself be removed", () => {
const worktreePath = `${managedWorktreesDir}/project/feature-restore`;
// Windows refuses to remove a folder that is still some process's cwd.
const remove = vi.fn((target: string) =>
target === worktreePath
? Effect.fail(
PlatformError.systemError({
_tag: "Busy",
module: "FileSystem",
method: "remove",
pathOrDescriptor: target,
}),
)
: Effect.void,
);
const createWorktree = vi.fn(() => Effect.succeed({} as never));
return Effect.gen(function* () {
const { restore } = yield* ThreadWorktreeRestore;
yield* restore({
id: ThreadId.make("thread_leftover_restore"),
projectId: ProjectId.make("project_leftover_restore"),
worktreePath,
branch: "feature/restore",
});
expect(remove).toHaveBeenCalledWith(`${worktreePath}/node_modules`, { recursive: true });
expect(createWorktree).toHaveBeenCalledTimes(1);
}).pipe(
Effect.provide(
layerThreadWorktreeRestore.pipe(
Layer.provide(
Layer.mergeAll(
Layer.succeed(FileSystem.FileSystem, {
exists: (target: string) => Effect.succeed(!target.endsWith("/.git")),
readDirectory: () => Effect.succeed(["node_modules"]),
stat: () => Effect.succeed({ type: "Directory" }),
realPath: (target: string) => Effect.succeed(normalize(target)),
remove,
} as never),
layerWorktreeLocation(),
Layer.mock(GitWorkflow.GitWorkflowService)({
pruneWorktrees: () => Effect.void,
createWorktree,
listLocalBranchNames: () => Effect.succeed(["feature/restore"]),
}),
Layer.mock(ProjectService.ProjectService)({
getById: () =>
Effect.succeedSome({ workspaceRoot: "/tmp/provider-turn-start-project" } as never),
}),
),
),
),
),
);
});

it.each([
{ label: "directly in the scratch root", folder: "/tmp/t3-home/scratch/notes", recreated: true },
{ label: "below another folder", folder: "/tmp/t3-home/scratch/notes/deep", recreated: false },
Expand Down Expand Up @@ -2445,7 +2500,9 @@ effectIt.effect.each(["cleanup-first", "startup-first"])(
failReadsAfterRunning: true,
});
harness.createWorktree.mockImplementation(() =>
fileSystem.makeDirectory(worktreePath).pipe(Effect.as({} as never), Effect.orDie),
fileSystem
.makeDirectory(worktreePath, { recursive: true })
.pipe(Effect.as({} as never), Effect.orDie),
);
const open = harness.open.getMockImplementation()!;
harness.open.mockImplementation(() =>
Expand Down
14 changes: 12 additions & 2 deletions apps/server/src/workspace/restoreMissingWorktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,18 @@ export const makeRestoreMissingWorktree = Effect.gen(function* () {
worktreePath,
branch,
});
// `git worktree add` refuses a non-empty target.
yield* (leftover ? fileSystem.remove(worktreePath, { recursive: true }) : Effect.void).pipe(
// `git worktree add` refuses a non-empty target but accepts an empty one. Keep the folder
// itself: Windows cannot remove a folder that is still a terminal's or provider's cwd.
const clearLeftovers = Effect.suspend(() => fileSystem.readDirectory(worktreePath)).pipe(
Effect.flatMap((entries) =>
Effect.forEach(
entries,
(entry) => fileSystem.remove(path.join(worktreePath, entry), { recursive: true }),
{ discard: true },
),
),
);
yield* (leftover ? clearLeftovers : Effect.void).pipe(
Effect.andThen(recreateMissingWorktree({ cwd: project.workspaceRoot, worktreePath, branch })),
Effect.mapError((cause) =>
cause._tag === "WorktreeRecreateNoBaseError"
Expand Down
Loading