@devries-cameron20 / t3code Mirror · read-only / commits / bf046e3456
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 04:30 UTC

fix(cli): validate npm runtime before caching it (#30099)

Lakshmi Tanmay committed Nov 4, 2026 at 05:16 UTC bf046e3456d36055c6d3ade885bbbf708d4520d3
2 files changed +125 -15
scripts/build-npm-launcher.test.ts
+97 -14
@@ -47,7 +47,15 @@ const run = Effect.fn("test.run")(function* (
47 return { stdout, stderr, exitCode };
48 }, Effect.scoped);
49
50 -const serveFakeRelease = Effect.fn("test.serveFakeRelease")(function* (root: string) {
50 +const serveFakeRelease = Effect.fn("test.serveFakeRelease")(function* (
51 + root: string,
52 + executable = `#!/bin/sh
53 +if [ -n "$T3CODE_TEST_CALLS" ]; then echo "$*|$T3CODE_NPM_LAUNCHER_PATH" >> "$T3CODE_TEST_CALLS"; fi
54 +if [ "$1" = "--version" ]; then echo "${VERSION}"; exit 0; fi
55 +echo "stub $* launcher=$T3CODE_NPM_LAUNCHER_PATH"
56 +exit 7
57 +`,
58 +) {
59 const fs = yield* FileSystem.FileSystem;
60 const path = yield* Path.Path;
61 const key = `${yield* HostProcess.Platform}-${yield* HostProcess.Architecture}`;
@@ -56,10 +64,7 @@ const serveFakeRelease = Effect.fn("test.serveFakeRelease")(function* (root: str
64 yield* fs.makeDirectory(path.join(root, "release", stem, "node_modules/native"), {
65 recursive: true,
66 });
59 - yield* fs.writeFileString(
60 - path.join(root, "release", stem, "t3"),
61 - '#!/bin/sh\necho "stub $* launcher=$T3CODE_NPM_LAUNCHER_PATH"\nexit 7\n',
62 - );
67 + yield* fs.writeFileString(path.join(root, "release", stem, "t3"), executable);
68 yield* fs.chmod(path.join(root, "release", stem, "t3"), 0o755);
69 const tar = yield* run("tar", ["-czf", archiveName, stem], {
70 cwd: path.join(root, "release"),
@@ -136,6 +141,7 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
141 const env = {
142 PATH: process.env.PATH,
143 T3CODE_HOME: home,
144 + T3CODE_TEST_CALLS: path.join(root, "calls"),
145 T3CODE_RELEASE_BASE_URL: release.baseUrl,
146 };
147 const launcher = path.join(packageDir, "bin/t3.mjs");
@@ -145,6 +151,10 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
151 });
152 assert.equal(first.stdout.trim(), `stub serve a b launcher=${launcher}`, first.stderr);
153 assert.equal(first.exitCode, 7);
154 + assert.deepStrictEqual(
155 + (yield* fs.readFileString(env.T3CODE_TEST_CALLS)).trim().split("\n"),
156 + ["--version|", `serve a b|${launcher}`],
157 + );
158 const versionDir = path.join(home, "runtime/versions", VERSION);
159 assert.equal(
160 (yield* fs.readFileString(path.join(versionDir, ".install-complete"))).trim(),
@@ -158,8 +168,24 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
168
169 const requestsAfterFirst = release.requests.length;
170 const second = yield* run(process.execPath, [launcher, "--version"], { cwd: root, env });
161 - assert.equal(second.exitCode, 7);
171 + assert.equal(second.exitCode, 0);
172 assert.equal(release.requests.length, requestsAfterFirst);
173 + assert.deepStrictEqual(
174 + (yield* fs.readFileString(env.T3CODE_TEST_CALLS)).trim().split("\n"),
175 + ["--version|", `serve a b|${launcher}`, `--version|${launcher}`],
176 + );
177 +
178 + yield* fs.writeFileString(path.join(versionDir, "t3"), "#!/t3-test-missing-loader\n");
179 + for (const _attempt of [1, 2]) {
180 + const cached = yield* run(process.execPath, [launcher, "serve"], { cwd: root, env });
181 + assert.equal(cached.exitCode, 1, cached.stderr);
182 + assert.include(cached.stderr, "could not run");
183 + assert.equal(release.requests.length, requestsAfterFirst);
184 + assert.equal(
185 + yield* fs.readFileString(path.join(versionDir, ".install-complete")),
186 + `${VERSION}\n`,
187 + );
188 + }
189
190 const ipcServer = path.join(home, "runtime/versions", VERSION, "t3");
191 yield* fs.writeFileString(
@@ -189,8 +215,8 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
215 env: { ...env, T3CODE_HOME: raceHome },
216 });
217 const [one, two] = yield* Effect.all([launch, launch], { concurrency: "unbounded" });
192 - assert.equal(one.exitCode, 7, one.stderr);
193 - assert.equal(two.exitCode, 7, two.stderr);
218 + assert.equal(one.exitCode, 0, one.stderr);
219 + assert.equal(two.exitCode, 0, two.stderr);
220 assert.deepStrictEqual(yield* fs.readDirectory(path.join(raceHome, "runtime/versions")), [
221 VERSION,
222 ]);
@@ -296,12 +322,8 @@ fs.readFileSync = (file, ...rest) => {
322 cwd: root,
323 env: { ...env, T3CODE_HOME: staleOnlyHome },
324 });
299 - assert.equal(
300 - replaced.stdout.trim(),
301 - `stub --version launcher=${launcher}`,
302 - replaced.stderr,
303 - );
304 - assert.equal(replaced.exitCode, 7);
325 + assert.equal(replaced.stdout.trim(), VERSION, replaced.stderr);
326 + assert.equal(replaced.exitCode, 0);
327 assert.deepStrictEqual(
328 yield* fs.readDirectory(path.join(staleOnlyHome, "runtime/versions")),
329 [VERSION],
@@ -320,4 +342,65 @@ fs.readFileSync = (file, ...rest) => {
342 );
343 }),
344 );
345 + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
346 + "does not publish an unusable release",
347 + () =>
348 + Effect.gen(function* () {
349 + const fs = yield* FileSystem.FileSystem;
350 + const path = yield* Path.Path;
351 + for (const executable of [
352 + "#!/t3-test-missing-loader\n",
353 + '#!/bin/sh\necho "runtime loader failed" >&2\nexit 42\n',
354 + "#!/bin/sh\nkill -TERM $$\n",
355 + '#!/bin/sh\ntrap \'echo caught > "$T3CODE_TEST_TERM"; exit 42\' TERM\necho ready > "$T3CODE_TEST_READY"\nwhile :; do :; done\n',
356 + ]) {
357 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-npm-validation-test-" });
358 + const output = yield* buildNpmLauncher({
359 + version: VERSION,
360 + outputDir: path.join(root, "out"),
361 + });
362 + const release = yield* serveFakeRelease(root, executable);
363 + const home = path.join(root, "home");
364 + const dir = path.join(home, "runtime/versions", VERSION);
365 + const probeTimeoutHook = path.join(root, "probe-timeout.mjs");
366 + yield* fs.writeFileString(
367 + probeTimeoutHook,
368 + `import childProcess from "node:child_process";
369 +import { syncBuiltinESMExports } from "node:module";
370 +const { spawnSync } = childProcess;
371 +childProcess.spawnSync = (command, args, options) => spawnSync(command, args,
372 + options?.timeout === undefined ? options : { ...options, timeout: 500 });
373 +syncBuiltinESMExports();
374 +`,
375 + );
376 + const env = {
377 + PATH: process.env.PATH,
378 + T3CODE_HOME: home,
379 + T3CODE_RELEASE_BASE_URL: release.baseUrl,
380 + T3CODE_TEST_READY: path.join(root, "probe-ready"),
381 + T3CODE_TEST_TERM: path.join(root, "probe-term"),
382 + };
383 + const launcher = path.join(output.packageDir, "bin/t3.mjs");
384 + const failed = yield* run(
385 + process.execPath,
386 + ["--import", probeTimeoutHook, launcher, "serve"],
387 + { cwd: root, env },
388 + );
389 + assert.equal(failed.exitCode, 1, failed.stderr);
390 + assert.deepStrictEqual(yield* fs.readDirectory(path.dirname(dir)), [], failed.stderr);
391 + assert.include(failed.stderr, "could not run");
392 + if (executable.includes("runtime loader failed")) {
393 + assert.include(failed.stderr, "runtime loader failed");
394 + }
395 + if (executable.includes("trap")) {
396 + assert.isTrue(yield* fs.exists(env.T3CODE_TEST_READY));
397 + assert.isFalse(yield* fs.exists(env.T3CODE_TEST_TERM));
398 + }
399 + if ((yield* HostProcess.Platform) === "linux") {
400 + assert.include(failed.stderr, "glibc");
401 + assert.include(failed.stderr, "musl");
402 + }
403 + }
404 + }),
405 + );
406 });
scripts/npm-launcher/t3.mjs
+28 -1
@@ -90,6 +90,24 @@ async function install(dir, key) {
90 });
91 if (extract.status !== 0) throw new Error(`could not extract ${archive}`);
92 fs.rmSync(file);
93 + const validate = spawnSync(
94 + path.join(staging, process.platform === "win32" ? "t3.exe" : "t3"),
95 + ["--version"],
96 + {
97 + stdio: ["ignore", "ignore", "pipe"],
98 + encoding: "utf8",
99 + timeout: 30_000,
100 + killSignal: "SIGKILL",
101 + },
102 + );
103 + if (validate.status !== 0) {
104 + throw executableError(
105 + key,
106 + validate.error?.message ||
107 + validate.stderr?.trim() ||
108 + `--version exited with ${validate.signal || validate.status}`,
109 + );
110 + }
111 fs.writeFileSync(path.join(staging, ".install-complete"), `${version}\n`);
112 publish(staging, dir);
113 } finally {
@@ -172,12 +190,21 @@ async function main() {
190 for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
191 process.on(signal, () => (ipc || signal !== "SIGINT") && child.kill(signal));
192 }
175 - child.on("error", (error) => fail(error));
193 + child.on("error", (error) => fail(executableError(key, error.message)));
194 child.on("exit", (code, signal) =>
195 process.exit(code ?? 128 + (os.constants.signals[signal] || 1)),
196 );
197 }
198
199 +function executableError(key, detail) {
200 + return new Error(
201 + `could not run T3 Code ${version} for ${key}: ${detail}` +
202 + (process.platform === "linux"
203 + ? "\nThe Linux release requires glibc; musl-based systems such as Alpine are not supported. Use a glibc-based Linux environment."
204 + : ""),
205 + );
206 +}
207 +
208 function fail(error) {
209 process.stderr.write(`t3: ${error.message}\n`);
210 process.exit(1);