Skip to content

Commit

Permalink
Auto merge of #13743 - epage:msrv-implicit, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Apr 12, 2024
2 parents 7dc84a2 + 38718ea commit 91796b1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cargo/ops/resolve.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions tests/testsuite/rust_version.rs
Expand Up @@ -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")
Expand Down

0 comments on commit 91796b1

Please sign in to comment.