From fbf5efb5705be1030421321c1966ca135c90f284 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 1 Jan 2026 22:55:21 +0100 Subject: [PATCH] feat(process): support env overrides in exec --- src/process/exec.test.ts | 22 ++++++++++++++++++++++ src/process/exec.ts | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/process/exec.test.ts diff --git a/src/process/exec.test.ts b/src/process/exec.test.ts new file mode 100644 index 000000000..27ffe3f03 --- /dev/null +++ b/src/process/exec.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; + +import { runCommandWithTimeout } from "./exec.js"; + +describe("runCommandWithTimeout", () => { + it("passes env overrides to child", async () => { + const result = await runCommandWithTimeout( + [ + process.execPath, + "-e", + 'process.stdout.write(process.env.CLAWDIS_TEST_ENV ?? "")', + ], + { + timeoutMs: 5_000, + env: { CLAWDIS_TEST_ENV: "ok" }, + }, + ); + + expect(result.code).toBe(0); + expect(result.stdout).toBe("ok"); + }); +}); diff --git a/src/process/exec.ts b/src/process/exec.ts index eb5f7a2d0..914f3bfe5 100644 --- a/src/process/exec.ts +++ b/src/process/exec.ts @@ -47,6 +47,7 @@ export type CommandOptions = { timeoutMs: number; cwd?: string; input?: string; + env?: NodeJS.ProcessEnv; }; export async function runCommandWithTimeout( @@ -57,13 +58,14 @@ export async function runCommandWithTimeout( typeof optionsOrTimeout === "number" ? { timeoutMs: optionsOrTimeout } : optionsOrTimeout; - const { timeoutMs, cwd, input } = options; + const { timeoutMs, cwd, input, env } = options; // Spawn with inherited stdin (TTY) so tools like `pi` stay interactive when needed. return await new Promise((resolve, reject) => { const child = spawn(argv[0], argv.slice(1), { stdio: [input ? "pipe" : "inherit", "pipe", "pipe"], cwd, + env: env ? { ...process.env, ...env } : process.env, }); let stdout = ""; let stderr = "";