{"id":"4389257b3dd02d54","repo":"rust-lang/cargo","slug":"cannot-override-workspace-dependency-with-defau","errorCode":null,"errorMessage":"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","messagePattern":"cannot override workspace dependency with `--default-features`, either change `workspace\\.dependencies\\.(.+?)\\.default-features` or define the dependency exclusively in the package's manifest","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":723,"sourceCode":"/// the source of the dependency such as `version`, `registry`, `registry-index`,\n/// `path`, `git`, `branch`, `tag`, `rev`, or `package`. You can also not define\n/// `default-features`.\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) -> 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() {\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":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L705-L741","documentation":"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).","triggerScenarios":"`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`).","commonSituations":"Trying to toggle default-features for just one member of a workspace that centralizes that dependency.","solutions":["Change the setting centrally: edit `workspace.dependencies.<key>.default-features` in the root manifest.","Or stop inheriting: remove `workspace = true` for that member and define the dependency (with its own `default-features`) in the member's `[dependencies]`.","Avoid passing `--default-features`/`--no-default-features` for workspace-inherited deps."],"exampleFix":"# before (member tries to override)\ncargo add serde --no-default-features   # serde is workspace-inherited\n\n# after (central change)\n# root Cargo.toml:\n[workspace.dependencies]\nserde = { version = \"1\", default-features = false }","handlingStrategy":"validation","validationCode":"// Do not set default_features on a workspace-inherited DepOp.\nfn assert_no_override_on_ws(op: &DepOp, is_workspace: bool) -> Result<(), &'static str> {\n    if is_workspace && op.default_features.is_some() {\n        Err(\"edit workspace.dependencies.<key>.default-features instead of --default-features\")\n    } else { Ok(()) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Route default-features changes through the workspace root manifest.","Track which deps are workspace-inherited in a config map your add wrapper consults.","Document the override matrix (default-features/registry/rename) for contributors."],"tags":["cargo-add","workspace","inheritance","default-features"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}