diff --git a/bin/cli-main.ts b/bin/cli-main.ts index fb04514..2e3bf68 100644 --- a/bin/cli-main.ts +++ b/bin/cli-main.ts @@ -2255,9 +2255,11 @@ export async function runCli() { } catch (error) { // Name where the bad value came from: an MCP host debugging a server // that will not start needs to know it is the environment, not a flag. - const source = - flagValue(process.argv, "--profile") === undefined ? "BETTERWRIGHT_PROFILE" : "--profile"; - console.error(`${source}: ${error?.message || error}`); + // Launch-default errors already name their variable. + const message = String(error?.message || error); + const source = /^BETTERWRIGHT_[A-Z_]+ /.test(message) ? "" + : `${flagValue(process.argv, "--profile") === undefined ? "BETTERWRIGHT_PROFILE" : "--profile"}: `; + console.error(`${source}${message}`); return 1; } } diff --git a/tests/node/cli-startup.test.ts b/tests/node/cli-startup.test.ts index d128a10..bf18984 100644 --- a/tests/node/cli-startup.test.ts +++ b/tests/node/cli-startup.test.ts @@ -79,3 +79,29 @@ test("an absent or empty environment model still loads default model discovery", assert.match(result.stderr, /Unexpected setup module: .*doctor\.js/); } }); + +test("invalid launch environment errors name their own variable, not the profile source", () => { + for (const [name, value, profileArgs] of [ + ["BETTERWRIGHT_HEADLESS", "maybe", []], + ["BETTERWRIGHT_HEADLESS", "maybe", ["--profile", "work"]], + ["BETTERWRIGHT_DOWNLOAD_POLICY", "sometimes", []], + ] as const) { + const result = spawnSync(process.execPath, [cli, "run", ...profileArgs, "-c", "return 1"], { + cwd: root, + env: { ...process.env, BETTERWRIGHT_HOME: makeTempDir("betterwright-startup-home-"), BETTERWRIGHT_NO_DAEMON: "1", [name]: value }, + encoding: "utf8", + timeout: 15_000, + }); + assert.equal(result.status, 1); + assert.match(result.stderr, new RegExp(`^${name} must be`, "m")); + assert.doesNotMatch(result.stderr, /BETTERWRIGHT_PROFILE:|--profile:/); + } + const profile = spawnSync(process.execPath, [cli, "run", "-c", "return 1"], { + cwd: root, + env: { ...process.env, BETTERWRIGHT_HOME: makeTempDir("betterwright-startup-home-"), BETTERWRIGHT_NO_DAEMON: "1", BETTERWRIGHT_PROFILE: "../escape" }, + encoding: "utf8", + timeout: 15_000, + }); + assert.equal(profile.status, 1); + assert.match(profile.stderr, /^BETTERWRIGHT_PROFILE: /m); +});