diff --git a/src/cli.ts b/src/cli.ts index 3869c6b..7563b08 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -546,12 +546,10 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): P return 0; } - if (parsed.command === "mcp") { - startMcpStdioServer(); - return 0; - } - - if (parsed.help || !parsed.command) { + // `--help` and `--version` short-circuit on purpose, but a bad flag must not + // be swallowed just because no subcommand was given: `bla --bogus` used to + // print help and exit 0, so a CI typo looked like success. + if (parsed.help) { printHelp(); return 0; } @@ -561,6 +559,16 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): P return 1; } + if (parsed.command === "mcp") { + startMcpStdioServer(); + return 0; + } + + if (!parsed.command) { + printHelp(); + return 0; + } + try { switch (parsed.command) { case "setup": { @@ -1272,9 +1280,19 @@ const isDirectExecution = process.argv[1].endsWith("bla-agents") || process.argv[1].endsWith("bla-shield")); +/** + * The stdio server owns the process lifetime once it starts, so a zero exit + * from the `mcp` path must not be written to `process.exitCode`. A non-zero + * code means the server never started (bad flags), and that must still surface. + */ +export function shouldSetProcessExitCode(argv: readonly string[], code: number): boolean { + const stdioServerPath = argv[2] === "mcp" || argv.includes("--mcp"); + return !stdioServerPath || code !== 0; +} + if (isDirectExecution) { runCli().then((code) => { - if (process.argv[2] !== "mcp" && !process.argv.includes("--mcp")) { + if (shouldSetProcessExitCode(process.argv, code)) { process.exitCode = code; } }); diff --git a/tests/cli-flag-validation.test.ts b/tests/cli-flag-validation.test.ts index 4af8c9a..d3526b9 100644 --- a/tests/cli-flag-validation.test.ts +++ b/tests/cli-flag-validation.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { parseCliArgs, runCli } from "../src/cli.js"; +import { parseCliArgs, runCli, shouldSetProcessExitCode } from "../src/cli.js"; describe("CLI flag validation", () => { it("rejects out-of-range --fail-under values", () => { @@ -38,6 +38,29 @@ describe("CLI flag validation", () => { await expect(runCli(["--help", "--fail-under", "-1"])).resolves.toBe(0); }); + it("returns exit code 1 for invalid flags even when no subcommand is given", async () => { + await expect(runCli(["--bogus"])).resolves.toBe(1); + await expect(runCli(["--fail-under", "-1"])).resolves.toBe(1); + await expect(runCli([])).resolves.toBe(0); + }); + + it("rejects invalid flags passed to mcp before starting the server", async () => { + await expect(runCli(["mcp", "--bogus"])).resolves.toBe(1); + }); + + it("still surfaces a non-zero exit code on the mcp path", () => { + // The stdio server owns the process lifetime once it starts, so a zero exit + // is suppressed there — but a refused invocation must not be. + expect(shouldSetProcessExitCode(["node", "cli.js", "mcp"], 0)).toBe(false); + expect(shouldSetProcessExitCode(["node", "cli.js", "mcp", "--bogus"], 1)).toBe(true); + expect(shouldSetProcessExitCode(["node", "cli.js", "audit", "."], 1)).toBe(true); + expect(shouldSetProcessExitCode(["node", "cli.js", "audit", "."], 0)).toBe(true); + }); + + it("keeps --version short-circuiting ahead of flag validation", async () => { + await expect(runCli(["--version", "--bogus"])).resolves.toBe(0); + }); + it("accepts --flag=value spellings for every value flag", () => { const parsed = parseCliArgs([ "defend",