Skip to content

Commit 7411af7

Browse files
committed
fix(server): preserve setup completion when the shell exits early
1 parent d3fd5a9 commit 7411af7

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
22
import { describe, expect, it } from "@effect/vitest";
3-
import { ProjectId, type TerminalEvent, type TerminalSessionSnapshot } from "@t3tools/contracts";
3+
import {
4+
ProjectId,
5+
TerminalNotRunningError,
6+
type TerminalEvent,
7+
type TerminalSessionSnapshot,
8+
} from "@t3tools/contracts";
49
import * as Deferred from "effect/Deferred";
510
import * as Effect from "effect/Effect";
611
import * as Fiber from "effect/Fiber";
@@ -35,7 +40,7 @@ const openSnapshot = (history: string): TerminalSessionSnapshot => ({
3540
const makeHarness = Effect.fn(function* (options: {
3641
readonly history?: string;
3742
readonly opening?: Deferred.Deferred<void>;
38-
readonly shell?: string;
43+
readonly shell?: string | null;
3944
}) {
4045
const listeners = new Set<(event: TerminalEvent) => Effect.Effect<void>>();
4146
const writes = yield* Queue.unbounded<string>();
@@ -46,7 +51,10 @@ const makeHarness = Effect.fn(function* (options: {
4651
? Deferred.succeed(options.opening, undefined).pipe(Effect.andThen(Effect.never))
4752
: Effect.void
4853
).pipe(Effect.as(openSnapshot(options.history ?? ""))),
49-
getShell: () => Effect.succeed(options.shell ?? "/bin/zsh"),
54+
getShell: () =>
55+
options.shell === null
56+
? Effect.fail(new TerminalNotRunningError({ threadId, terminalId }))
57+
: Effect.succeed(options.shell ?? "/bin/zsh"),
5058
subscribe: (listener) =>
5159
Effect.sync(() => {
5260
listeners.add(listener);
@@ -94,7 +102,7 @@ const makeHarness = Effect.fn(function* (options: {
94102
const setup = Effect.fn(function* (
95103
options: {
96104
readonly history?: string;
97-
readonly shell?: string;
105+
readonly shell?: string | null;
98106
} = {},
99107
) {
100108
const harness = yield* makeHarness(options);
@@ -173,13 +181,20 @@ describe("ProjectSetupScriptRunner", () => {
173181
}),
174182
);
175183

176-
it.effect("does not type into a shell that exits during startup", () =>
177-
Effect.gen(function* () {
178-
const { completion, writes, emit } = yield* setup();
179-
yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null });
180-
expect(yield* completion).toMatchObject({ exitCode: null });
181-
yield* TestClock.adjust("1 minute");
182-
expect(yield* Queue.size(writes)).toBe(0);
183-
}),
184+
it.effect.each([false, true])(
185+
"does not type into a shell that exits during startup (before shell lookup: %s)",
186+
(beforeShellLookup) =>
187+
Effect.gen(function* () {
188+
const { completion, writes, emit, listeners } = yield* setup(
189+
beforeShellLookup ? { shell: null } : {},
190+
);
191+
if (!beforeShellLookup) {
192+
yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null });
193+
}
194+
expect(yield* completion).toMatchObject({ exitCode: null });
195+
yield* TestClock.adjust("1 minute");
196+
expect(yield* Queue.size(writes)).toBe(0);
197+
expect(listeners.size).toBe(0);
198+
}),
184199
);
185200
});

‎apps/server/src/project/ProjectSetupScriptRunner.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,11 @@ export const make = Effect.gen(function* () {
473473
env: { ...env, NO_COLOR: "1", FORCE_COLOR: "0" },
474474
})
475475
.pipe(
476-
Effect.andThen(() => terminalManager.getShell({ threadId: input.threadId, terminalId })),
476+
Effect.andThen(() =>
477+
terminalManager
478+
.getShell({ threadId: input.threadId, terminalId })
479+
.pipe(Effect.catchTags({ TerminalNotRunningError: () => Effect.succeed(null) })),
480+
),
477481
Effect.mapError(
478482
(cause) =>
479483
new ProjectSetupScriptOperationError({
@@ -483,6 +487,10 @@ export const make = Effect.gen(function* () {
483487
}),
484488
),
485489
);
490+
if (shell === null) {
491+
yield* observed?.abandon ?? Effect.void;
492+
return { completionShell: null, commandLine: null };
493+
}
486494
const completionShell = resolveCompletionShell(shell);
487495
let commandLine = script.command;
488496
if (observe && completionToken) {
@@ -515,7 +523,7 @@ export const make = Effect.gen(function* () {
515523
: Effect.succeed("ready" as const)
516524
).pipe(
517525
Effect.flatMap((shell) =>
518-
shell === "exited"
526+
shell === "exited" || commandLine === null
519527
? Effect.void
520528
: terminalManager.write({
521529
threadId: input.threadId,

0 commit comments

Comments
 (0)