rust-lang/cargo · error

required(true)

Error message

required(true)

What it means

Cargo's `cargo remove` command reads its positional `dependencies` argument via clap's `get_many("dependencies").expect("required(true)")`. The argument is declared with `.required(true)` in the clap definition, so clap guarantees at least one value is present when parsing succeeds. This expect is an internal invariant: if it fires, the clap argument definition and the parser are out of sync — a Cargo programming bug, not a user error.

Source

Thrown at src/bin/cargo/commands/remove.rs:100

        }
        1 => packages[0],
        _ => {
            let names = packages.iter().map(|p| p.name()).collect::<Vec<_>>();
            return Err(CliError::new(
                anyhow::format_err!(
                    "no package selected to modify
help: specify a package with `-p <PKGID>`
      available packages: {}",
                    names.join(", ")
                ),
                101,
            ));
        }
    };

    let dependencies = args
        .get_many::<String>("dependencies")
        .expect("required(true)")
        .cloned()
        .collect::<Vec<_>>();

    let section = parse_section(args);

    let options = RemoveOptions {
        gctx,
        spec,
        dependencies,
        section,
        dry_run,
    };
    remove(&options)?;

    if !dry_run {
        // Clean up the workspace
        gc_workspace(&workspace)?;

View on GitHub (pinned to 0e07a15537)

Solutions

  1. If using a stock Cargo release, file a bug at https://github.com/rust-lang/cargo/issues with the `cargo remove` invocation and Cargo version — this path should be unreachable.
  2. If hacking on Cargo, verify the `dependencies` arg in the `clap::Args` struct still has `required = true` and that no `ArgGroup`/default handling strips it before `get_many` runs.
  3. Pin to a known-good Cargo / clap combination (`rustup override set <stable>`) until the regression is fixed.

Example fix

// before
let dependencies = args
    .get_many::<String>("dependencies")
    .expect("required(true)");
// after (defensive, only if the invariant genuinely cannot be upheld)
let dependencies = args
    .get_many::<String>("dependencies")
    .ok_or_else(|| anyhow::anyhow!("internal error: `dependencies` arg not present"))?
    .cloned()
    .collect::<Vec<_>>();
Defensive patterns

Strategy: validation

Validate before calling

// Nothing a *caller* of `cargo remove` can validate; this is an internal
// invariant. If you embed Cargo as a library and build the clap AppArgs
// yourself, assert the arg definition before invoking:
// (pseudo) ensure args.find("dependencies").map(|a| a.is_required_set()) == Some(true)

Prevention

When it happens

Trigger: Calling `cargo remove <dep>` after the clap `AppArgs` definition for `dependencies` was edited so it is no longer marked `.required(true)`, or a regression in clap's argument propagation that yields no values for a required arg. The panic happens at argument-consumption time, before any manifest work begins.

Common situations: Maintainers hacking on `src/bin/cargo/commands/remove.rs` who tweak the clap definition; upgrading clap across a major version that changes how `required(true)` interacts with `get_many`; running an unrepaired dev build of Cargo after a refactor of the `remove` subcommand.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/6c28d4d47078a371.json. Report an issue: GitHub.