Skip to content

A group accepts any unknown verb: exit 0 and 4 KB of help, where the root refuses with 64 #897

Closed

Description

@David-Noble-at-work

A mistyped or retired verb under a subcommand group is indistinguishable from success.

Observed, 2026-09-19

$ writ repo add team ~/x        exit=0   prints the group's help
$ writ repo nosuchverb          exit=0   prints the group's help
$ writ secret nosuchverb        exit=0   4,422 bytes of help
$ writ config nosuchverb        exit=0   4,757 bytes of help
$ writ snapshot nosuchverb      exit=64  Error: unknown command "snapshot" for "writ"
$ writ nosuchverb               exit=64  Error: unknown command "nosuchverb" for "writ"

Counted across the built binaries: writ 9 of 12, lore 6 of 18, star 10 of 11 top-level commands
accept an unknown verb and exit 0.

The mechanism

Two cobra decisions, in this order.

1. Find validates the root and nothing below it (command.go:776, args.go:28):

commandFound, a := innerfind(c, args)
if commandFound.Args == nil {
    return commandFound, a, legacyArgs(commandFound, stripFlags(a, commandFound))
}
return commandFound, a, nil

func legacyArgs(cmd *Command, args []string) error {
    if !cmd.HasSubCommands() { return nil }                         // a leaf takes its arguments
    if !cmd.HasParent() && len(args) > 0 { return fmt.Errorf(...) } // ROOT with subcommands: check
    return nil                                                      // a group: accepted
}

2. execute abandons a runless command before it validates (command.go:955, :968):

if !c.Runnable() { return flag.ErrHelp }           // a group stops here
...
if err := c.ValidateArgs(argWoFlags); err != nil   // thirteen lines too late

ExecuteC then catches ErrHelp, prints help and returns nil, so the exit code is 0.

What does not fix it

Setting Args on groups makes it worse. Find consults legacyArgs only when Args == nil; a non-nil
Args takes the command off that branch, and the validator it names is never reached, because execute
abandons a runless command first. Measured 2026-09-19: with cobra.NoArgs on every group, every group still
exited 0, and the root-level check was disabled into the bargain.

Giving groups a RunE does work — Runnable() becomes true, ValidateArgs is reached — but it
contradicts §3 and §14's invariant 7, which require that a group take no action of its own.

Replacing legacyArgs is impossible: an unexported function declaration, not a variable, with one
production call site inside a method that cannot be overridden. Traverse, the alternative path, validates
nothing at all.

Fix

Validate the command line before executing it, which is the order the specification assumes: parse, validate,
then run. Find is exported and free of side effects, so the program can stand where legacyArgs stands —
same command, same leftovers, nothing executed:

func main() {
	root := writ.NewRootCmd()
	if err := cli.ValidateCommandLine(root, os.Args[1:]); err != nil {
		os.Exit(cli.ExitCode(err))
	}
	os.Exit(cli.ExitCode(root.Execute()))
}

ValidateCommandLine resolves with root.Find and refuses when the resolved command has subcommands, is not
runnable, and operands remain. The message is cobra's own wording, which isUsageError already maps to
ExitUsage:

unknown command "add" for "writ repo"

Three things it must get right:

  • Operands, not flags. writ repo --output json reaches Execute and prints help; writ repo add does
    not. Cobra's stripFlags is unexported, so this is ours to write and to pin with a test.
  • Groups only. A leaf's arguments are ValidateArgs's business, and the root's are legacyArgs's. This
    adds the one level neither covers.
  • Groups keep Args == nil, so legacyArgs continues to do its root-level job.

Find then runs twice, once here and once inside ExecuteC. It is pure, so that costs a walk and nothing else.

Acceptance

  • writ repo nosuchverb, writ secret nosuchverb, writ config nosuchverb exit 64 with unknown command
  • writ repo add team ~/x exits 64: the retired verb is refused, not shown help
  • a bare writ repo, writ secret, writ config still print help and exit 0
  • writ repo --output json still prints help and exits 0: a flag is not a verb
  • the same holds for every group in lore, star and devlore-test, star's after its extensions load
  • a §14 invariant asserts every program validates before executing
  • §3 of docs/architecture/10-command-line-interface.md states that a group refuses a verb it does not
    have, and §14 records the invariant


Ticked 2026-09-19. Measured on the rebuilt binaries: every unknown verb under every group in writ, lore and star exits 64 with cobra's own message and suggestions; bare and flagged groups still print help at 0; writ repo add team ~/x is refused. devlore-test carries only the shared root's groups, which the same pre-flight covers. Unit tests: TestValidateCommandLine_* and TestOperandsOf_* in cmd/internal/cli, TestRepo_RetiredSpellings_AreUnknown in cmd/writ/writ. §3 states the rule; §14 records invariant 8.

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions