{"record":{"id":"ffa377e8c1a495c1","repo":"rust-lang/cargo","slug":"cannot-override-workspace-dependency-with-flag","errorCode":null,"errorMessage":"cannot override workspace dependency with `{flag}`, either change `workspace.dependencies.{toml_key}.{field}` or define the dependency exclusively in the package's manifest","messagePattern":"cannot override workspace dependency with `(.+?)`, either change `workspace\\.dependencies\\.(.+?)\\.(.+?)` or define the dependency exclusively in the package's manifest","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":739,"sourceCode":"/// Prior to Edition 2024 (RFC 3945) `default-features` was also forbidden;\n/// from Edition 2024 onwards a package-level `default-features` overrides the\n/// workspace value, so it is allowed.\n///\n/// Only `default-features`, `registry` and `rename` need to be checked\n///  for currently. This is because `git` and its associated keys, `path`, and\n/// `version`  should all bee checked before this is called. `rename` is checked\n/// for as it turns into `package`\nfn check_invalid_ws_keys(toml_key: &str, arg: &DepOp, edition: Edition) -> CargoResult<()> {\n    fn err_msg(toml_key: &str, flag: &str, field: &str) -> String {\n        format!(\n            \"cannot override workspace dependency with `{flag}`, \\\n            either change `workspace.dependencies.{toml_key}.{field}` \\\n            or define the dependency exclusively in the package's manifest\"\n        )\n    }\n\n    if arg.default_features.is_some() && edition < Edition::Edition2024 {\n        anyhow::bail!(\n            \"{}\",\n            err_msg(toml_key, \"--default-features\", \"default-features\")\n        )\n    }\n    if arg.registry.is_some() {\n        anyhow::bail!(\"{}\", err_msg(toml_key, \"--registry\", \"registry\"))\n    }\n    // rename is `package`\n    if arg.rename.is_some() {\n        anyhow::bail!(\"{}\", err_msg(toml_key, \"--rename\", \"package\"))\n    }\n    Ok(())\n}\n\n/// When the `--optional` option is added using `cargo add`, we need to\n/// check the current rust-version. As the `dep:` syntax is only available\n/// starting with Rust 1.60.0\n///","sourceCodeStart":721,"sourceCodeEnd":757,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/ops/cargo_add/mod.rs#L721-L757","documentation":"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.","triggerScenarios":"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.","commonSituations":"Trying to toggle default-features on a workspace-inherited dependency in a pre-2024 edition crate. Migrating to workspace dependencies without upgrading edition.","solutions":["Change the default-features setting in the [workspace.dependencies] entry in the workspace root Cargo.toml","Define the dependency exclusively in the package manifest instead of inheriting from the workspace","Upgrade the package edition to 2024: `cargo fix --edition` then set `edition = \"2024\"` in [package]"],"exampleFix":"# before — edition 2021, trying to override\ncargo add foo --default-features\n# error: cannot override workspace dependency with `--default-features`\n\n# after — change at workspace level\n# (workspace root Cargo.toml)\n[workspace.dependencies]\nfoo = { version = \"1.0\", default-features = false }","handlingStrategy":"validation","validationCode":"// Check edition before allowing default-features override\nfn validate_ws_override(edition: &str, default_features: Option<bool>) -> Result<(), String> {\n    if default_features.is_some() && edition != \"2024\" {\n        return Err(concat!(\n            \"cannot override workspace dependency with --default-features \",\n            \"in editions before 2024; change workspace.dependencies or upgrade edition\"\n        ).into());\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["For pre-2024 editions, set default-features at the [workspace.dependencies] level","Upgrade to edition 2024 to enable per-package default-features overrides on workspace deps","Define the dependency exclusively in the package manifest if you need package-specific overrides"],"tags":["cargo-add","workspace","dependency","default-features","edition"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}