{"id":"8b7c57be6f97b754","repo":"rust-lang/cargo","slug":"the-crate-depends-on-crate-multiple-time","errorCode":null,"errorMessage":"the crate `{}` depends on crate `{}` multiple times with different names","messagePattern":"the crate `(.+?)` depends on crate `(.+?)` multiple times with different names","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/resolver/resolve.rs","lineNumber":430,"sourceCode":"        to: PackageId,\n        to_target: &Target,\n    ) -> CargoResult<(InternedString, Option<InternedString>)> {\n        let empty_set: HashSet<Dependency> = HashSet::default();\n        let deps = if from == to {\n            &empty_set\n        } else {\n            self.dependencies_listed(from, to)\n        };\n\n        let target_crate_name = || (to_target.crate_name(), None);\n        let mut name_pairs = deps.iter().map(|d| {\n            d.explicit_name_in_toml()\n                .map(|s| (s.as_str().replace(\"-\", \"_\"), Some(s)))\n                .unwrap_or_else(target_crate_name)\n        });\n        let (extern_crate_name, dep_name) = name_pairs.next().unwrap_or_else(target_crate_name);\n        for (n, _) in name_pairs {\n            anyhow::ensure!(\n                n == extern_crate_name,\n                \"the crate `{}` depends on crate `{}` multiple times with different names\",\n                from,\n                to,\n            );\n        }\n        Ok((extern_crate_name.into(), dep_name))\n    }\n\n    fn dependencies_listed(&self, from: PackageId, to: PackageId) -> &HashSet<Dependency> {\n        // We've got a dependency on `from` to `to`, but this dependency edge\n        // may be affected by [replace]. If the `to` package is listed as the\n        // target of a replacement (aka the key of a reverse replacement map)\n        // then we try to find our dependency edge through that. If that fails\n        // then we go down below assuming it's not replaced.\n        //\n        // Note that we don't treat `from` as if it's been replaced because\n        // that's where the dependency originates from, and we only replace","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/resolver/resolve.rs#L412-L448","documentation":"In `extern_crate_name_and_dep_name`: a single source crate (`from`) lists two or more dependency edges to the same target crate (`to`) that resolve to different extern crate names. Rust forbids a crate from depending on the same dependency twice under different `extern` names through the normal dependency graph, so the resolver rejects this when building the name mapping.","triggerScenarios":"A `Cargo.toml` listing the same crate twice under different `package = ` rename keys, or as both a normal and dev/build dependency with conflicting `explicit_name_in_toml`, such that two dependency entries point at the same package id with different renamed identifiers. The `for (n, _) in name_pairs` loop finds a mismatched `n`.","commonSituations":"Renaming a dependency via `package =` in two dependency tables (normal + dev) inconsistently; copy-paste dependency entries; a `[dependencies]` and `[dev-dependencies]` block both pulling the same crate with different alias names.","solutions":["Consolidate the duplicate dependency into a single entry with one consistent name/alias.","If you need the crate under two configurations (e.g. normal + dev), use the same `package` rename in both tables.","Remove the redundant `Cargo.toml` dependency line causing the second edge to the same package."],"exampleFix":"# before\n[dependencies]\nmy-serde = { package = \"serde\", version = \"1\" }\n[dev-dependencies]\nserde2 = { package = \"serde\", version = \"1\" }\n# after\n[dependencies]\nserde = \"1\"\n[dev-dependencies]\nserde = \"1\"","handlingStrategy":"validation","validationCode":"# Lint Cargo.toml for duplicate target crates with different aliases:\n# e.g. flag two keys with package = \"<same crate>\"\ntomlq -r '.dependencies | to_entries[] | select(.value.package?) | \"\\(.value.package)\\t\\(.key)\"' Cargo.toml \\\n  | sort | uniq -c | awk '$1>1{print \"duplicate dependency:\",$2}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["List each external crate once in `Cargo.toml`; reuse it across `[dev-dependencies]` with the same name.","Use a single consistent `package = ` alias if renaming.","Run a manifest linter (e.g. `cargo-sort`, `cargo-deny`) in CI."],"tags":["dependency","naming","manifest","duplicate"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}