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

cannot override workspace dependency with `--rename`, either

Error message

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

What it means

The third override guard: `--rename` is rejected for workspace-inherited dependencies because a member-level rename maps to the `package` field, which must be consistent across the workspace. The bail at mod.rs:733 directs the user to `workspace.dependencies.<key>.package` or to a local member-level definition.

Source

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

        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>,
) -> CargoResult<bool> {
    match rust_version {
        Some(version) => {
            let syntax_support_version = RustVersion::new(1, 60, 0);

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Perform the rename centrally by setting `workspace.dependencies.<key>.package = "<real-name>"`.
  2. Or define the dependency locally in the member with `package = "..."` instead of `workspace = true`.
  3. Drop `--rename` and accept the workspace key as the dependency name.

Example fix

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

# after (root Cargo.toml)
[workspace.dependencies]
my-serde = { package = "serde", version = "1" }
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: `cargo add <workspace-inherited-crate> --rename other_name` sets `arg.rename` and trips the guard in `check_invalid_ws_keys`.

Common situations: Renaming a dependency in one member to avoid a name clash while the workspace treats it under the canonical key.

Related errors


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