{"id":"7a63f89080a51d71","repo":"rust-lang/cargo","slug":"cannot-override-workspace-dependency-with-regis","errorCode":null,"errorMessage":"cannot override workspace dependency with `--registry`, either change `workspace.dependencies.{toml_key}.registry` or define the dependency exclusively in the package's manifest","messagePattern":"cannot override workspace dependency with `--registry`, either change `workspace\\.dependencies\\.(.+?)\\.registry` 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":729,"sourceCode":"/// `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///\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>,","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L711-L747","documentation":"Same override guard as the default-features case, but for `--registry`. A workspace-inherited dependency's registry is controlled centrally; passing `--registry` per member is rejected at mod.rs:729. The message points the user to `workspace.dependencies.<key>.registry` or to defining the dependency locally.","triggerScenarios":"`cargo add <workspace-inherited-crate> --registry my-registry` — `arg.registry.is_some()` while the dep resolves to a workspace dependency.","commonSituations":"Using a private registry for a single member while the workspace pins crates.io, or migrating one crate to an alternate registry.","solutions":["Set the registry centrally in `workspace.dependencies.<key>.registry`.","Or define the dependency exclusively in the member's manifest (drop `workspace = true`) and specify `--registry` there.","Drop the `--registry` flag if the workspace default is correct."],"exampleFix":"# before\ncargo add serde --registry my-registry   # workspace-inherited\n\n# after (local definition in member)\n# member Cargo.toml:\n[dependencies]\nserde = { version = \"1\", registry = \"my-registry\" }","handlingStrategy":"validation","validationCode":"fn assert_no_registry_override_on_ws(op: &DepOp, is_workspace: bool) -> Result<(), &'static str> {\n    if is_workspace && op.registry.is_some() {\n        Err(\"edit workspace.dependencies.<key>.registry instead of --registry\")\n    } else { Ok(()) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralize registry choice in `[workspace.dependencies]`.","If a single member needs an alternate registry, drop inheritance for that member.","Keep a registry map in the root manifest and reference it by key."],"tags":["cargo-add","workspace","inheritance","registry"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}