Skip to content

Commit 90a681d

Browse files
committed
fix(msrv): Report all incompatible packages, not just a random one
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). 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.
1 parent 41449f7 commit 90a681d

File tree

2 files changed

+91
-31
lines changed

2 files changed

+91
-31
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

+58-5
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ fn rust_version_too_high() {
180180
.with_status(101)
181181
.with_stderr(
182182
"\
183-
[ERROR] package `foo v0.0.1 ([..])` cannot be built because it requires rustc 1.9876.0 or newer, while the currently active rustc version is [..]
183+
[ERROR] rustc [..] is not supported by the following package:
184+
foo@0.0.1 requires rustc 1.9876.0
184185
185186
",
186187
)
@@ -218,10 +219,62 @@ fn dependency_rust_version_newer_than_rustc() {
218219
[UPDATING] `[..]` index
219220
[DOWNLOADING] crates ...
220221
[DOWNLOADED] bar v0.0.1 (registry `[..]`)
221-
[ERROR] package `bar v0.0.1` cannot be built because it requires rustc 1.2345.0 or newer, while the currently active rustc version is [..]
222-
Either upgrade to rustc 1.2345.0 or newer, or use
223-
cargo update bar@0.0.1 --precise ver
224-
where `ver` is the latest version of `bar` supporting rustc [..]",
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+
",
225278
)
226279
.run();
227280
p.cargo("check --ignore-rust-version").run();

0 commit comments

Comments
 (0)