rust-lang/cargo · critical

always at least one crate

Error message

always at least one crate

What it means

Internal invariant panic in `cargo add` (src/bin/cargo/commands/add.rs:309). In the single-crate branch (`crates.len() == 1`, i.e. not `> 1`), cargo calls `crates.first_mut().expect("always at least one crate")`. Clap guarantees at least one crate or a path/git source (an `unreachable!` guards the empty case at line 278), so reaching this point with an empty map should be impossible.

Source

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

                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",)
            }
            FeatureValue::DepFeature {
                dep_name,
                dep_feature,
                ..
            } => {
                if infer_crate_name {
                    anyhow::bail!(
                        "`{feature}` is unsupported when inferring the crate name, use `{dep_feature}`"
                    );
                }
                if dep_feature.contains('/') {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Report a cargo bug with the exact `cargo add` command and `cargo --version`.
  2. As a workaround, specify the crate name explicitly instead of relying on inference.
Defensive patterns

Strategy: validation

Validate before calling

// Not user-reachable normally; ensure a crate source is always selected.
fn has_source(crates_empty: bool, path: Option<&str>, git: Option<&str>) -> bool {
    !crates_empty || path.is_some() || git.is_some()
}
// when scripting cargo add, ensure at least one crate name or a path/git source is given

Prevention

When it happens

Trigger: Unreachable in normal use — would require `crates` to be empty at the feature-assignment stage, which clap argument validation and the path/git fallback at lines 273-280 prevent. Only a regression in `cargo add` could trigger it.

Common situations: Not user-triggerable in practice; if seen, it signals a bug in cargo's `add` command. Report with the invocation and version.

Related errors


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