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

cannot override workspace dependency with `--registry`, eith

Error message

cannot override workspace dependency with `--registry`, either change `workspace.dependencies.{toml_key}.registry` or define the dependency exclusively in the package's manifest

What it means

Same override guard as the default-features case, but for `--registry`. A workspace-inherited dependency's registry is controlled centrally; passing `--registry` per member is rejected at mod.rs:729. The message points the user to `workspace.dependencies.<key>.registry` or to defining the dependency locally.

Source

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

/// `version`  should all bee checked before this is called. `rename` is checked
/// for as it turns into `package`
fn check_invalid_ws_keys(toml_key: &str, arg: &DepOp) -> CargoResult<()> {
    fn err_msg(toml_key: &str, flag: &str, field: &str) -> String {
        format!(
            "cannot override workspace dependency with `{flag}`, \
            either change `workspace.dependencies.{toml_key}.{field}` \
            or define the dependency exclusively in the package's manifest"
        )
    }

    if arg.default_features.is_some() {
        anyhow::bail!(
            "{}",
            err_msg(toml_key, "--default-features", "default-features")
        )
    }
    if arg.registry.is_some() {
        anyhow::bail!("{}", err_msg(toml_key, "--registry", "registry"))
    }
    // rename is `package`
    if arg.rename.is_some() {
        anyhow::bail!("{}", err_msg(toml_key, "--rename", "package"))
    }
    Ok(())
}

/// When the `--optional` option is added using `cargo add`, we need to
/// check the current rust-version. As the `dep:` syntax is only available
/// starting with Rust 1.60.0
///
/// `true` means that the rust-version is None or the rust-version is higher
/// than the version needed.
///
/// Note: Previous versions can only use the implicit feature name.
fn check_rust_version_for_optional_dependency(
    rust_version: Option<&RustVersion>,

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Set the registry centrally in `workspace.dependencies.<key>.registry`.
  2. Or define the dependency exclusively in the member's manifest (drop `workspace = true`) and specify `--registry` there.
  3. Drop the `--registry` flag if the workspace default is correct.

Example fix

# before
cargo add serde --registry my-registry   # workspace-inherited

# after (local definition in member)
# member Cargo.toml:
[dependencies]
serde = { version = "1", registry = "my-registry" }
Defensive patterns

Strategy: validation

Validate before calling

fn assert_no_registry_override_on_ws(op: &DepOp, is_workspace: bool) -> Result<(), &'static str> {
    if is_workspace && op.registry.is_some() {
        Err("edit workspace.dependencies.<key>.registry instead of --registry")
    } else { Ok(()) }
}

Prevention

When it happens

Trigger: `cargo add <workspace-inherited-crate> --registry my-registry` — `arg.registry.is_some()` while the dep resolves to a workspace dependency.

Common situations: Using a private registry for a single member while the workspace pins crates.io, or migrating one crate to an alternate registry.

Related errors


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