Skip to content

Commit d756ff8

Browse files
committed
feat(add): Fallback to rustc when rust-version is unset
1 parent a30652f commit d756ff8

File tree

8 files changed

+58
-33
lines changed

8 files changed

+58
-33
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::str::FromStr;
1111

1212
use anyhow::Context as _;
1313
use cargo_util::paths;
14+
use cargo_util_schemas::core::PartialVersion;
1415
use cargo_util_schemas::manifest::RustVersion;
1516
use indexmap::IndexSet;
1617
use itertools::Itertools;
@@ -615,17 +616,36 @@ fn get_latest_dependency(
615616
})?;
616617

617618
if gctx.cli_unstable().msrv_policy && honor_rust_version {
618-
if let Some(req_msrv) = spec.rust_version() {
619-
let msrvs = possibilities
620-
.iter()
621-
.map(|s| (s, s.rust_version()))
622-
.collect::<Vec<_>>();
623-
624-
// Find the latest version of the dep which has a compatible rust-version. To
625-
// determine whether or not one rust-version is compatible with another, we
626-
// compare the lowest possible versions they could represent, and treat
627-
// candidates without a rust-version as compatible by default.
628-
let latest_msrv = latest_compatible(&msrvs, req_msrv).ok_or_else(|| {
619+
let req_msrv = spec
620+
.rust_version()
621+
.cloned()
622+
.map(CargoResult::Ok)
623+
.unwrap_or_else(|| {
624+
let rustc = gctx.load_global_rustc(None)?;
625+
626+
// Remove any pre-release identifiers for easier comparison
627+
let current_version = &rustc.version;
628+
let untagged_version = RustVersion::try_from(PartialVersion {
629+
major: current_version.major,
630+
minor: Some(current_version.minor),
631+
patch: Some(current_version.patch),
632+
pre: None,
633+
build: None,
634+
})
635+
.unwrap();
636+
Ok(untagged_version)
637+
})?;
638+
639+
let msrvs = possibilities
640+
.iter()
641+
.map(|s| (s, s.rust_version()))
642+
.collect::<Vec<_>>();
643+
644+
// Find the latest version of the dep which has a compatible rust-version. To
645+
// determine whether or not one rust-version is compatible with another, we
646+
// compare the lowest possible versions they could represent, and treat
647+
// candidates without a rust-version as compatible by default.
648+
let latest_msrv = latest_compatible(&msrvs, &req_msrv).ok_or_else(|| {
629649
let name = spec.name();
630650
let dep_name = &dependency.name;
631651
let latest_version = latest.version();
@@ -639,17 +659,16 @@ help: pass `--ignore-rust-version` to select {dep_name}@{latest_version} which r
639659
)
640660
})?;
641661

642-
if latest_msrv.version() < latest.version() {
643-
let latest_version = latest.version();
644-
let latest_rust_version = latest.rust_version().unwrap();
645-
let name = spec.name();
646-
gctx.shell().warn(format_args!(
662+
if latest_msrv.version() < latest.version() {
663+
let latest_version = latest.version();
664+
let latest_rust_version = latest.rust_version().unwrap();
665+
let name = spec.name();
666+
gctx.shell().warn(format_args!(
647667
"\
648668
ignoring {dependency}@{latest_version} (which requires rustc {latest_rust_version}) to maintain {name}'s rust-version of {req_msrv}",
649669
))?;
650670

651-
latest = latest_msrv;
652-
}
671+
latest = latest_msrv;
653672
}
654673
}
655674

tests/testsuite/cargo_add/rustc_incompatible/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn case() {
2323
.current_dir(cwd)
2424
.masquerade_as_nightly_cargo(&["msrv-policy"])
2525
.assert()
26-
.success()
26+
.failure()
2727
.stdout_matches(str![""])
2828
.stderr_matches(file!["stderr.term.svg"]);
2929

tests/testsuite/cargo_add/rustc_incompatible/out/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
name = "cargo-list-test-fixture"
55
version = "0.0.0"
66
edition = "2015"
7-
8-
[dependencies]
9-
rust-version-user = "0.2.1"

tests/testsuite/cargo_add/rustc_incompatible/stderr.term.svg

Lines changed: 6 additions & 3 deletions
Loading

tests/testsuite/cargo_add/rustc_latest/out/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ version = "0.0.0"
66
edition = "2015"
77

88
[dependencies]
9-
rust-version-user = "0.2.1"
9+
rust-version-user = "0.1.1"

tests/testsuite/cargo_add/rustc_latest/stderr.term.svg

Lines changed: 6 additions & 3 deletions
Loading

tests/testsuite/cargo_add/rustc_older/out/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ version = "0.0.0"
66
edition = "2015"
77

88
[dependencies]
9-
rust-version-user = "0.2.1"
9+
rust-version-user = "0.1.1"

tests/testsuite/cargo_add/rustc_older/stderr.term.svg

Lines changed: 6 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)