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

cannot override workspace dependency with `--default-feature

Error message

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

What it means

For a dependency inherited from `[workspace.dependencies]`, cargo-add forbids per-member overrides of source-shaping keys because they would silently diverge from the workspace definition. Specifically `--default-features` is rejected: the message tells the caller to either edit `workspace.dependencies.<key>.default-features` or drop workspace inheritance and define the dep locally. The check lives in `check_invalid_ws_keys` (mod.rs:722).

Source

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

/// the source of the dependency such as `version`, `registry`, `registry-index`,
/// `path`, `git`, `branch`, `tag`, `rev`, or `package`. You can also not define
/// `default-features`.
///
/// Only `default-features`, `registry` and `rename` need to be checked
///  for currently. This is because `git` and its associated keys, `path`, and
/// `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
///

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Change the setting centrally: edit `workspace.dependencies.<key>.default-features` in the root manifest.
  2. Or stop inheriting: remove `workspace = true` for that member and define the dependency (with its own `default-features`) in the member's `[dependencies]`.
  3. Avoid passing `--default-features`/`--no-default-features` for workspace-inherited deps.

Example fix

# before (member tries to override)
cargo add serde --no-default-features   # serde is workspace-inherited

# after (central change)
# root Cargo.toml:
[workspace.dependencies]
serde = { version = "1", default-features = false }
Defensive patterns

Strategy: validation

Validate before calling

// Do not set default_features on a workspace-inherited DepOp.
fn assert_no_override_on_ws(op: &DepOp, is_workspace: bool) -> Result<(), &'static str> {
    if is_workspace && op.default_features.is_some() {
        Err("edit workspace.dependencies.<key>.default-features instead of --default-features")
    } else { Ok(()) }
}

Prevention

When it happens

Trigger: `cargo add <crate>` where `<crate>` is already a `workspace = true` dependency, combined with `--default-features` (or `--no-default-features`-equivalent flag that sets `arg.default_features`).

Common situations: Trying to toggle default-features for just one member of a workspace that centralizes that dependency.

Related errors


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