rust-lang/cargo · error · anyhow::Error
patch `{}` version mismatch note: patch location contains {}
Error message
patch `{}` version mismatch
note: patch location contains {}, but patch definition requires `{}`
help: check patch location `{}`
help: check `{}` patch definition for `{}` in `{}` What it means
Thrown inside `summary_for_patch` when a `[patch]` entry in Cargo.toml resolves to a package that exists at the patch location by name, but none of the versions found there satisfy the version requirement stated in the patch definition. Cargo first queries the patch source by name-only (src/workspace/registry.rs:1008), collects candidate versions, sorts them, and — finding at least one but none matching `version_req()` — constructs this 'version mismatch' anyhow error naming the package, the versions actually present, and the requirement that failed.
Source
Thrown at src/workspace/registry.rs:1051
1 => format!("version `{}`", vers[0]),
_ => {
vers.sort();
let strs: Vec<_> = vers.into_iter().map(|v| v.to_string()).collect();
format!("versions `{}`", strs.join(", "))
}
};
Err(if found.is_empty() {
anyhow::anyhow!(
"patch location `{}` does not contain packages matching `{}`\n\
help: check `{}` patch definition for `{}` in `{}`",
&original_patch.dep.source_id(),
&original_patch.dep.package_name(),
&original_patch.dep.package_name(),
orig_patch_url,
original_patch.loc
)
} else {
anyhow::anyhow!(
"patch `{}` version mismatch\n\
note: patch location contains {}, but patch definition requires `{}`\n\
help: check patch location `{}`\n\
help: check `{}` patch definition for `{}` in `{}`",
&original_patch.dep.package_name(),
found,
&original_patch.dep.version_req(),
&original_patch.dep.source_id(),
&original_patch.dep.package_name(),
orig_patch_url,
original_patch.loc
)
})
}
View on GitHub (pinned to 0e07a15537)
Solutions
- Open the patch location shown in the error and read its Cargo.toml `version` field, then set the patch table's `version =` to a requirement that matches it (e.g. exact `"1.2.3"`).
- If you want the patch source's version to win, remove the `version` constraint from the patch entry entirely so any version is accepted.
- Update or checkout the patched fork to a commit whose version satisfies the stated requirement, then rebuild.
- If multiple versions exist at the location, qualify the requirement to pick the intended one (e.g. `version = ">=1.2, <1.3"`).
Example fix
// before — patch requires 2.x but the fork is 1.x
[patch.crates-io]
serde = { path = "../serde-fork", version = "2.0" }
// after — match the fork's actual version
[patch.crates-io]
serde = { path = "../serde-fork", version = "1.0" }
// or drop the requirement to accept whatever the fork ships
[patch.crates-io]
serde = { path = "../serde-fork" } Defensive patterns
Strategy: validation
Validate before calling
// Before defining a patch, confirm the patch source's version satisfies your requirement.
use semver::VersionReq;
fn patch_version_ok(req: &str, actual: &semver::Version) -> bool {
VersionReq::parse(req).map(|r| r.matches(actual)).unwrap_or(false)
}
// read ../fork/Cargo.toml version, then assert patch_version_ok("^1.0", &actual) Prevention
- Keep patch `version =` constraints in sync with the patched fork's Cargo.toml.
- Prefer omitting `version` in patch entries when you always want the fork's version.
- Run `cargo update` after changing a patch source to surface mismatches early.
When it happens
Trigger: A `[patch.crates-io]` table points a dependency at a path/git/registry source where the crate exists but at a different version than the `version = "..."` field requires. Concretely: `summary_for_patch` is called with an `original_patch` whose `dep.version_req()` does not intersect any `IndexSummary::Candidate` version returned by `source.query_vec(&name_only_dep, QueryKind::Exact)`.
Common situations: Vendoring a crate via `[patch]` and pinning `version = "2.0"` while the vendored checkout is actually 1.x; bumping a dependency's version in the patch table but forgetting to update the patched fork; pointing a patch at a git branch whose Cargo.toml version diverged from the requirement; using `cargo vendor` output whose versions drifted from the lockfile.
Related errors
- dependency `{}` in package `{}` requires a `{}` artifact to
- single file packages cannot be used as dependencies
- failed to parse the version requirement `{}` for dependency
- package `{}` cannot be tested because it requires dev-depend
- could not compile due to {error_count} previous target resol
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/7ace46dd933665e3.json.
Report an issue: GitHub.