Auto merge of #13743 - epage:msrv-implicit, r=weihanglo

feat(resolve): Fallback to 'rustc -V' for MSRV resolving

### What does this PR try to resolve?

This is part of #9930 and adds a fallback if the rust-version isn't set

### How should we test and review this PR?

Tests are added in a separate commit so people can see how the behavior changed.

### Additional information
This commit is contained in:
bors 2024-04-12 20:41:25 +00:00
commit 91796b1828
2 changed files with 58 additions and 2 deletions

View File

@ -73,7 +73,6 @@ use crate::util::cache_lock::CacheLockMode;
use crate::util::errors::CargoResult;
use crate::util::CanonicalUrl;
use anyhow::Context as _;
use cargo_util_schemas::manifest::RustVersion;
use std::collections::{HashMap, HashSet};
use tracing::{debug, trace};
@ -304,7 +303,14 @@ pub fn resolve_with_previous<'gctx>(
version_prefs.version_ordering(VersionOrdering::MinimumVersionsFirst)
}
if ws.resolve_honors_rust_version() {
version_prefs.max_rust_version(ws.rust_version().cloned().map(RustVersion::into_partial));
let rust_version = if let Some(ver) = ws.rust_version() {
ver.clone().into_partial()
} else {
let rustc = ws.gctx().load_global_rustc(Some(ws))?;
let rustc_version = rustc.version.clone().into();
rustc_version
};
version_prefs.max_rust_version(Some(rust_version));
}
let avoid_patch_ids = if register_patches {

View File

@ -370,6 +370,56 @@ fn dependency_rust_version_older_and_newer_than_package() {
.run();
}
#[cargo_test]
fn resolve_with_rustc() {
Package::new("bar", "1.5.0")
.rust_version("1.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();
Package::new("bar", "1.6.0")
.rust_version("1.2345")
.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]
bar = "1.0.0"
"#,
)
.file("src/main.rs", "fn main(){}")
.build();
p.cargo("generate-lockfile --ignore-rust-version")
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[LOCKING] 2 packages
",
)
.run();
p.cargo("generate-lockfile")
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[LOCKING] 2 packages
[ADDING] bar v1.5.0 (latest: v1.6.0)
",
)
.run();
}
#[cargo_test]
fn dependency_rust_version_backtracking() {
Package::new("has-rust-version", "1.6.0")