Ray's Knowledge Base
Unlock

With clap_derive defer = true, a struct doc comment replaces the about text and a field can clash with a global arg

PitfallVerified 5 Nov 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

You add #[command(defer = true)] to a clap derive Subcommand enum to build only the subcommand that runs. Then:

  • -h and generated help show the wrong about text for many commands (5,885 help differences in one CLI).
  • A debug assert fires: Argument names must be unique.
  • Code that walks the command tree sees subcommands with no args.

Cause#

defer = true wraps each subcommand's augment in .defer(..). The deferred augment runs after the command is set up. So:

  1. A /// doc comment on a #[derive(Args)] struct, or on a nested #[derive(Subcommand)] enum, replaces the command's about.
  2. Global args propagate before the deferred args exist. A field with the id of a global arg is then added twice.
  3. A walker that reads a subcommand gets an unbuilt command.

Fix#

  • Change /// to // on every Args struct and nested Subcommand enum.
  • Remove a field that has the same id as a global arg. Read the global value instead.
  • Build the subcommand before you read it. A helper that does this: let mut cmd = cmd.clone().bin_name(path); cmd.build(); cmd. Use it in -h, help <name> and any tree walk.
  • Compare old and new help output. Only intended lines should differ (36 of 8,571 here).

Evidence#

clap_derive 4.6.7 in rgit. Result: rev-parse HEAD dropped to 59 M instructions (git 65 M), 14 MiB RSS. A new test building_a_subcommand_keeps_its_text guards the about text.

Also found by searching

  • thread 'main' panicked: Argument names must be unique
  • clap subcommand help shows the wrong about text after adding defer
  • doc comment on a clap Args struct replaces the command about
  • how do I build only the subcommand that runs in clap derive