rust-lang/cargo · error · anyhow::Error

dependency name is required

Error message

dependency name is required

What it means

`cargo add` requires at least a crate specifier, a `--git`, or a `--path` to know which dependency to add. If all three are absent the resolver reaches the final `else` branch at mod.rs:432 and bails with "dependency name is required". This guards against an empty/no-op `DepOp` reaching the registry query stage.

Source

Thrown at src/ops/cargo_add/mod.rs:432

                    "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 0e07a15537)

Solutions

  1. Supply a crate name: `cargo add <name>` or set `DepOp.crate_spec = Some("name".into())`.
  2. If you meant to modify an existing dependency rather than add one, use `cargo add <existing-name>` (it merges) — you still must name it.
  3. Validate `DepOp` before calling `add()` by asserting at least one of `crate_spec`, `git`, `path` is `Some`.

Example fix

// before
let op = DepOp { crate_spec: None, git: None, path: None, ..Default::default() };

// after
let op = DepOp { crate_spec: Some("serde".into()), ..Default::default() };
Defensive patterns

Strategy: type-guard

Validate before calling

// Ensure the DepOp names a source before calling add().
fn dep_op_has_target(op: &DepOp) -> bool {
    op.crate_spec.is_some() || op.git.is_some() || op.path.is_some()
}

// assert!(dep_op_has_target(&op), "DepOp needs crate_spec, git, or path");

Type guard

// Newtype that statically guarantees a target is present.
struct AddableDepOp { inner: DepOp }
impl AddableDepOp {
    fn new(op: DepOp) -> Result<Self, &'static str> {
        match (op.crate_spec.is_some(), op.git.is_some(), op.path.is_some()) {
            (true, _, _) | (_, true, _) | (_, _, true) => Ok(Self { inner: op }),
            _ => Err("DepOp requires crate_spec, git, or path"),
        }
    }
}

Prevention

When it happens

Trigger: A `DepOp` constructed with `crate_spec: None`, `git: None`, `path: None` (e.g. only flags like `--features` or `--optional` with no target), or a CLI invocation like `cargo add --features foo` with no crate name.

Common situations: Programmatic callers building `DepOp` to tweak an existing dependency without naming it, or shell alias mishaps that drop the positional crate argument.

Related errors


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