From d6598858620870efeefb59d4b545fc24f55bcdce Mon Sep 17 00:00:00 2001 From: Ohad Cohen Date: Sat, 21 Nov 2026 10:53:00 +0200 Subject: [PATCH] fix: leeway overrides env in any case, exits 128+signal, forwards SIGWINCH (#88) --- docs/adr/0001-leeway.md | 3 ++- packages/leeway/README.md | 9 +++++--- packages/leeway/src/os/run-with-ports.ts | 29 ++++++++++++++++-------- packages/leeway/test/cli.test.ts | 28 +++++++++++++++++++++-- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/docs/adr/0001-leeway.md b/docs/adr/0001-leeway.md index 7d83009..53594e0 100644 --- a/docs/adr/0001-leeway.md +++ b/docs/adr/0001-leeway.md @@ -102,7 +102,8 @@ shell line with it fails rather than lose its quotes. **Environment variables are the transport.** They pass through `concurrently`, `tsx`, package scripts and shells to every child process with no integration. `leeway` runs the command through the system shell: one argument is a shell line, several are an argv quoted for the shell. Stopping leeway stops the tree: on POSIX the command gets its own process group and SIGINT, SIGTERM, SIGHUP and SIGQUIT are -forwarded to it; on Windows, which has no process groups to signal, Ctrl+C, Ctrl+Break and console close run `taskkill /T /F`. +forwarded to it, and SIGWINCH too so a resize reaches it; on Windows, which has no process groups to signal, Ctrl+C, Ctrl+Break and +console close run `taskkill /T /F`. **Browser and vite.** `devPorts` is browser-safe. `leeway/vite` sets `server.port` and `preview.port` with `strictPort`, so a taken port fails instead of drifting. `preview.port` uses the dev server's port name unless `preview` names another: left alone, vite previews on diff --git a/packages/leeway/README.md b/packages/leeway/README.md index f3533d2..e8812a5 100644 --- a/packages/leeway/README.md +++ b/packages/leeway/README.md @@ -208,11 +208,14 @@ folder still claims its own range, and an explicit `--default` or `--inherit` ke shell when needed, so `leeway node -e "console.log(1)"` runs exactly that. On Windows a `.cmd` shim re-parses its arguments, so an argument holding both `"` and a cmd metacharacter like `&` can still break there. - The command runs through the system shell (`sh`, `cmd.exe` on Windows). -- leeway exits with the shell's exit code (a missing command: 127 from `sh`, 1 from `cmd.exe`; a command killed by a signal: usually - 128 plus the signal number from `sh`), or 1 if the shell itself died from a signal or could not start. +- The command's port variables replace any variable of the same name in leeway's environment, in any case: on Windows a `PORT_WEB` in + the shell does not shadow a `port_web` port name. +- leeway exits with the shell's exit code (a missing command: 127 from `sh`, 1 from `cmd.exe`), 128 plus the signal number when the + command or the shell died from a signal (130 for Ctrl+C), or 1 if the shell could not start. - Stopping leeway stops the whole command tree: Ctrl+C, `Ctrl+\`, closing the terminal, or SIGINT, SIGTERM, SIGHUP or SIGQUIT on Linux and macOS; Ctrl+C, Ctrl+Break or closing the console on Windows. On Linux and macOS the tree gets the signal - leeway got, so a command that handles it and keeps running is not stopped ([Limits](#limits)). + leeway got, so a command that handles it and keeps running is not stopped ([Limits](#limits)). A terminal resize (SIGWINCH) + reaches the command too, so a TUI redraws. `leeway --help` (or `-h`) prints the usage, `leeway --version` the version. diff --git a/packages/leeway/src/os/run-with-ports.ts b/packages/leeway/src/os/run-with-ports.ts index 7b0279c..1d48166 100644 --- a/packages/leeway/src/os/run-with-ports.ts +++ b/packages/leeway/src/os/run-with-ports.ts @@ -1,19 +1,18 @@ import { spawn } from "node:child_process"; +import os from "node:os"; import { ProcessTree } from "./process-tree.ts"; const WINDOWS_STOP_SIGNALS: NodeJS.Signals[] = ["SIGINT", "SIGBREAK", "SIGHUP"]; -const POSIX_FORWARDED_SIGNALS: NodeJS.Signals[] = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"]; +const POSIX_FORWARDED_SIGNALS: NodeJS.Signals[] = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT", "SIGWINCH"]; export class RunWithPorts { static runAndExit(commandLine: string, env: Record): void { const isWindows = process.platform === "win32"; - const child = spawn(commandLine, { - stdio: "inherit", - shell: true, - detached: !isWindows, - env: { ...process.env, ...Object.fromEntries(Object.entries(env).map(([name, value]) => [name, String(value)])) }, - }); + + RunWithPorts.overrideInheritedEnv(env); + + const child = spawn(commandLine, { stdio: "inherit", shell: true, detached: !isWindows }); const pid = child.pid; if (pid === undefined) { @@ -30,8 +29,18 @@ export class RunWithPorts { POSIX_FORWARDED_SIGNALS.forEach(signal => process.on(signal, () => ProcessTree.signalGroup(pid, signal))); } - child.on("exit", (code, signal) => { - process.exit(signal ? 1 : (code ?? 1)); - }); + child.on("exit", (code, signal) => process.exit(signal ? RunWithPorts.signalExitCode(signal) : (code ?? 1))); + } + + private static overrideInheritedEnv(env: Record): void { + for (const [name, value] of Object.entries(env)) { + process.env[name] = String(value); + } + } + + private static signalExitCode(signal: NodeJS.Signals): number { + const signalNumber: number | undefined = os.constants.signals[signal]; + + return signalNumber === undefined ? 1 : 128 + signalNumber; } } diff --git a/packages/leeway/test/cli.test.ts b/packages/leeway/test/cli.test.ts index 2401878..34961d3 100644 --- a/packages/leeway/test/cli.test.ts +++ b/packages/leeway/test/cli.test.ts @@ -1,6 +1,7 @@ import { type ChildProcess, spawn, spawnSync } from "node:child_process"; import fs from "node:fs"; import http from "node:http"; +import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { Pools } from "../src/registry/pools.ts"; @@ -13,6 +14,9 @@ const SERVER = const PRINT_PORTS = "console.log('argument ' + process.argv[1] + ', env ' + process.env.PORT_WEB + ' ' + process.env.PORT_API)"; +const RESIZE_WAITER = + "process.on('SIGWINCH', () => { console.log('resized'); process.exit(0); }); console.log('listening 0'); setInterval(() => {}, 1000);"; + const FIXTURES = { "launcher.mjs": `import { spawn } from "node:child_process"; spawn(process.execPath, ["-e", ${JSON.stringify(SERVER)}], { stdio: "inherit" }); @@ -303,6 +307,15 @@ describe("leeway ", () => { expect(LeewayCli.run(["node", "-e", "process.exit(0)"], dir, home).status).toBe(0); }); + it("gives the command its port over a parent variable whose name differs only in case", () => { + const { dir, home } = temporaryFolder(); + fs.writeFileSync(path.join(dir, "leeway.json"), JSON.stringify({ ports: { port_web: { port: 5173 } } })); + + const result = LeewayCli.run(["--default", "node", "-e", "console.log(process.env.port_web)"], dir, home, { PORT_WEB: "9999" }); + + expect(result.stdout).toBe("5173\n"); + }); + it("gives the command the claimed ports, and its server answers on one", async () => { const { dir, home } = temporaryFolder(); const leeway = startLeeway(["node", "-e", SERVER], dir, home); @@ -606,7 +619,7 @@ describe("leeway errors", () => { describe.skipIf(process.platform === "win32")("leeway on a signal", () => { it.each(["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"] as const)( - "%s stops the whole command tree, so nothing is left listening", + "%s stops the whole command tree, so nothing is left listening, and exits 128 plus the signal number", async signal => { const { dir, home } = temporaryFolder(); const leeway = startLeeway(["node", "launcher.mjs"], dir, home); @@ -617,10 +630,21 @@ describe.skipIf(process.platform === "win32")("leeway on a signal", () leeway.kill(signal); - expect(await exitCode).not.toBe(0); + expect(await exitCode).toBe(128 + os.constants.signals[signal]); expect(await stopsAnswering(port)).toBe(true); }, ); + + it("hands SIGWINCH to the command, so a TUI redraws on a terminal resize", async () => { + const { dir, home } = temporaryFolder(); + const leeway = startLeeway(["node", "-e", RESIZE_WAITER], dir, home); + const result = finished(leeway); + + await listeningPort(leeway); + leeway.kill("SIGWINCH"); + + expect(await result).toEqual({ code: 0, stdout: "listening 0\nresized\n" }); + }); }); describe.runIf(process.platform === "win32")("leeway on Ctrl+C in its console", () => {