Skip to content

Commit

Permalink
Auto merge of #13514 - epage:msrv-error, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Mar 1, 2024
2 parents 0342c44 + 90a681d commit db609a5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 36 deletions.
59 changes: 33 additions & 26 deletions src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ pub fn create_bcx<'a, 'gctx>(
current_version.patch,
);

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

let guidance = if ws.is_ephemeral() {
local_incompatible |= unit.is_local();
incompatible.push((unit, version));
}
if !incompatible.is_empty() {
use std::fmt::Write as _;

let plural = if incompatible.len() == 1 { "" } else { "s" };
let mut message = format!(
"rustc {current_version} is not supported by the following package{plural}:\n"
);
incompatible.sort_by_key(|(unit, _)| (unit.pkg.name(), unit.pkg.version()));
for (unit, msrv) in incompatible {
let name = &unit.pkg.name();
let version = &unit.pkg.version();
writeln!(&mut message, " {name}@{version} requires rustc {msrv}").unwrap();
}
if ws.is_ephemeral() {
if ws.ignore_lock() {
"Try re-running cargo install with `--locked`".to_string()
} else {
String::new()
writeln!(
&mut message,
"Try re-running `cargo install` with `--locked`"
)
.unwrap();
}
} else if !unit.is_local() {
format!(
"Either upgrade to rustc {} or newer, or use\n\
cargo update {}@{} --precise ver\n\
where `ver` is the latest version of `{}` supporting rustc {}",
version,
unit.pkg.name(),
unit.pkg.version(),
unit.pkg.name(),
current_version,
} else if !local_incompatible {
writeln!(
&mut message,
"Either upgrade rustc or select compatible dependency versions with
`cargo update <name>@<current-ver> --precise <compatible-ver>`
where `<compatible-ver>` is the latest version supporting rustc {current_version}",
)
} else {
String::new()
};

anyhow::bail!(
"package `{}` cannot be built because it requires rustc {} or newer, \
while the currently active rustc version is {}\n{}",
unit.pkg,
version,
current_version,
guidance,
);
.unwrap();
}
return Err(anyhow::Error::msg(message));
}
}

Expand Down
75 changes: 65 additions & 10 deletions tests/testsuite/rust_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ fn rust_version_too_high() {
p.cargo("check")
.with_status(101)
.with_stderr(
"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 [..]\n\n",
"\
[ERROR] rustc [..] is not supported by the following package:
foo@0.0.1 requires rustc 1.9876.0
",
)
.run();
p.cargo("check --ignore-rust-version").run();
Expand Down Expand Up @@ -212,14 +215,66 @@ fn dependency_rust_version_newer_than_rustc() {
p.cargo("check")
.with_status(101)
.with_stderr(
" Updating `[..]` index\n \
Downloading crates ...\n \
Downloaded bar v0.0.1 (registry `[..]`)\n\
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 [..]\n\
Either upgrade to rustc 1.2345.0 or newer, or use\n\
cargo update bar@0.0.1 --precise ver\n\
where `ver` is the latest version of `bar` supporting rustc [..]",
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] bar v0.0.1 (registry `[..]`)
[ERROR] rustc [..] is not supported by the following package:
bar@0.0.1 requires rustc 1.2345.0
Either upgrade rustc or select compatible dependency versions with
`cargo update <name>@<current-ver> --precise <compatible-ver>`
where `<compatible-ver>` is the latest version supporting rustc [..]
",
)
.run();
p.cargo("check --ignore-rust-version").run();
}

#[cargo_test]
fn dependency_tree_rust_version_newer_than_rustc() {
Package::new("baz", "0.0.1")
.dep("bar", "0.0.1")
.rust_version("1.2345.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();
Package::new("bar", "0.0.1")
.rust_version("1.2345.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []
[dependencies]
baz = "0.0.1"
"#,
)
.file("src/main.rs", "fn main(){}")
.build();

p.cargo("check")
.with_status(101)
.with_stderr(
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] baz v0.0.1 (registry `[..]`)
[DOWNLOADED] bar v0.0.1 (registry `[..]`)
[ERROR] rustc [..] is not supported by the following packages:
bar@0.0.1 requires rustc 1.2345.0
baz@0.0.1 requires rustc 1.2345.0
Either upgrade rustc or select compatible dependency versions with
`cargo update <name>@<current-ver> --precise <compatible-ver>`
where `<compatible-ver>` is the latest version supporting rustc [..]
",
)
.run();
p.cargo("check --ignore-rust-version").run();
Expand Down

0 comments on commit db609a5

Please sign in to comment.