{"record":{"id":"779ad84ea992cc7c","repo":"rust-lang/cargo","slug":"dependency-name-is-required","errorCode":null,"errorMessage":"dependency name is required","messagePattern":"dependency name is required","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":434,"sourceCode":"                    \"translating `{}` to `{}`\",\n                    dependency.name, selected.name,\n                ))?;\n            }\n            selected\n        } else {\n            let source = crate::sources::PathSource::new(&src.path, src.source_id()?, gctx);\n            let package = source.root_package()?;\n            let mut selected = Dependency::from(package.summary());\n            if let Some(Source::Path(selected_src)) = &mut selected.source {\n                selected_src.base = src.base;\n            }\n            selected\n        };\n        selected\n    } else if let Some(crate_spec) = &crate_spec {\n        crate_spec.to_dependency()?\n    } else {\n        anyhow::bail!(\"dependency name is required\");\n    };\n    selected_dep = populate_dependency(selected_dep, arg);\n\n    let lookup = |dep_key: &_| {\n        get_existing_dependency(\n            ws,\n            spec.manifest().unstable_features(),\n            manifest,\n            dep_key,\n            section,\n        )\n    };\n    let old_dep = fuzzy_lookup(&mut selected_dep, lookup, gctx)?;\n    let mut dependency = if let Some(mut old_dep) = old_dep.clone() {\n        if old_dep.name != selected_dep.name {\n            // Assuming most existing keys are not relevant when the package changes\n            if selected_dep.optional.is_none() {\n                selected_dep.optional = old_dep.optional;","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_add/mod.rs#L416-L452","documentation":"Thrown by cargo add when no dependency name or source is provided at all. This is the final else branch after checking for git source, path source, and crate_spec — none were given.","triggerScenarios":"Running `cargo add` with no positional argument and no --git or --path flags. This can happen with scripting errors, malformed command construction, or empty variable expansion.","commonSituations":"Shell script with an empty variable: `cargo add $DEP_NAME` where DEP_NAME is unset. Programmatic invocation that forgets to pass the crate name. Accidental enter on an incomplete command.","solutions":["Provide a crate name as a positional argument: `cargo add serde`","If using a shell variable, ensure it is set and non-empty: `cargo add \"${DEP_NAME:?must be set}\"`","If adding from a path or git, ensure the --path or --git flag is provided"],"exampleFix":"# before — empty variable\nDEP_NAME=\"\"\ncargo add \"$DEP_NAME\"\n# error: dependency name is required\n\n# after\nDEP_NAME=\"serde\"\ncargo add \"$DEP_NAME\"","handlingStrategy":"validation","validationCode":"// Ensure a dependency name is provided\nfn validate_add_command(crate_spec: &Option<String>, git: &Option<String>, path: &Option<PathBuf>) -> Result<(), String> {\n    if crate_spec.is_none() && git.is_none() && path.is_none() {\n        return Err(\"dependency name is required; provide a crate name or source\".into());\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a crate name as the positional argument to cargo add","In shell scripts, guard against empty variables: cargo add \"${DEP:?must be set}\"","Validate script inputs before constructing cargo add commands"],"tags":["cargo-add","missing-argument"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}