Skip to content

Commit c68c636

Browse files
committed
fix(install): Suggest an alternative version on MSRV failure
The next step would be to also automatically install an MSRV compatible version if compatible with the version req (#10903). This improved error message will still be useful if the MSRV compatible version is outside of the version req. I did this as the first step - Helps people now, not needing to wait on `-Zmsrv-policy` to be stabilized - Has fewer questions on how it should be done (or if it should be)
1 parent cc2d88c commit c68c636

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/cargo/ops/common_for_install_and_uninstall.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,45 @@ where
559559
if !msrv_req.matches(current) {
560560
let name = summary.name();
561561
let ver = summary.version();
562+
let extra = if dep.source_id().is_registry() {
563+
// Match any version, not just the selected
564+
let msrv_dep =
565+
Dependency::parse(dep.package_name(), None, dep.source_id())?;
566+
let msrv_deps = loop {
567+
match source.query_vec(&msrv_dep, QueryKind::Exact)? {
568+
Poll::Ready(deps) => break deps,
569+
Poll::Pending => source.block_until_ready()?,
570+
}
571+
};
572+
if let Some(alt) = msrv_deps
573+
.iter()
574+
.filter(|summary| {
575+
summary
576+
.rust_version()
577+
.map(|msrv| msrv.caret_req().matches(current))
578+
.unwrap_or(true)
579+
})
580+
.max_by_key(|s| s.package_id())
581+
{
582+
if let Some(rust_version) = alt.rust_version() {
583+
format!(
584+
"\n`{name} {}` supports rustc {rust_version}",
585+
alt.version()
586+
)
587+
} else {
588+
format!(
589+
"\n`{name} {}` has an unspecified minimum rustc version",
590+
alt.version()
591+
)
592+
}
593+
} else {
594+
String::new()
595+
}
596+
} else {
597+
String::new()
598+
};
562599
bail!("\
563-
cannot install package `{name} {ver}`, it requires rustc {msrv} or newer, while the currently active rustc version is {current}"
600+
cannot install package `{name} {ver}`, it requires rustc {msrv} or newer, while the currently active rustc version is {current}{extra}"
564601
)
565602
}
566603
}

tests/testsuite/install.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,7 @@ fn install_incompat_msrv() {
24792479
.with_stderr("\
24802480
[UPDATING] `dummy-registry` index
24812481
[ERROR] cannot install package `foo 0.2.0`, it requires rustc 1.9876.0 or newer, while the currently active rustc version is [..]
2482+
`foo 0.1.0` supports rustc 1.30
24822483
")
24832484
.with_status(101).run();
24842485
}

0 commit comments

Comments
 (0)