From 0f1a8b96dbf3fc709f50831b1732a92ce9a8d3e5 Mon Sep 17 00:00:00 2001 From: Ashot Petrosian Date: Sat, 5 Dec 2026 17:55:14 -0500 Subject: [PATCH 1/2] Stop the root --version flag from swallowing a subcommand's --version --- packages/cli/src/index.ts | 5 ++++ packages/cli/src/rootVersionFlag.test.ts | 35 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 packages/cli/src/rootVersionFlag.test.ts diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index fa9e652a8d..561a162eb2 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -2949,6 +2949,11 @@ program " cast agent-context --json every command with its flags and arguments, as JSON" ) .version(getVersion()) + // 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). + .enablePositionalOptions() .action(() => { const operands = program.args.filter((a) => !a.startsWith("-")); if (operands.length > 0) failUnknownCommand(operands); diff --git a/packages/cli/src/rootVersionFlag.test.ts b/packages/cli/src/rootVersionFlag.test.ts new file mode 100644 index 0000000000..a56f77986f --- /dev/null +++ b/packages/cli/src/rootVersionFlag.test.ts @@ -0,0 +1,35 @@ +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("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+/); + } + }); +}); From d0efe3005aa4d25914285313cd7970a54ad843f0 Mon Sep 17 00:00:00 2001 From: Ashot Petrosian Date: Sat, 5 Dec 2026 18:20:38 -0500 Subject: [PATCH 2/2] Set positional options on the root only, after its group is registered Codecast-Session: https://codecast.sh/conversation/jx723v27sc92rh0vh5yngz1d9x8kh072 --- packages/cli/src/index.ts | 13 ++++++++----- packages/cli/src/rootVersionFlag.test.ts | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 561a162eb2..5e1a7f3e61 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -2949,11 +2949,6 @@ program " cast agent-context --json every command with its flags and arguments, as JSON" ) .version(getVersion()) - // 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). - .enablePositionalOptions() .action(() => { const operands = program.args.filter((a) => !a.startsWith("-")); if (operands.length > 0) failUnknownCommand(operands); @@ -23459,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 index a56f77986f..c86ee2a405 100644 --- a/packages/cli/src/rootVersionFlag.test.ts +++ b/packages/cli/src/rootVersionFlag.test.ts @@ -25,6 +25,15 @@ describe("root --version", () => { 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);