Skip to content

Commit eaf9336

Browse files
author
baggiiiie
committed
fix: update desktop/server PATH resolution and startup check for codex
Bug (related to #234): - codex availability checks can fail at startup because the app/server process gets an incorrect or incomplete PATH, causing startup spawn failures and false “codex not installed/on PATH” errors. - Server health probes could run before PATH repair during layer initialization. - crash loop observed: ``` @t3tools/desktop:start: [10:59:35.373] INFO (#10): Migrations ran successfully @t3tools/desktop:start: [10:59:35.375] ERROR (#10): RangeError: The value of "err" is out of range. It must be a negative integer. Received 1 @t3tools/desktop:start: at Object.getSystemErrorName (node:util:331:11) @t3tools/desktop:start: at new ErrnoException (node:internal/errors:733:23) @t3tools/desktop:start: at ChildProcess.spawn (node:internal/child_process:421:11) @t3tools/desktop:start: at Module.spawn (node:child_process:810:9) @t3tools/desktop:start: at MixedScheduler.<anonymous> (file://personal/t3code/node_modules/.bun/@effect+platform-node-shared@https+++pkg.pr.new+Effect-TS+effect-smol+@effect+platform-node-shared@8881a9b606d84a6f5eb6615279138322984f5368+eecd35a1726d7096/node_modules/@effect/platform-node-shared/dist/NodeChildProcessSpawner.js:228:37) @t3tools/desktop:start: [desktop] backend exited unexpectedly (code=1 signal=null); restarting in 500ms ``` - Error `RangeError ... getSystemErrorName ... Received 1` came from a failed `spawn()` during startup health checks, and in my case it was triggered by the broken PATH state (caused by shell/PATH normalization behavior). Cause: - PATH normalization assumed shell output shape and did not robustly sanitize interactive shell artifacts. - Non-Windows handling was inconsistent across desktop/server startup paths. - Provider health check (`codex --version`) executed before PATH was reliably normalized. Fix: - Add shared shell PATH resolver (`@t3tools/shared/shellPath`) with: - fish-aware probing - ANSI/control-sequence stripping - delimiter normalization - Use shared resolver in desktop and server `fixPath` logic. - Expand PATH repair to non-Windows startup paths (macOS + Linux). - Ensure server `fixPath` executes before provider layer startup/health probing. - Add focused tests for shell PATH normalization behavior.
1 parent 5a90b6b commit eaf9336

6 files changed

Lines changed: 91 additions & 19 deletions

File tree

‎apps/desktop/src/fixPath.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import * as ChildProcess from "node:child_process";
1+
import { resolveLoginShellPath } from "@t3tools/shared/shellPath";
22

33
export function fixPath(): void {
4-
if (process.platform !== "darwin") return;
4+
if (process.platform === "win32") return;
55

66
try {
7-
const shell = process.env.SHELL ?? "/bin/zsh";
8-
const result = ChildProcess.execFileSync(shell, ["-ilc", "echo -n $PATH"], {
9-
encoding: "utf8",
10-
timeout: 5000,
11-
});
7+
const shell = process.env.SHELL ?? (process.platform === "darwin" ? "/bin/zsh" : "/bin/sh");
8+
const result = resolveLoginShellPath(shell, process.env);
129
if (result) {
1310
process.env.PATH = result;
1411
}

‎apps/server/src/main.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,10 @@ export const recordStartupHeartbeat = Effect.gen(function* () {
235235
});
236236
});
237237

238-
const makeServerProgram = (input: CliInput) =>
238+
const makeServerProgram = () =>
239239
Effect.gen(function* () {
240-
const cliConfig = yield* CliConfig;
241240
const { start, stopSignal } = yield* Server;
242241
const openDeps = yield* Open;
243-
yield* cliConfig.fixPath;
244242

245243
const config = yield* ServerConfig;
246244

@@ -280,7 +278,7 @@ const makeServerProgram = (input: CliInput) =>
280278
}
281279

282280
return yield* stopSignal;
283-
}).pipe(Effect.provide(LayerLive(input)));
281+
});
284282

285283
/**
286284
* These flags mirrors the environment variables and the config shape.
@@ -343,5 +341,12 @@ export const t3Cli = Command.make("t3", {
343341
logWebSocketEvents: logWebSocketEventsFlag,
344342
}).pipe(
345343
Command.withDescription("Run the T3 Code server."),
346-
Command.withHandler((input) => Effect.scoped(makeServerProgram(input))),
344+
Command.withHandler((input) =>
345+
Effect.scoped(
346+
Effect.gen(function* () {
347+
yield* Effect.sync(fixPath);
348+
return yield* makeServerProgram().pipe(Effect.provide(LayerLive(input)));
349+
}),
350+
),
351+
),
347352
);

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import * as OS from "node:os";
22
import { Effect, Path } from "effect";
3-
import { execFileSync } from "node:child_process";
3+
4+
import { resolveLoginShellPath } from "@t3tools/shared/shellPath";
45

56
export function fixPath(): void {
6-
if (process.platform !== "darwin") return;
7+
if (process.platform === "win32") return;
78

89
try {
9-
const shell = process.env.SHELL ?? "/bin/zsh";
10-
const result = execFileSync(shell, ["-ilc", "echo -n $PATH"], {
11-
encoding: "utf8",
12-
timeout: 5000,
13-
});
10+
const shell = process.env.SHELL ?? (process.platform === "darwin" ? "/bin/zsh" : "/bin/sh");
11+
const result = resolveLoginShellPath(shell, process.env);
1412
if (result) {
1513
process.env.PATH = result;
1614
}

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"types": "./src/logging.ts",
1717
"import": "./src/logging.ts"
1818
},
19+
"./shellPath": {
20+
"types": "./src/shellPath.ts",
21+
"import": "./src/shellPath.ts"
22+
},
1923
"./Net": {
2024
"types": "./src/Net.ts",
2125
"import": "./src/Net.ts"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import assert from "node:assert/strict";
2+
3+
import { it } from "@effect/vitest";
4+
5+
import { normalizeShellPathOutput } from "./shellPath";
6+
7+
it("normalizes colon-delimited shell PATH output", () => {
8+
assert.strictEqual(
9+
normalizeShellPathOutput("/usr/local/bin:/opt/homebrew/bin:/usr/bin"),
10+
"/usr/local/bin:/opt/homebrew/bin:/usr/bin",
11+
);
12+
});
13+
14+
it("normalizes fish-style whitespace-delimited PATH output", () => {
15+
assert.strictEqual(
16+
normalizeShellPathOutput("/usr/local/bin /opt/homebrew/bin /usr/bin"),
17+
"/usr/local/bin:/opt/homebrew/bin:/usr/bin",
18+
);
19+
});
20+
21+
it("strips ANSI control sequences from shell PATH output", () => {
22+
assert.strictEqual(
23+
normalizeShellPathOutput("\u001b[6 q/usr/local/bin:/opt/homebrew/bin\u001b[2 q"),
24+
"/usr/local/bin:/opt/homebrew/bin",
25+
);
26+
});

‎packages/shared/src/shellPath.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { delimiter, basename } from "node:path";
2+
import { execFileSync } from "node:child_process";
3+
4+
const ESCAPE_CHARACTER = String.fromCharCode(27);
5+
const ANSI_ESCAPE_SEQUENCE = new RegExp(`${ESCAPE_CHARACTER}\\[[0-?]*[ -/]*[@-~]`, "g");
6+
7+
function getShellProbe(shellPath: string): ReadonlyArray<string> {
8+
const shellName = basename(shellPath).toLowerCase();
9+
if (shellName === "fish") {
10+
return ["-ilc", "string join : $PATH"];
11+
}
12+
return ["-ilc", 'printf "%s" "$PATH"'];
13+
}
14+
15+
export function normalizeShellPathOutput(raw: string): string | undefined {
16+
const stripped = raw.replace(ANSI_ESCAPE_SEQUENCE, "").trim();
17+
if (stripped.length === 0) return undefined;
18+
19+
const separator = stripped.includes(":") ? ":" : stripped.includes(";") ? ";" : undefined;
20+
if (!separator) return undefined;
21+
22+
const segments = stripped
23+
.split(separator)
24+
.map((segment) => segment.trim())
25+
.filter((segment) => segment.length > 0);
26+
27+
if (segments.length === 0) return undefined;
28+
return segments.join(delimiter);
29+
}
30+
31+
export function resolveLoginShellPath(shellPath: string, env: NodeJS.ProcessEnv = process.env) {
32+
const result = execFileSync(shellPath, [...getShellProbe(shellPath)], {
33+
encoding: "utf8",
34+
timeout: 5000,
35+
env: {
36+
...env,
37+
TERM: "dumb",
38+
},
39+
});
40+
41+
return normalizeShellPathOutput(result);
42+
}

0 commit comments

Comments
 (0)