Skip to content

Commit db609a5

Browse files
committed
Auto merge of #13514 - epage:msrv-error, r=weihanglo
fix(msrv): Report all incompatible packages, not just a random one ### What does this PR try to resolve? In #9930, it recommended improving the error for incompatible packages so people can better get to the root of the problem. For example, you might get an error about `clap_lex` but resolving the error for the higher level `clap` could make the problem with `clap_lex` go away. Because I generally saw earlier packages in the graph reported, I assumed we were reporting these errors bottom up. It turns out, we are reporting them in the `UnitGraph`s order, which is non-deterministic because it is built on a `HashMap`. So this adds determinism and shows all incompatible dependencies (not just the bottom or the root). ### How should we test and review this PR? This is a first step. We might find that we still want to only include the shallowest units, rather than all. At that point, we can add the complexity to address this by walking the unit graph. We could also further improve this by querying the index to suggest compatible versions of packages. The multi-package test wasn't split out into its own commit because hat would have required fixing the non-determinism one way to then just revert it when we fix it this way. ### Additional information
2 parents 0342c44 + 90a681d commit db609a5

File tree

2 files changed

+98
-36
lines changed

2 files changed

+98
-36
lines changed

src/cargo/ops/cargo_compile/mod.rs

+33-26
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ pub fn create_bcx<'a, 'gctx>(
487487
current_version.patch,
488488
);
489489

490+
let mut incompatible = Vec::new();
491+
let mut local_incompatible = false;
490492
for unit in unit_graph.keys() {
491493
let Some(version) = unit.pkg.rust_version() else {
492494
continue;
@@ -497,35 +499,40 @@ pub fn create_bcx<'a, 'gctx>(
497499
continue;
498500
}
499501

500-
let guidance = if ws.is_ephemeral() {
502+
local_incompatible |= unit.is_local();
503+
incompatible.push((unit, version));
504+
}
505+
if !incompatible.is_empty() {
506+
use std::fmt::Write as _;
507+
508+
let plural = if incompatible.len() == 1 { "" } else { "s" };
509+
let mut message = format!(
510+
"rustc {current_version} is not supported by the following package{plural}:\n"
511+
);
512+
incompatible.sort_by_key(|(unit, _)| (unit.pkg.name(), unit.pkg.version()));
513+
for (unit, msrv) in incompatible {
514+
let name = &unit.pkg.name();
515+
let version = &unit.pkg.version();
516+
writeln!(&mut message, " {name}@{version} requires rustc {msrv}").unwrap();
517+
}
518+
if ws.is_ephemeral() {
501519
if ws.ignore_lock() {
502-
"Try re-running cargo install with `--locked`".to_string()
503-
} else {
504-
String::new()
520+
writeln!(
521+
&mut message,
522+
"Try re-running `cargo install` with `--locked`"
523+
)
524+
.unwrap();
505525
}
506-
} else if !unit.is_local() {
507-
format!(
508-
"Either upgrade to rustc {} or newer, or use\n\
509-
cargo update {}@{} --precise ver\n\
510-
where `ver` is the latest version of `{}` supporting rustc {}",
511-
version,
512-
unit.pkg.name(),
513-
unit.pkg.version(),
514-
unit.pkg.name(),
515-
current_version,
526+
} else if !local_incompatible {
527+
writeln!(
528+
&mut message,
529+
"Either upgrade rustc or select compatible dependency versions with
530+
`cargo update <name>@<current-ver> --precise <compatible-ver>`
531+
where `<compatible-ver>` is the latest version supporting rustc {current_version}",
516532
)
517-
} else {
518-
String::new()
519-
};
520-
521-
anyhow::bail!(
522-
"package `{}` cannot be built because it requires rustc {} or newer, \
523-
while the currently active rustc version is {}\n{}",
524-
unit.pkg,
525-
version,
526-
current_version,
527-
guidance,
528-
);
533+
.unwrap();
534+
}
535+
return Err(anyhow::Error::msg(message));
529536
}
530537
}
531538

tests/testsuite/rust_version.rs

+65-10
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ fn rust_version_too_high() {
179179
p.cargo("check")
180180
.with_status(101)
181181
.with_stderr(
182-
"error: package `foo v0.0.1 ([..])` cannot be built because it requires \
183-
rustc 1.9876.0 or newer, while the currently active rustc version is [..]\n\n",
182+
"\
183+
[ERROR] rustc [..] is not supported by the following package:
184+
foo@0.0.1 requires rustc 1.9876.0
185+
186+
",
184187
)
185188
.run();
186189
p.cargo("check --ignore-rust-version").run();
@@ -212,14 +215,66 @@ fn dependency_rust_version_newer_than_rustc() {
212215
p.cargo("check")
213216
.with_status(101)
214217
.with_stderr(
215-
" Updating `[..]` index\n \
216-
Downloading crates ...\n \
217-
Downloaded bar v0.0.1 (registry `[..]`)\n\
218-
error: package `bar v0.0.1` cannot be built because it requires \
219-
rustc 1.2345.0 or newer, while the currently active rustc version is [..]\n\
220-
Either upgrade to rustc 1.2345.0 or newer, or use\n\
221-
cargo update bar@0.0.1 --precise ver\n\
222-
where `ver` is the latest version of `bar` supporting rustc [..]",
218+
"\
219+
[UPDATING] `[..]` index
220+
[DOWNLOADING] crates ...
221+
[DOWNLOADED] bar v0.0.1 (registry `[..]`)
222+
[ERROR] rustc [..] is not supported by the following package:
223+
bar@0.0.1 requires rustc 1.2345.0
224+
Either upgrade rustc or select compatible dependency versions with
225+
`cargo update <name>@<current-ver> --precise <compatible-ver>`
226+
where `<compatible-ver>` is the latest version supporting rustc [..]
227+
228+
",
229+
)
230+
.run();
231+
p.cargo("check --ignore-rust-version").run();
232+
}
233+
234+
#[cargo_test]
235+
fn dependency_tree_rust_version_newer_than_rustc() {
236+
Package::new("baz", "0.0.1")
237+
.dep("bar", "0.0.1")
238+
.rust_version("1.2345.0")
239+
.file("src/lib.rs", "fn other_stuff() {}")
240+
.publish();
241+
Package::new("bar", "0.0.1")
242+
.rust_version("1.2345.0")
243+
.file("src/lib.rs", "fn other_stuff() {}")
244+
.publish();
245+
246+
let p = project()
247+
.file(
248+
"Cargo.toml",
249+
r#"
250+
[package]
251+
name = "foo"
252+
version = "0.0.1"
253+
edition = "2015"
254+
authors = []
255+
[dependencies]
256+
baz = "0.0.1"
257+
"#,
258+
)
259+
.file("src/main.rs", "fn main(){}")
260+
.build();
261+
262+
p.cargo("check")
263+
.with_status(101)
264+
.with_stderr(
265+
"\
266+
[UPDATING] `[..]` index
267+
[DOWNLOADING] crates ...
268+
[DOWNLOADED] baz v0.0.1 (registry `[..]`)
269+
[DOWNLOADED] bar v0.0.1 (registry `[..]`)
270+
[ERROR] rustc [..] is not supported by the following packages:
271+
bar@0.0.1 requires rustc 1.2345.0
272+
baz@0.0.1 requires rustc 1.2345.0
273+
Either upgrade rustc or select compatible dependency versions with
274+
`cargo update <name>@<current-ver> --precise <compatible-ver>`
275+
where `<compatible-ver>` is the latest version supporting rustc [..]
276+
277+
",
223278
)
224279
.run();
225280
p.cargo("check --ignore-rust-version").run();

0 commit comments

Comments
 (0)