rust-lang/cargo · error · anyhow::Error
dependency name is required
Error message
dependency name is required
What it means
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.
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
Example fix
# before — empty variable DEP_NAME="" cargo add "$DEP_NAME" # error: dependency name is required # after DEP_NAME="serde" cargo add "$DEP_NAME"
Defensive patterns
Strategy: validation
Validate before calling
// Ensure a dependency name is provided
fn validate_add_command(crate_spec: &Option<String>, git: &Option<String>, path: &Option<PathBuf>) -> Result<(), String> {
if crate_spec.is_none() && git.is_none() && path.is_none() {
return Err("dependency name is required; provide a crate name or source".into());
}
Ok(())
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- all versions of crate
- cannot add ` ` as a dependency to itself
- cannot override workspace dependency with
- cannot specify a git URL
- cannot specify a path
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/779ad84ea992cc7c.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_add/mod.rs:434
"translating `{}` to `{}`",
dependency.name, selected.name,
))?;
}
selected
} else {
let source = crate::sources::PathSource::new(&src.path, src.source_id()?, gctx);
let package = source.root_package()?;
let mut selected = Dependency::from(package.summary());
if let Some(Source::Path(selected_src)) = &mut selected.source {
selected_src.base = src.base;
}
selected
};
selected
} else if let Some(crate_spec) = &crate_spec {
crate_spec.to_dependency()?
} else {
anyhow::bail!("dependency name is required");
};
selected_dep = populate_dependency(selected_dep, arg);
let lookup = |dep_key: &_| {
get_existing_dependency(
ws,
spec.manifest().unstable_features(),
manifest,
dep_key,
section,
)
};
let old_dep = fuzzy_lookup(&mut selected_dep, lookup, gctx)?;
let mut dependency = if let Some(mut old_dep) = old_dep.clone() {
if old_dep.name != selected_dep.name {
// Assuming most existing keys are not relevant when the package changes
if selected_dep.optional.is_none() {
selected_dep.optional = old_dep.optional;View on GitHub (pinned to 98a09e7e7d)