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

cannot override workspace dependency with `{flag}`, either c

Error message

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

What it means

Thrown by check_invalid_ws_keys() when --default-features is passed while adding a dependency that inherits from the workspace, and the package's edition is earlier than 2024. Starting with edition 2024, default-features can be overridden per-package, but earlier editions require the override at the workspace level.

Source

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

/// Prior to Edition 2024 (RFC 3945) `default-features` was also forbidden;
/// from Edition 2024 onwards a package-level `default-features` overrides the
/// workspace value, so it is allowed.
///
/// 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, edition: Edition) -> 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() && edition < Edition::Edition2024 {
        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 eb98b54bc9)

Solutions

  1. Change the default-features setting in the [workspace.dependencies] entry in the workspace root Cargo.toml
  2. Define the dependency exclusively in the package manifest instead of inheriting from the workspace
  3. Upgrade the package edition to 2024: `cargo fix --edition` then set `edition = "2024"` in [package]

Example fix

# before — edition 2021, trying to override
cargo add foo --default-features
# error: cannot override workspace dependency with `--default-features`

# after — change at workspace level
# (workspace root Cargo.toml)
[workspace.dependencies]
foo = { version = "1.0", default-features = false }
Defensive patterns

Strategy: validation

Validate before calling

// Check edition before allowing default-features override
fn validate_ws_override(edition: &str, default_features: Option<bool>) -> Result<(), String> {
    if default_features.is_some() && edition != "2024" {
        return Err(concat!(
            "cannot override workspace dependency with --default-features ",
            "in editions before 2024; change workspace.dependencies or upgrade edition"
        ).into());
    }
    Ok(())
}

Prevention

When it happens

Trigger: Running `cargo add foo --default-features` (or --no-default-features) where foo is declared in [workspace.dependencies] and the current package uses edition 2021 or earlier.

Common situations: Trying to toggle default-features on a workspace-inherited dependency in a pre-2024 edition crate. Migrating to workspace dependencies without upgrading edition.

Related errors


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