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

cannot specify a path

Error message

cannot specify a path (`{raw_path}`) with a version (`{v}`).

What it means

Thrown by cargo add when a dependency is specified with both a version requirement (via crate@version syntax) and a --path flag. These are mutually exclusive: a path source uses whatever version is in the local Cargo.toml, so an explicit version requirement is not applicable.

Solutions

  1. Remove the version suffix from the crate spec: `cargo add foo --path ./local-foo`
  2. Or remove --path if you want a registry version: `cargo add foo@1.0`

Example fix

# before
cargo add foo@1.0 --path ./local-foo
# error: cannot specify a path with a version

# after — omit version when using --path
cargo add foo --path ./local-foo
Defensive patterns

Strategy: validation

Validate before calling

// Validate that version and path are not both specified
fn validate_add_args_path(crate_spec: &str, path: Option<&Path>) -> Result<(), String> {
    let has_version = crate_spec.contains('@');
    if has_version && path.is_some() {
        return Err("cannot specify a path with a version; remove one".into());
    }
    Ok(())
}

Prevention

When it happens

Trigger: Running `cargo add foo@1.0 --path ./local-foo` where both the @version suffix and --path flag are present.

Common situations: Copy-pasting from a registry-style command and adding --path. Forgetting to remove the version when switching to a local source.

Related errors


AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11). Data as JSON: /api/errors/560ce7af0134b794. Report an issue: GitHub.

Appendix: source

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

        let path = paths::normalize_path(&std::env::current_dir()?.join(raw_path));
        let mut src = PathSource::new(path);
        src.base = arg.base.clone();

        if let Some(base) = &arg.base {
            // Validate that the base is valid.
            let workspace_root = || Ok(ws.root_manifest().parent().unwrap());
            lookup_path_base(
                &PathBaseName::new(base.clone())?,
                &gctx,
                &workspace_root,
                spec.manifest().unstable_features(),
            )?;
        }

        let selected = if let Some(crate_spec) = &crate_spec {
            if let Some(v) = crate_spec.version_req() {
                // crate specifier includes a version (e.g. `docopt@0.8`)
                anyhow::bail!("cannot specify a path (`{raw_path}`) with a version (`{v}`).");
            }
            let dependency = crate_spec.to_dependency()?.set_source(src);
            let selected = select_package(&dependency, gctx, registry)?;
            if dependency.name != selected.name {
                gctx.shell().warn(format!(
                    "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

View on GitHub (pinned to 98a09e7e7d)