Skip to content

Commit 91796b1

Browse files
committed
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
2 parents 7dc84a2 + 38718ea commit 91796b1

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/cargo/ops/resolve.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ use crate::util::cache_lock::CacheLockMode;
7373
use crate::util::errors::CargoResult;
7474
use crate::util::CanonicalUrl;
7575
use anyhow::Context as _;
76-
use cargo_util_schemas::manifest::RustVersion;
7776
use std::collections::{HashMap, HashSet};
7877
use tracing::{debug, trace};
7978

@@ -304,7 +303,14 @@ pub fn resolve_with_previous<'gctx>(
304303
version_prefs.version_ordering(VersionOrdering::MinimumVersionsFirst)
305304
}
306305
if ws.resolve_honors_rust_version() {
307-
version_prefs.max_rust_version(ws.rust_version().cloned().map(RustVersion::into_partial));
306+
let rust_version = if let Some(ver) = ws.rust_version() {
307+
ver.clone().into_partial()
308+
} else {
309+
let rustc = ws.gctx().load_global_rustc(Some(ws))?;
310+
let rustc_version = rustc.version.clone().into();
311+
rustc_version
312+
};
313+
version_prefs.max_rust_version(Some(rust_version));
308314
}
309315

310316
let avoid_patch_ids = if register_patches {

tests/testsuite/rust_version.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,56 @@ fn dependency_rust_version_older_and_newer_than_package() {
370370
.run();
371371
}
372372

373+
#[cargo_test]
374+
fn resolve_with_rustc() {
375+
Package::new("bar", "1.5.0")
376+
.rust_version("1.0")
377+
.file("src/lib.rs", "fn other_stuff() {}")
378+
.publish();
379+
Package::new("bar", "1.6.0")
380+
.rust_version("1.2345")
381+
.file("src/lib.rs", "fn other_stuff() {}")
382+
.publish();
383+
384+
let p = project()
385+
.file(
386+
"Cargo.toml",
387+
r#"
388+
[package]
389+
name = "foo"
390+
version = "0.0.1"
391+
edition = "2015"
392+
authors = []
393+
[dependencies]
394+
bar = "1.0.0"
395+
"#,
396+
)
397+
.file("src/main.rs", "fn main(){}")
398+
.build();
399+
400+
p.cargo("generate-lockfile --ignore-rust-version")
401+
.arg("-Zmsrv-policy")
402+
.masquerade_as_nightly_cargo(&["msrv-policy"])
403+
.with_stderr(
404+
"\
405+
[UPDATING] `dummy-registry` index
406+
[LOCKING] 2 packages
407+
",
408+
)
409+
.run();
410+
p.cargo("generate-lockfile")
411+
.arg("-Zmsrv-policy")
412+
.masquerade_as_nightly_cargo(&["msrv-policy"])
413+
.with_stderr(
414+
"\
415+
[UPDATING] `dummy-registry` index
416+
[LOCKING] 2 packages
417+
[ADDING] bar v1.5.0 (latest: v1.6.0)
418+
",
419+
)
420+
.run();
421+
}
422+
373423
#[cargo_test]
374424
fn dependency_rust_version_backtracking() {
375425
Package::new("has-rust-version", "1.6.0")

0 commit comments

Comments
 (0)