{"id":"e779a6ba7687dbeb","repo":"rust-lang/cargo","slug":"cannot-override-workspace-dependency-with-renam","errorCode":null,"errorMessage":"cannot override workspace dependency with `--rename`, either change `workspace.dependencies.{toml_key}.package` or define the dependency exclusively in the package's manifest","messagePattern":"cannot override workspace dependency with `--rename`, either change `workspace\\.dependencies\\.(.+?)\\.package` 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":733,"sourceCode":"        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///\n/// `true` means that the rust-version is None or the rust-version is higher\n/// than the version needed.\n///\n/// Note: Previous versions can only use the implicit feature name.\nfn check_rust_version_for_optional_dependency(\n    rust_version: Option<&RustVersion>,\n) -> CargoResult<bool> {\n    match rust_version {\n        Some(version) => {\n            let syntax_support_version = RustVersion::new(1, 60, 0);","sourceCodeStart":715,"sourceCodeEnd":751,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L715-L751","documentation":"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.","triggerScenarios":"`cargo add <workspace-inherited-crate> --rename other_name` sets `arg.rename` and trips the guard in `check_invalid_ws_keys`.","commonSituations":"Renaming a dependency in one member to avoid a name clash while the workspace treats it under the canonical key.","solutions":["Perform the rename centrally by setting `workspace.dependencies.<key>.package = \"<real-name>\"`.","Or define the dependency locally in the member with `package = \"...\"` instead of `workspace = true`.","Drop `--rename` and accept the workspace key as the dependency name."],"exampleFix":"# before\ncargo add serde --rename my-serde   # workspace-inherited\n\n# after (root Cargo.toml)\n[workspace.dependencies]\nmy-serde = { package = \"serde\", version = \"1\" }","handlingStrategy":"validation","validationCode":"fn assert_no_rename_override_on_ws(op: &DepOp, is_workspace: bool) -> Result<(), &'static str> {\n    if is_workspace && op.rename.is_some() {\n        Err(\"edit workspace.dependencies.<key>.package instead of --rename\")\n    } else { Ok(()) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Express renames centrally via `package = \"...\"` in the workspace table.","Avoid per-member `--rename` for shared dependencies.","Maintain a naming-conventions doc so members use canonical keys."],"tags":["cargo-add","workspace","inheritance","rename"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}