Skip to content

Commit 76e4cb5

Browse files
authored
fix(server): removing a worktree on Windows no longer spins the server until it hangs (#34774)
1 parent 7b17bdb commit 76e4cb5

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,50 @@ describe("VcsStatusBroadcaster", () => {
15271527
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
15281528
);
15291529

1530+
it.effect("a HEAD watch ends when its git directory is removed", () =>
1531+
Effect.gen(function* () {
1532+
const gitDir = "/repo/.git/worktrees/feature";
1533+
const events = yield* Queue.unbounded<FileSystem.WatchEvent>();
1534+
const closed = yield* Deferred.make<void>();
1535+
let gitDirExists = true;
1536+
const state: Parameters<typeof layerTestFor>[0] = {
1537+
currentLocalStatus: { ...baseLocalStatus, refName: "main" },
1538+
currentRemoteStatus: null,
1539+
localStatusCalls: 0,
1540+
remoteStatusCalls: 0,
1541+
localInvalidationCalls: 0,
1542+
remoteInvalidationCalls: 0,
1543+
headPath: `${gitDir}/HEAD`,
1544+
fileSystem: FileSystem.makeNoop({
1545+
realPath: (target) => Effect.succeed(target),
1546+
exists: (target) => Effect.succeed(target !== gitDir || gitDirExists),
1547+
watch: (_target, options) =>
1548+
Stream.fromQueue(events).pipe(
1549+
Stream.onStart(Effect.sync(() => options?.onReady?.())),
1550+
Stream.ensuring(Deferred.succeed(closed, undefined)),
1551+
),
1552+
}),
1553+
};
1554+
1555+
yield* Effect.gen(function* () {
1556+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
1557+
const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
1558+
assert.deepEqual(yield* branches, [{ refName: "main" }]);
1559+
1560+
// Every checkout removes HEAD.lock, which leaves the watch running.
1561+
state.currentLocalStatus = { ...baseLocalStatus, refName: "other" };
1562+
yield* Queue.offer(events, { _tag: "Remove", path: "HEAD.lock" });
1563+
yield* Queue.offer(events, { _tag: "Update", path: "HEAD" });
1564+
assert.deepEqual(yield* branches, [{ refName: "other" }]);
1565+
1566+
// `git worktree remove` deletes the watched directory; Windows keeps reporting it.
1567+
gitDirExists = false;
1568+
yield* Queue.offer(events, { _tag: "Remove", path: gitDir });
1569+
yield* Deferred.await(closed);
1570+
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
1571+
}),
1572+
);
1573+
15301574
it.effect("releases remote demand while passive observers remain", () => {
15311575
const state = {
15321576
currentLocalStatus: baseLocalStatus,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,19 @@ export const make = Effect.gen(function* () {
822822
const watchHead = Effect.fnUntraced(function* (cwd: string, ready: Deferred.Deferred<void>) {
823823
const headPath = yield* workflow.headPath(cwd);
824824
if (headPath === null) return;
825+
const headDirectory = path.dirname(headPath);
825826
// Git replaces HEAD by renaming HEAD.lock, which a watch on the file itself would lose.
826827
yield* fs
827-
.watch(path.dirname(headPath), {
828+
.watch(headDirectory, {
828829
onReady: () => Deferred.doneUnsafe(ready, Exit.void),
829830
})
830831
.pipe(
832+
// On Windows a watch outlives its deleted directory and reports it ~150k times a second.
833+
Stream.takeUntilEffect((event) =>
834+
event._tag === "Remove"
835+
? fs.exists(headDirectory).pipe(Effect.map((exists) => !exists))
836+
: Effect.succeed(false),
837+
),
831838
Stream.filter((event) => path.basename(event.path) === "HEAD"),
832839
// A rebase moves HEAD many times; refresh once for whatever arrived during a refresh.
833840
Stream.buffer({ capacity: 1, strategy: "sliding" }),

0 commit comments

Comments
 (0)