Skip to content

fix(cli): report an unknown subcommand before an empty positional - #6235

Draft
shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/cli-unknown-subcommand-precedence
Draft

shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/cli-unknown-subcommand-precedence

Conversation

@shunxinyi-ljs

Copy link
Copy Markdown
Contributor

Description

weknora doc viwe "$ID" with ID unset reported argument 2 is empty, not the mistyped subcommand:

$ weknora doc viwe ""        # before
{"ok":false,"error":{"type":"input.invalid_argument","message":"argument 2 is empty","exit_code":5,...}}

$ weknora doc viwe ""        # after
{"ok":false,"error":{"type":"input.unknown_subcommand","message":"unknown subcommand \"viwe\" for \"weknora doc\"","exit_code":5,"hint":"did you mean: view? ..."}}

The input-hygiene gate runs in the root PersistentPreRunE, which cobra executes before a group command's RunE reports the unknown subcommand, so an empty positional masked the real mistake. Two runs of the same command line produced two different errors depending on whether the variable captured a value.

PersistentPreRunE now resolves the subcommand first: if the command has children and args[0] is not one of them, it returns unknownSubcommandRunE (the same typed error, hint and retry_argv the RunE produces) before hygiene runs. Hygiene is unchanged for every command that actually resolves.

Type of Change

  • 🐛 Bug fix

Related Issue

Fixes #6234

Testing

  • New TestUnknownSubcommand_WinsOverEmptyArgument in cmd/root_unknown_subcommand_test.go: executes tag create "" in-process and asserts input.unknown_subcommand naming "tag".
  • Reverse-verified by file-level rollback of cmd/root.go: on the pre-patch tree the same test fails with code = input.invalid_argument, want input.unknown_subcommand (argument 3 is empty); with the patch it passes.
  • cd cli && go test ./cmd/ ./internal/cmdutil/ ./acceptance/contract/ — passes.
  • End-to-end against the built binary: doc viwe "" and kb veiw "" now report the unknown subcommand with the "did you mean" hint; doc viwe x is unchanged.
  • gofmt -l clean; git diff --check clean.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted (gofmt)
  • Targeted tests for the changed packages pass
  • Diff-scoped lint (golangci-lint is not installed here; CI covers it)
  • Full-repository checks (not run: internal/agent/tools needs cgo + pg_query, which this host cannot build; unrelated to this diff)
  • Self-reviewed the code
  • Added tests covering the change
  • Updated related documentation (comment states the ordering and why)
  • Breaking changes: no — the resolved-command path is untouched; only the unknown-subcommand path reports a different (correct) error.

Screenshots / Recordings

Not a UI change.

`weknora tag create "$NAME"` with NAME unset failed as "argument 3 is
empty": the input-hygiene gate runs in PersistentPreRunE, which cobra
executes before dispatching to the group command, so the empty positional
was reported instead of the mistyped command - the same line without the
empty value reports `input.unknown_subcommand`.

Resolve the subcommand first and let the hygiene gate handle the command
that actually runs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CLI reports "argument N is empty" instead of the unknown subcommand when a typo carries an empty value

1 participant