Skip to content

Commit 15b753d

Browse files
SunkenInTimeclaude
andcommitted
fix(server): only remove leftover worktree folders while they are empty
Listing a folder and then removing it recursively deletes anything written in between. rmdir refuses a folder that is no longer empty. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5596196 commit 15b753d

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
3535
import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings";
3636
import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
3737
import * as GitVcsDriver from "./GitVcsDriver.ts";
38+
import { removeEmptyDirectory } from "./removeEmptyDirectory.ts";
3839
import {
3940
parseRemoteNames,
4041
parseRemoteNamesInGitOrder,
@@ -3663,9 +3664,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36633664
for (const name of yield* fileSystem.readDirectory(target)) {
36643665
if (!(yield* removeLeftoverLinks(path.join(target, name)))) empty = false;
36653666
}
3666-
// Node's rm refuses a directory without `recursive`, even an empty one.
3667-
if (empty) yield* fileSystem.remove(target, { recursive: true });
3668-
return empty;
3667+
// Only an empty directory goes, so files written since the listing stay.
3668+
return empty && (yield* removeEmptyDirectory(target));
36693669
});
36703670

36713671
const removeWorktree: GitVcsDriver.GitVcsDriver["Service"]["removeWorktree"] = Effect.fn(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Effect's FileSystem has no rmdir.
2+
import * as NodeFSP from "node:fs/promises";
3+
4+
import * as Effect from "effect/Effect";
5+
6+
/**
7+
* Removes `path` only while it is an empty directory. Succeeds with false when
8+
* anything else is there, including files written after the caller looked.
9+
*/
10+
export const removeEmptyDirectory = (path: string) =>
11+
Effect.tryPromise(() => NodeFSP.rmdir(path)).pipe(
12+
Effect.as(true),
13+
Effect.orElseSucceed(() => false),
14+
);

0 commit comments

Comments
 (0)