Skip to content
TypeScript

fix(ssh): exec managed servers without npm wrappers (#9843)

Julius Marmingef33fdc992488
diff --git a/packages/ssh/src/runnerProcess.test.ts b/packages/ssh/src/runnerProcess.test.ts
new file mode 100644
index 00000000000..7dda675ee95
--- /dev/null
+++ b/packages/ssh/src/runnerProcess.test.ts
@@ -0,0 +1,167 @@
+import * as NodeServices from "@effect/platform-node/NodeServices";
+import { assert, describe, it } from "@effect/vitest";
+import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
+import * as Deferred from "effect/Deferred";
+import * as Effect from "effect/Effect";
+import * as Fiber from "effect/Fiber";
+import * as FileSystem from "effect/FileSystem";
+import * as Path from "effect/Path";
+import * as Schema from "effect/Schema";
+import * as Stream from "effect/Stream";
+import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
+import * as NodeNet from "node:net";
+
+import { buildRemoteT3RunnerScript } from "./tunnel.ts";
+
+const Started = Schema.Struct({
+ pid: Schema.Number,
+ port: Schema.Number,
+ args: Schema.Array(Schema.String),
+});
+const decodeStarted = Schema.decodeUnknownSync(Schema.fromJsonString(Started));
+
+describe.skipIf(HostProcessPlatform.defaultValue() === "win32")(
+ "remote runner process ownership",
+ () => {
+ it.live.each(["npx", "npm"] as const)(
+ "keeps the server PID and graceful shutdown through the %s fallback",
+ (packageManager) =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const fixture = yield* fs.makeTempDirectoryScoped({ prefix: "t3-runner-" });
+ const bin = path.join(fixture, "bin");
+ const cliPath = path.join(fixture, "installed cli.mjs");
+ const callsPath = path.join(fixture, "package-manager-calls.jsonl");
+ const packageSpec = "[email protected]";
+ yield* fs.makeDirectory(bin);
+ yield* fs.symlink(process.execPath, path.join(bin, "node"));
+ yield* fs.writeFileString(
+ cliPath,
+ `#!/usr/bin/env node
+import * as net from "node:net";
+const server = net.createServer((socket) => {
+ socket.end();
+ server.close();
+});
+process.on("SIGTERM", () => server.close(() => {
+ process.stdout.write("graceful shutdown\\n");
+}));
+server.listen(Number(process.env.T3_TEST_PORT ?? 0), "127.0.0.1", () => {
+ process.stdout.write(JSON.stringify({
+ pid: process.pid,
+ port: server.address().port,
+ args: process.argv.slice(2),
+ }) + "\\n");
+});
+`,
+ );
+ yield* fs.chmod(cliPath, 0o700);
+ yield* fs.writeFileString(
+ path.join(bin, packageManager),
+ `#!/usr/bin/env node
+const fs = require("node:fs");
+const childProcess = require("node:child_process");
+const args = process.argv.slice(2);
+fs.appendFileSync(process.env.T3_TEST_CALLS, JSON.stringify(args) + "\\n");
+if (args.includes("--package")) {
+ process.stdout.write(process.env.T3_TEST_CLI + "\\n");
+} else {
+ const child = childProcess.spawn(process.execPath, [process.env.T3_TEST_CLI, ...args], { stdio: "inherit" });
+ child.once("exit", (code) => { process.exitCode = code ?? 1; });
+}
+`,
+ );
+ yield* fs.chmod(path.join(bin, packageManager), 0o700);
+
+ const runServer = (port = 0) =>
+ Effect.gen(function* () {
+ const child = yield* spawner.spawn(
+ ChildProcess.make("/bin/sh", ["-s", "--", "serve", "a path with spaces"], {
+ cwd: fixture,
+ env: {
+ PATH: bin,
+ T3_TEST_CLI: cliPath,
+ T3_TEST_CALLS: callsPath,
+ T3_TEST_PORT: String(port),
+ },
+ detached: false,
+ stdin: Stream.make(
+ new TextEncoder().encode(buildRemoteT3RunnerScript({ packageSpec })),
+ ),
+ }),
+ );
+ const ready = yield* Deferred.make<typeof Started.Type>();
+ const stdout: string[] = [];
+ const output = yield* child.stdout.pipe(
+ Stream.decodeText(),
+ Stream.splitLines,
+ Stream.runForEach((line) =>
+ Effect.gen(function* () {
+ stdout.push(line);
+ if (stdout.length === 1) {
+ yield* Deferred.succeed(ready, decodeStarted(line));
+ }
+ }),
+ ),
+ Effect.forkScoped,
+ );
+ const stderr = yield* child.stderr.pipe(
+ Stream.decodeText(),
+ Stream.mkString,
+ Effect.forkScoped,
+ );
+ const receipt = yield* Effect.raceFirst(
+ Deferred.await(ready),
+ Fiber.join(output).pipe(
+ Effect.flatMap(() => Fiber.join(stderr)),
+ Effect.flatMap((message) =>
+ Effect.die(new Error(`Runner exited before listening: ${message}`)),
+ ),
+ ),
+ );
+ // A failed PID assertion must still close the owned fixture server, including an npm child.
+ yield* Effect.addFinalizer(() =>
+ Effect.gen(function* () {
+ if (yield* child.isRunning) {
+ yield* Effect.callback<void>((resume) => {
+ const connection = NodeNet.connect(receipt.port, "127.0.0.1");
+ connection.on("error", () => undefined);
+ connection.once("close", () => resume(Effect.void));
+ return Effect.sync(() => connection.destroy());
+ });
+ yield* child.exitCode;
+ }
+ }).pipe(Effect.orDie),
+ );
+ assert.equal(receipt.pid, child.pid);
+ assert.deepEqual(receipt.args, ["serve", "a path with spaces"]);
+ yield* child.kill({ killSignal: "SIGTERM" });
+ assert.equal(yield* child.exitCode, 0);
+ yield* Fiber.join(output);
+ assert.include(stdout, "graceful shutdown");
+ return receipt.port;
+ }).pipe(Effect.scoped);
+
+ const port = yield* runServer();
+ assert.equal(yield* runServer(port), port);
+ const calls = (yield* fs.readFileString(callsPath))
+ .trim()
+ .split("\n")
+ .map((line) => JSON.parse(line));
+ const expectedCall = [
+ ...(packageManager === "npm" ? ["exec"] : []),
+ "--yes",
+ "--package",
+ packageSpec,
+ "--",
+ "sh",
+ "-c",
+ "command -v t3",
+ ];
+ assert.deepEqual(calls, [expectedCall, expectedCall]);
+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
+ );
+ },
+);
diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts
index 392885b640c..4a49cacc2eb 100644
--- a/packages/ssh/src/tunnel.test.ts
+++ b/packages/ssh/src/tunnel.test.ts
@@ -105,8 +105,7 @@ describe("ssh tunnel scripts", () => {
assert.include(script, "T3_NODE_SCRIPT_PATH=''");
assert.include(script, 'exec t3 "$@"');
- assert.include(script, "exec npx --yes 't3@latest' \"$@\"");
- assert.include(script, "exec npm exec --yes 't3@latest' -- \"$@\"");
+ assert.include(script, 'exec "$T3_CLI_PATH" "$@"');
assert.include(script, "could not install 't3@latest'");
assert.include(script, "require_installed_t3_cli npx --yes --package 't3@latest'");
assert.include(script, "require_installed_t3_cli npm exec --yes --package 't3@latest'");
@@ -141,8 +140,6 @@ describe("ssh tunnel scripts", () => {
packageSpec: "t3@nightly; touch /tmp/t3-owned",
});
- assert.include(script, "exec npx --yes 't3@nightly; touch /tmp/t3-owned' \"$@\"");
- assert.include(script, "exec npm exec --yes 't3@nightly; touch /tmp/t3-owned' -- \"$@\"");
assert.include(
script,
"require_installed_t3_cli npx --yes --package 't3@nightly; touch /tmp/t3-owned'",
diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts
index 0afd1007f8e..04dbce65af6 100644
--- a/packages/ssh/src/tunnel.ts
+++ b/packages/ssh/src/tunnel.ts
@@ -440,13 +440,14 @@ require_installed_t3_cli() {
printf 'Remote host installed %s but npm produced no t3 executable, which usually means a native dependency (node-pty) failed to build. Install a C toolchain on the remote host (Debian/Ubuntu: build-essential, Fedora/RHEL: gcc-c++ make, macOS: xcode-select --install) and try again.\\n' @@T3_PACKAGE_SPEC@@ >&2
return 1
}
+# The launcher records this PID, so exec the CLI without an npm wrapper process.
if command -v npx >/dev/null 2>&1; then
require_installed_t3_cli npx --yes --package @@T3_PACKAGE_SPEC@@ || exit 1
- exec npx --yes @@T3_PACKAGE_SPEC@@ "$@"
+ exec "$T3_CLI_PATH" "$@"
fi
if command -v npm >/dev/null 2>&1; then
require_installed_t3_cli npm exec --yes --package @@T3_PACKAGE_SPEC@@ || exit 1
- exec npm exec --yes @@T3_PACKAGE_SPEC@@ -- "$@"
+ exec "$T3_CLI_PATH" "$@"
fi
printf 'Remote host is missing the t3 CLI and could not install @@T3_PACKAGE_SPEC@@ because node/npm/npx are unavailable on PATH. Install Node or configure a supported version manager for non-interactive shells.\\n' >&2
exit 1