|
1 | | -import { spawnSync } from "node:child_process" |
| 1 | +import { spawn } from "node:child_process" |
2 | 2 | import { userInfo } from "node:os" |
3 | 3 | import { Effect, Path } from "effect" |
4 | 4 |
|
5 | 5 | const TIMEOUT = 5_000 |
6 | 6 |
|
7 | 7 | type Probe = { type: "Loaded"; value: Record<string, string> } | { type: "Timeout" } | { type: "Unavailable" } |
8 | 8 |
|
| 9 | +type Run = |
| 10 | + | { type: "Exited"; status: number | null; stdout: Buffer } |
| 11 | + | { type: "Error"; error: Error } |
| 12 | + | { type: "Timeout" } |
| 13 | + |
9 | 14 | export function resolveUserShell(envShell: string | undefined, loginShell: string | null | undefined) { |
10 | 15 | const resolvedLoginShell = loginShell && loginShell !== "unknown" ? loginShell : undefined |
11 | 16 |
|
@@ -34,18 +39,32 @@ export function parseShellEnv(out: Buffer) { |
34 | 39 | return env |
35 | 40 | } |
36 | 41 |
|
| 42 | +// Async so the main thread keeps painting the window and serving renderer assets while a slow rc file runs. |
| 43 | +const run = (shell: string, mode: "-il" | "-l") => |
| 44 | + Effect.callback<Run>((resume) => { |
| 45 | + const child = spawn(shell, [mode, "-c", "env -0"], { |
| 46 | + stdio: ["ignore", "pipe", "ignore"], |
| 47 | + windowsHide: true, |
| 48 | + }) |
| 49 | + |
| 50 | + const chunks: Buffer[] = [] |
| 51 | + |
| 52 | + child.stdout.on("data", (chunk: Buffer) => chunks.push(chunk)) |
| 53 | + child.on("error", (error) => resume(Effect.succeed({ type: "Error", error }))) |
| 54 | + child.on("close", (status) => resume(Effect.succeed({ type: "Exited", status, stdout: Buffer.concat(chunks) }))) |
| 55 | + |
| 56 | + return Effect.sync(() => { |
| 57 | + if (child.exitCode === null && child.signalCode === null) child.kill() |
| 58 | + }) |
| 59 | + }).pipe(Effect.timeoutOrElse({ duration: TIMEOUT, orElse: () => Effect.succeed<Run>({ type: "Timeout" }) })) |
| 60 | + |
37 | 61 | const probe = Effect.fn("ShellEnv.probe")(function* (shell: string, mode: "-il" | "-l") { |
38 | | - const out = spawnSync(shell, [mode, "-c", "env -0"], { |
39 | | - stdio: ["ignore", "pipe", "ignore"], |
40 | | - timeout: TIMEOUT, |
41 | | - windowsHide: true, |
42 | | - }) |
| 62 | + const out = yield* run(shell, mode) |
43 | 63 |
|
44 | | - const err = out.error as NodeJS.ErrnoException | undefined |
| 64 | + if (out.type === "Timeout") return { type: "Timeout" } satisfies Probe |
45 | 65 |
|
46 | | - if (err) { |
47 | | - if (err.code === "ETIMEDOUT") return { type: "Timeout" } satisfies Probe |
48 | | - yield* Effect.logWarning(`[server] Shell env probe failed for ${shell} ${mode}: ${err.message}`) |
| 66 | + if (out.type === "Error") { |
| 67 | + yield* Effect.logWarning(`[server] Shell env probe failed for ${shell} ${mode}: ${out.error.message}`) |
49 | 68 |
|
50 | 69 | return { type: "Unavailable" } satisfies Probe |
51 | 70 | } |
|
0 commit comments