{"id":"0246212e20e9059c","repo":"rust-lang/cargo","slug":"always-at-least-one-crate","errorCode":null,"errorMessage":"always at least one crate","messagePattern":"always at least one crate","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/bin/cargo/commands/add.rs","lineNumber":309,"sourceCode":"                if 1 < crates.len() {\n                    let candidates = crates\n                        .keys()\n                        .map(|c| {\n                            format!(\n                                \"`{}/{}`\",\n                                c.as_deref().expect(\"only none when there is 1\"),\n                                feature\n                            )\n                        })\n                        .collect::<Vec<_>>();\n                    anyhow::bail!(\n                        \"feature `{feature}` must be qualified by the dependency it's being activated for, like {}\",\n                        candidates.join(\", \")\n                    );\n                }\n                crates\n                    .first_mut()\n                    .expect(\"always at least one crate\")\n                    .1\n                    .get_or_insert_with(IndexSet::default)\n                    .insert(feature.to_owned());\n            }\n            FeatureValue::Dep { .. } => {\n                anyhow::bail!(\"feature `{feature}` is not allowed to use explicit `dep:` syntax\",)\n            }\n            FeatureValue::DepFeature {\n                dep_name,\n                dep_feature,\n                ..\n            } => {\n                if infer_crate_name {\n                    anyhow::bail!(\n                        \"`{feature}` is unsupported when inferring the crate name, use `{dep_feature}`\"\n                    );\n                }\n                if dep_feature.contains('/') {","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/bin/cargo/commands/add.rs#L291-L327","documentation":"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.","triggerScenarios":"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.","commonSituations":"Not user-triggerable in practice; if seen, it signals a bug in cargo's `add` command. Report with the invocation and version.","solutions":["Report a cargo bug with the exact `cargo add` command and `cargo --version`.","As a workaround, specify the crate name explicitly instead of relying on inference."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Not user-reachable normally; ensure a crate source is always selected.\nfn has_source(crates_empty: bool, path: Option<&str>, git: Option<&str>) -> bool {\n    !crates_empty || path.is_some() || git.is_some()\n}\n// when scripting cargo add, ensure at least one crate name or a path/git source is given","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always specify at least one crate name or a --path/--git source with `cargo add`.","If this panic appears, report a cargo bug with the invocation."],"tags":["cargo","cargo-add","panic","invariant","internal"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}