{"id":"bb992b54c7908785","repo":"rust-lang/cargo","slug":"replacement-specification-matched-and-trie","errorCode":null,"errorMessage":"replacement specification `{}` matched {} and tried to override it with {}\navoid matching unrelated packages by being more specific","messagePattern":"replacement specification `(.+?)` matched (.+?) and tried to override it with (.+?)\navoid matching unrelated packages by being more specific","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/resolver/dep_cache.rs","lineNumber":158,"sourceCode":"                    .iter()\n                    .map(|s| format!(\"  * {}\", s.package_id()))\n                    .collect::<Vec<_>>();\n                return Err(anyhow::anyhow!(\n                    \"the replacement specification `{}` matched \\\n                     multiple packages:\\n  * {}\\n{}\",\n                    spec,\n                    s.package_id(),\n                    bullets.join(\"\\n\")\n                ));\n            }\n\n            assert_eq!(\n                s.name(),\n                summary.name(),\n                \"dependency should be hard coded to have the same name\"\n            );\n            if s.version() != summary.version() {\n                return Err(anyhow::anyhow!(\n                    \"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() {","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/resolver/dep_cache.rs#L140-L176","documentation":"Emitted in `dep_cache.rs` after a replacement matched: the matched override package `s` has a different version than the `summary` being replaced. The assertion ensures name equality, then explicitly rejects version mismatch because `[replace]` semantics expect the override to stand in for the same version. Substituting a different version would silently change the dependency graph.","triggerScenarios":"A `[replace]` spec's target dependency (the right-hand side) resolves to a version different from the package on the left being replaced — e.g. replacing `foo:1.2.3` but the replacement source publishes `foo` at `1.2.4`. The `s.version() != summary.version()` branch fires.","commonSituations":"Replacement source (path/git fork) was updated and re-published at a new version after the `[replace]` entry was written; mismatched version req in the replacement dependency declaration; using `[replace]` where you actually want a version bump (which `[replace]` forbids by design).","solutions":["Align the replacement package's version with the original (bump the replaced package's version in its `Cargo.toml` to match, or pin both sides to the same version).","Use `[patch]` instead of `[replace]` if you intend to substitute a different version of a dependency.","Make the replacement spec more specific so it cannot accidentally match a differently-versioned package."],"exampleFix":"# before\n[replace]\n\"foo:1.2.3\" = { git = \"https://example/foo-fork\" }  # fork is at 1.2.4\n# after\n# edit fork's Cargo.toml to version = \"1.2.3\", OR:\n[patch.crates-io]\nfoo = { git = \"https://example/foo-fork\" }","handlingStrategy":"validation","validationCode":"# Ensure replace target version == replaced version before committing:\n# edit the fork's Cargo.toml so `version = ` matches the left-hand side of [replace].\ngrep -n 'version' fork-crate/Cargo.toml","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep replacement fork versions synchronized with the originals.","Use `[patch]` when you need version flexibility in overrides.","Add a CI step that runs `cargo check` on any `[replace]`/`[patch]` change."],"tags":["replace","dependency-resolution","version-mismatch"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}