Skip to content

Commit 72873d8

Browse files
committed
Auto merge of #10449 - Eh2406:locked_version, r=alexcrichton
Use locked_version more In #9847 we added better tracking for when a requirement came from a lockfile. This uses that tracking in a few more error messages. Closes #10391
2 parents 5f611af + 4164c46 commit 72873d8

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/cargo/core/resolver/errors.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ pub(super) fn activation_error(
9595

9696
msg.push_str("\nversions that meet the requirements `");
9797
msg.push_str(&dep.version_req().to_string());
98-
msg.push_str("` are: ");
98+
msg.push_str("` ");
99+
100+
if let Some(v) = dep.version_req().locked_version() {
101+
msg.push_str("(locked to ");
102+
msg.push_str(&v.to_string());
103+
msg.push_str(") ");
104+
}
105+
106+
msg.push_str("are: ");
99107
msg.push_str(
100108
&candidates
101109
.iter()
@@ -239,12 +247,19 @@ pub(super) fn activation_error(
239247
versions.join(", ")
240248
};
241249

250+
let locked_version = dep
251+
.version_req()
252+
.locked_version()
253+
.map(|v| format!(" (locked to {})", v))
254+
.unwrap_or_default();
255+
242256
let mut msg = format!(
243-
"failed to select a version for the requirement `{} = \"{}\"`\n\
257+
"failed to select a version for the requirement `{} = \"{}\"`{}\n\
244258
candidate versions found which didn't match: {}\n\
245259
location searched: {}\n",
246260
dep.package_name(),
247261
dep.version_req(),
262+
locked_version,
248263
versions,
249264
registry.describe_source(dep.source_id()),
250265
);
@@ -254,7 +269,7 @@ pub(super) fn activation_error(
254269
// If we have a path dependency with a locked version, then this may
255270
// indicate that we updated a sub-package and forgot to run `cargo
256271
// update`. In this case try to print a helpful error!
257-
if dep.source_id().is_path() && dep.version_req().to_string().starts_with('=') {
272+
if dep.source_id().is_path() && dep.version_req().is_locked() {
258273
msg.push_str(
259274
"\nconsider running `cargo update` to update \
260275
a path dependency's locked version",

tests/testsuite/lockfile_compat.rs

+52
Original file line numberDiff line numberDiff line change
@@ -836,3 +836,55 @@ source = "git+{url}#{sha}"
836836

837837
assert_eq!(p.read_file("Cargo.lock"), lockfile);
838838
}
839+
840+
#[cargo_test]
841+
fn bad_data_in_lockfile_error_meg() {
842+
Package::new("bar", "0.0.1").publish();
843+
844+
let p = project()
845+
.file(
846+
"Cargo.toml",
847+
r#"
848+
[package]
849+
name = "test"
850+
version = "0.0.0"
851+
852+
[dependencies]
853+
bar = "*"
854+
"#,
855+
)
856+
.file("src/main.rs", "fn main() {}")
857+
.file(
858+
"Cargo.lock",
859+
r#"# This file is automatically @generated by Cargo.
860+
# It is not intended for manual editing.
861+
version = 3
862+
863+
[[package]]
864+
name = "bar"
865+
version = "0.1.0"
866+
source = "registry+https://github.com/rust-lang/crates.io-index"
867+
checksum = "8e1b9346248cf3391ead604c4407258d327c28e37209f6d56127598165165dda"
868+
869+
[[package]]
870+
name = "test"
871+
version = "0.0.0"
872+
dependencies = [
873+
"bar",
874+
]"#,
875+
)
876+
.build();
877+
p.cargo("build")
878+
.with_status(101)
879+
.with_stderr(
880+
"\
881+
[..]
882+
[ERROR] failed to select a version for the requirement `bar = \"*\"` (locked to 0.1.0)
883+
candidate versions found which didn't match: 0.0.1
884+
location searched: `dummy-registry` index (which is replacing registry `crates-io`)
885+
required by package `test v0.0.0 ([..])`
886+
perhaps a crate was updated and forgotten to be re-vendored?
887+
",
888+
)
889+
.run();
890+
}

0 commit comments

Comments
 (0)