Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bin/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/node/cli-startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading