Skip to content

Commit 57518b4

Browse files
fix(server): probe the Windows startup environment asynchronously
1 parent 2532e64 commit 57518b4

3 files changed

Lines changed: 220 additions & 147 deletions

File tree

‎apps/server/src/os-jank.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second
162162
yield* fixPath().pipe(
163163
Effect.provideService(HostProcess.Platform, "win32"),
164164
Effect.provideService(HostProcess.Environment, env),
165-
Effect.provideService(WindowsShellEnvironment, () => ({ PATH: "C:\\Tools" })),
165+
Effect.provideService(WindowsShellEnvironment, () => Effect.succeed({ PATH: "C:\\Tools" })),
166166
Effect.provideService(CommandAvailability, () => Effect.succeed(true)),
167167
Effect.provide(NodeServices.layer),
168168
);

‎packages/shared/src/shell.test.ts‎

Lines changed: 172 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { it as effectIt } from "@effect/vitest";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
44
import * as Effect from "effect/Effect";
5+
import * as Deferred from "effect/Deferred";
6+
import * as Fiber from "effect/Fiber";
57
import * as Exit from "effect/Exit";
68
import * as FileSystem from "effect/FileSystem";
79
import * as Path from "effect/Path";
10+
import * as Option from "effect/Option";
811
import * as Result from "effect/Result";
912
import * as TestClock from "effect/testing/TestClock";
1013
import * as Tracer from "effect/Tracer";
@@ -36,11 +39,15 @@ import {
3639

3740
const withWindowsEnvironmentMocks = <A, E, R>(
3841
effect: Effect.Effect<A, E, R>,
39-
readEnvironment: WindowsShellEnvironmentReader,
42+
readEnvironment: (
43+
...args: Parameters<WindowsShellEnvironmentReader>
44+
) => Partial<Record<string, string>>,
4045
commandAvailable: CommandAvailabilityChecker,
4146
) =>
4247
effect.pipe(
43-
Effect.provideService(WindowsShellEnvironment, readEnvironment),
48+
Effect.provideService(WindowsShellEnvironment, (...args) =>
49+
Effect.sync(() => readEnvironment(...args)),
50+
),
4451
Effect.provideService(CommandAvailability, commandAvailable),
4552
);
4653

@@ -218,91 +225,176 @@ describe("mergePathEntries", () => {
218225
});
219226

220227
describe("readEnvironmentFromWindowsShell", () => {
221-
it("extracts environment variables from a PowerShell command", () => {
222-
const execFile = vi.fn<
223-
(
224-
file: string,
225-
args: ReadonlyArray<string>,
226-
options: { encoding: "utf8"; timeout: number },
227-
) => string
228-
>(
229-
() =>
230-
"__T3CODE_ENV_PATH_START__\nC:\\Users\\testuser\\AppData\\Roaming\\npm\n__T3CODE_ENV_PATH_END__\n",
231-
);
228+
effectIt.effect("yields while the native shell probe is pending", () =>
229+
Effect.gen(function* () {
230+
let completed = false;
231+
const [environment, completedBeforeProbeReturned] = yield* Effect.all(
232+
[
233+
readEnvironmentFromWindowsShell(["PATH"]).pipe(
234+
Effect.tap(() =>
235+
Effect.sync(() => {
236+
completed = true;
237+
}),
238+
),
239+
),
240+
Effect.sync(() => completed),
241+
],
242+
{ concurrency: 2 },
243+
);
244+
expect(completedBeforeProbeReturned).toBe(false);
245+
expect(environment).toBeTypeOf("object");
246+
}),
247+
);
232248

233-
expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
234-
PATH: "C:\\Users\\testuser\\AppData\\Roaming\\npm",
235-
});
236-
expect(execFile).toHaveBeenCalledWith(
237-
"pwsh.exe",
238-
expect.arrayContaining(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]),
239-
{ encoding: "utf8", timeout: 5000 },
240-
);
241-
});
249+
effectIt.effect("returns an empty environment after both shell candidates fail", () =>
250+
Effect.gen(function* () {
251+
const execFile = vi.fn(() => Effect.succeed(Option.none<string>()));
252+
expect(yield* readEnvironmentFromWindowsShell(["PATH"], {}, execFile)).toEqual({});
253+
expect(execFile).toHaveBeenCalledTimes(2);
254+
}),
255+
);
242256

243-
it("strips CRLF delimiters from captured PowerShell values", () => {
244-
const execFile = vi.fn<
245-
(
246-
file: string,
247-
args: ReadonlyArray<string>,
248-
options: { encoding: "utf8"; timeout: number },
249-
) => string
250-
>(
251-
() =>
252-
"__T3CODE_ENV_FNM_DIR_START__\r\nC:\\Users\\testuser\\AppData\\Roaming\\fnm\r\n__T3CODE_ENV_FNM_DIR_END__\r\n",
253-
);
257+
effectIt.effect("does not launch a shell when no variables are requested", () =>
258+
Effect.gen(function* () {
259+
const execFile = vi.fn(() => Effect.succeed(Option.none<string>()));
260+
expect(yield* readEnvironmentFromWindowsShell([], {}, execFile)).toEqual({});
261+
expect(execFile).not.toHaveBeenCalled();
262+
}),
263+
);
254264

255-
expect(readEnvironmentFromWindowsShell(["FNM_DIR"], execFile)).toEqual({
256-
FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
257-
});
258-
});
265+
effectIt.effect("interrupts a pending probe without trying the next shell", () =>
266+
Effect.gen(function* () {
267+
const started = yield* Deferred.make<void>();
268+
let interrupted = false;
269+
const execFile = vi.fn(() =>
270+
Deferred.succeed(started, undefined).pipe(
271+
Effect.andThen(Effect.never),
272+
Effect.onInterrupt(() =>
273+
Effect.sync(() => {
274+
interrupted = true;
275+
}),
276+
),
277+
),
278+
);
279+
const fiber = yield* readEnvironmentFromWindowsShell(["PATH"], {}, execFile).pipe(
280+
Effect.forkChild,
281+
);
282+
yield* Deferred.await(started);
283+
yield* Fiber.interrupt(fiber);
284+
expect(interrupted).toBe(true);
285+
expect(execFile).toHaveBeenCalledTimes(1);
286+
}),
287+
);
259288

260-
it("omits -NoProfile when loadProfile is enabled", () => {
261-
const execFile = vi.fn<
262-
(
263-
file: string,
264-
args: ReadonlyArray<string>,
265-
options: { encoding: "utf8"; timeout: number },
266-
) => string
267-
>(() => "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n");
289+
effectIt.effect("extracts environment variables from a PowerShell command", () =>
290+
Effect.gen(function* () {
291+
const execFile = vi.fn<
292+
(
293+
file: string,
294+
args: ReadonlyArray<string>,
295+
options: { encoding: "utf8"; timeout: number },
296+
) => string
297+
>(
298+
() =>
299+
"__T3CODE_ENV_PATH_START__\nC:\\Users\\testuser\\AppData\\Roaming\\npm\n__T3CODE_ENV_PATH_END__\n",
300+
);
268301

269-
expect(readEnvironmentFromWindowsShell(["PATH"], { loadProfile: true }, execFile)).toEqual({
270-
PATH: "C:\\Tools",
271-
});
272-
expect(execFile).toHaveBeenCalledWith(
273-
"pwsh.exe",
274-
expect.arrayContaining(["-NoLogo", "-NonInteractive", "-Command"]),
275-
{ encoding: "utf8", timeout: 5000 },
276-
);
277-
expect(execFile.mock.calls[0]?.[1]).not.toContain("-NoProfile");
278-
});
302+
expect(
303+
yield* readEnvironmentFromWindowsShell(["PATH"], {}, (...args) =>
304+
Effect.try(() => execFile(...args)).pipe(Effect.option),
305+
),
306+
).toEqual({
307+
PATH: "C:\\Users\\testuser\\AppData\\Roaming\\npm",
308+
});
309+
expect(execFile).toHaveBeenCalledWith(
310+
"pwsh.exe",
311+
expect.arrayContaining(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]),
312+
{ encoding: "utf8", timeout: 5000 },
313+
);
314+
}),
315+
);
279316

280-
it("falls back to Windows PowerShell when pwsh.exe is unavailable", () => {
281-
const execFile = vi.fn<
282-
(
283-
file: string,
284-
args: ReadonlyArray<string>,
285-
options: { encoding: "utf8"; timeout: number },
286-
) => string
287-
>((file) => {
288-
if (file === "pwsh.exe") {
289-
throw new Error("spawn pwsh.exe ENOENT");
290-
}
291-
return "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n";
292-
});
317+
effectIt.effect("strips CRLF delimiters from captured PowerShell values", () =>
318+
Effect.gen(function* () {
319+
const execFile = vi.fn<
320+
(
321+
file: string,
322+
args: ReadonlyArray<string>,
323+
options: { encoding: "utf8"; timeout: number },
324+
) => string
325+
>(
326+
() =>
327+
"__T3CODE_ENV_FNM_DIR_START__\r\nC:\\Users\\testuser\\AppData\\Roaming\\fnm\r\n__T3CODE_ENV_FNM_DIR_END__\r\n",
328+
);
293329

294-
expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
295-
PATH: "C:\\Tools",
296-
});
297-
expect(execFile).toHaveBeenNthCalledWith(1, "pwsh.exe", expect.any(Array), {
298-
encoding: "utf8",
299-
timeout: 5000,
300-
});
301-
expect(execFile).toHaveBeenNthCalledWith(2, "powershell.exe", expect.any(Array), {
302-
encoding: "utf8",
303-
timeout: 5000,
304-
});
305-
});
330+
expect(
331+
yield* readEnvironmentFromWindowsShell(["FNM_DIR"], {}, (...args) =>
332+
Effect.try(() => execFile(...args)).pipe(Effect.option),
333+
),
334+
).toEqual({
335+
FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
336+
});
337+
}),
338+
);
339+
340+
effectIt.effect("omits -NoProfile when loadProfile is enabled", () =>
341+
Effect.gen(function* () {
342+
const execFile = vi.fn<
343+
(
344+
file: string,
345+
args: ReadonlyArray<string>,
346+
options: { encoding: "utf8"; timeout: number },
347+
) => string
348+
>(() => "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n");
349+
350+
expect(
351+
yield* readEnvironmentFromWindowsShell(["PATH"], { loadProfile: true }, (...args) =>
352+
Effect.try(() => execFile(...args)).pipe(Effect.option),
353+
),
354+
).toEqual({
355+
PATH: "C:\\Tools",
356+
});
357+
expect(execFile).toHaveBeenCalledWith(
358+
"pwsh.exe",
359+
expect.arrayContaining(["-NoLogo", "-NonInteractive", "-Command"]),
360+
{ encoding: "utf8", timeout: 5000 },
361+
);
362+
expect(execFile.mock.calls[0]?.[1]).not.toContain("-NoProfile");
363+
}),
364+
);
365+
366+
effectIt.effect("falls back to Windows PowerShell when pwsh.exe is unavailable", () =>
367+
Effect.gen(function* () {
368+
const execFile = vi.fn<
369+
(
370+
file: string,
371+
args: ReadonlyArray<string>,
372+
options: { encoding: "utf8"; timeout: number },
373+
) => string
374+
>((file) => {
375+
if (file === "pwsh.exe") {
376+
throw new Error("spawn pwsh.exe ENOENT");
377+
}
378+
return "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n";
379+
});
380+
381+
expect(
382+
yield* readEnvironmentFromWindowsShell(["PATH"], {}, (...args) =>
383+
Effect.try(() => execFile(...args)).pipe(Effect.option),
384+
),
385+
).toEqual({
386+
PATH: "C:\\Tools",
387+
});
388+
expect(execFile).toHaveBeenNthCalledWith(1, "pwsh.exe", expect.any(Array), {
389+
encoding: "utf8",
390+
timeout: 5000,
391+
});
392+
expect(execFile).toHaveBeenNthCalledWith(2, "powershell.exe", expect.any(Array), {
393+
encoding: "utf8",
394+
timeout: 5000,
395+
});
396+
}),
397+
);
306398
});
307399

308400
describe("mergePathValues", () => {

0 commit comments

Comments
 (0)