{"id":"7be6cf58a5101f34","repo":"rust-lang/cargo","slug":"overlapping-replacement-specifications-found","errorCode":null,"errorMessage":"overlapping replacement specifications found:\n\n  * {}\n  * {}\n\nboth specifications match: {}","messagePattern":"overlapping replacement specifications found:\n\n  \\* (.+?)\n  \\* (.+?)\n\nboth specifications match: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/resolver/dep_cache.rs","lineNumber":177,"sourceCode":"                    \"replacement specification `{}` matched {} and tried to override it with {}\\n\\\n                     avoid matching unrelated packages by being more specific\",\n                    spec,\n                    summary.version(),\n                    s.version(),\n                ));\n            }\n\n            let replace = if s.source_id() == summary.source_id() {\n                debug!(\"Preventing\\n{:?}\\nfrom replacing\\n{:?}\", summary, s);\n                None\n            } else {\n                Some(s)\n            };\n            let matched_spec = spec.clone();\n\n            // Make sure no duplicates\n            if let Some((spec, _)) = potential_matches.next() {\n                return Err(anyhow::anyhow!(\n                    \"overlapping replacement specifications found:\\n\\n  \\\n                     * {}\\n  * {}\\n\\nboth specifications match: {}\",\n                    matched_spec,\n                    spec,\n                    summary.package_id()\n                ));\n            }\n\n            for dep in summary.dependencies() {\n                debug!(\"\\t{} => {}\", dep.package_name(), dep.version_req());\n            }\n            if let Some(r) = replace {\n                self.used_replacements\n                    .borrow_mut()\n                    .insert(summary.package_id(), r);\n            }\n        }\n","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/resolver/dep_cache.rs#L159-L195","documentation":"Raised when two distinct `[replace]` specifications both match the same resolved package. After matching the first spec, the code checks `potential_matches.next()`; a second match means overlapping replacement rules, which would make the substitution non-deterministic. Cargo rejects this to keep the dependency graph well-defined.","triggerScenarios":"Two `[replace]` entries in `Cargo.toml` whose specs are broad enough that both match one package id — e.g. `[replace] \"foo:*\"` and `[replace] \"foo:1.*\"` both matching `foo 1.2.3`. The second `potential_matches.next()` returns `Some`, triggering the bail with both specs and the colliding package id.","commonSituations":"Accidentally duplicating a replace rule with different specificity; combining a workspace-level and a crate-level `[replace]` for the same package; leftover replace entries after refactoring dependencies.","solutions":["Audit `Cargo.toml` `[replace]` entries and remove duplicates so only one spec matches any given package.","Narrow each replace spec's version requirement so their match sets are disjoint.","Consolidate into a single `[patch]` entry, which does not have this overlapping-spec restriction."],"exampleFix":"# before\n[replace]\n\"foo:1.0.0\" = { path = \"../foo\" }\n\"foo:1.*\"   = { path = \"../foo\" }\n# after\n[patch.crates-io]\nfoo = { path = \"../foo\" }","handlingStrategy":"validation","validationCode":"# Lint for overlapping [replace] specs:\nif grep -c '^\".*:.*\" = ' Cargo.toml | grep -qw ...; then\n  echo 'check for duplicate/overlapping replace specs';\nfi\n# Better: cargo check will surface the overlap; run it in pre-commit.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Maintain at most one spec per replaced package; remove stale entries.","Use disjoint version requirements across `[replace]` specs.","Migrate to `[patch]`, which tolerates overlapping patches deterministically."],"tags":["replace","dependency-resolution","config","duplicate"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}