diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index fa9e652a8d..5e1a7f3e61 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -23454,6 +23454,14 @@ if (runFastPath(process.argv)) { // catch below as one line instead of an unhandled rejection's source dump. .then(() => { failUnknownCommandBeforeHelp(process.argv); + // Root options count only before the command name. Commander otherwise + // reads them anywhere, so the root's `--version` swallowed `cast ship + // mark --version 1.2.3`: it printed the CLI version and exited 0, and + // every release script's deploy marker went unrecorded (ct-99939). Set + // here, after the group is registered: a command copies the setting from + // its parent when made, and a group that inherited it would refuse its + // own options placed after a subcommand (`cast line trial run --project`). + program.enablePositionalOptions(); return program.parseAsync(); }) .catch((err) => { diff --git a/packages/cli/src/rootVersionFlag.test.ts b/packages/cli/src/rootVersionFlag.test.ts new file mode 100644 index 0000000000..c86ee2a405 --- /dev/null +++ b/packages/cli/src/rootVersionFlag.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +// Through the real root program, not a subcommand registered alone: the root's +// `.version()` flag is what swallowed `cast ship mark --version 1.2.3`, and a +// test that builds only the subcommand cannot see it (ct-99939). +const cliEntry = join(import.meta.dir, "index.ts"); +const home = mkdtempSync(join(tmpdir(), "cast-root-version-")); + +function cast(...args: string[]) { + const env: Record = { ...process.env, HOME: home, NO_COLOR: "1" }; + for (const key of Object.keys(env)) if (key.startsWith("CODECAST_")) delete env[key]; + const proc = spawnSync(process.execPath, [cliEntry, ...args], { env, encoding: "utf8", timeout: 60_000 }); + if (proc.stdout.length === 0) throw new Error(`cast ${args.join(" ")} printed nothing (exit ${proc.status}): ${proc.stderr}`); + return proc; +} + +describe("root --version", () => { + test("a subcommand's own --version takes its value", () => { + const proc = cast("ship", "mark", "--surface", "desktop", "--sha", "abc1234", "--version", "1.2.3", "--repo", "o/r", "--dry-run", "--json"); + expect(proc.status).toBe(0); + expect(JSON.parse(proc.stdout)).toMatchObject({ dry_run: true, surface: "desktop", version: "1.2.3" }); + }); + + test("a group's own option after its subcommand still parses", () => { + // `--project` belongs to `cast line trial`; placed after `run` it must not + // be refused as unknown. The run itself fails later, signed out. + const env: Record = { ...process.env, HOME: home, NO_COLOR: "1" }; + for (const key of Object.keys(env)) if (key.startsWith("CODECAST_")) delete env[key]; + const proc = spawnSync(process.execPath, [cliEntry, "line", "trial", "run", "--project", "x", "ct-1"], { env, encoding: "utf8", timeout: 60_000 }); + expect(`${proc.stdout}${proc.stderr}`).not.toContain("unknown option"); + }); + + test("cast --version and -V still print the CLI version", () => { + for (const flag of ["--version", "-V"]) { + const proc = cast(flag); + expect(proc.status).toBe(0); + expect(proc.stdout.trim()).toMatch(/^\d+\.\d+\.\d+/); + } + }); +});