rust-lang/cargo · critical

only none when there is 1

Error message

only none when there is 1

What it means

Internal invariant panic in `cargo add` feature parsing (src/bin/cargo/commands/add.rs:297). When `crates.len() > 1` (multiple crates being added) and cargo formats the disambiguation candidates `"<crate>/<feature>"`, it calls `.expect("only none when there is 1")` on each crate key, asserting that a `None` key only exists in the single-crate (path/git) case. By construction the `crates` map either holds all-`Some(name)` keys (named crates) or a single `None` key (path/git with no names), so a `None` key with >1 entries should be impossible.

Source

Thrown at src/bin/cargo/commands/add.rs:297

        }
    }
    for feature in matches
        .get_many::<String>("features")
        .into_iter()
        .flatten()
        .map(String::as_str)
        .flat_map(parse_feature)
    {
        let parsed_value = FeatureValue::new(feature.into());
        match parsed_value {
            FeatureValue::Feature(_) => {
                if 1 < crates.len() {
                    let candidates = crates
                        .keys()
                        .map(|c| {
                            format!(
                                "`{}/{}`",
                                c.as_deref().expect("only none when there is 1"),
                                feature
                            )
                        })
                        .collect::<Vec<_>>();
                    anyhow::bail!(
                        "feature `{feature}` must be qualified by the dependency it's being activated for, like {}",
                        candidates.join(", ")
                    );
                }
                crates
                    .first_mut()
                    .expect("always at least one crate")
                    .1
                    .get_or_insert_with(IndexSet::default)
                    .insert(feature.to_owned());
            }
            FeatureValue::Dep { .. } => {
                anyhow::bail!("feature `{feature}` is not allowed to use explicit `dep:` syntax",)

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Report a bug to the cargo project with the full `cargo add ...` command and cargo version.
  2. As a workaround, add crates one at a time rather than passing multiple names with --features.
Defensive patterns

Strategy: validation

Validate before calling

// Not user-reachable; guard at the CLI layer by ensuring you pass explicit crate names
// when using --features with multiple crates.
fn names_all_some(keys: &[Option<String>]) -> bool { keys.iter().all(|k| k.is_some()) }
// when invoking cargo add with multiple crates, pass names explicitly so no None key appears

Prevention

When it happens

Trigger: Effectively unreachable through normal CLI use; would require `crates` to contain more than one entry with at least one `None` key — the construction logic at lines 253-280 prevents this. Triggerable only via an internal bug or memory corruption.

Common situations: Not user-reachable in practice; if observed it indicates a regression in `cargo add`'s argument handling. Report it as a cargo bug with the exact `cargo add` invocation.

Related errors


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