Auto merge of #13769 - epage:msrv-config, r=weihanglo

fix(msrv): Put MSRV-aware resolver behind a config

### What does this PR try to resolve?
This is a part of #13540 which is a party of #9930.

The config is `resolver.something-like-precedence` with values:
- `something-like-maximum` (default)
- `something-like-rust-version`

This is punting on the actual config schema so we can implement
`package.resolver` and `edition = "2024"` support as we want the
MSRV-aware resolver available without `cargo_features`.

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

One of the included test cases shows a bug with `cargo install`.  Resolving that will be tracked in #9930

### Additional information
This commit is contained in:
bors 2024-04-18 18:48:45 +00:00
commit 2956296659
12 changed files with 331 additions and 10 deletions

View File

@ -26,7 +26,10 @@ use crate::util::errors::{CargoResult, ManifestError};
use crate::util::interning::InternedString;
use crate::util::lints::check_implicit_features;
use crate::util::toml::{read_manifest, InheritableFields};
use crate::util::{context::ConfigRelativePath, Filesystem, GlobalContext, IntoUrl};
use crate::util::{
context::CargoResolverConfig, context::CargoResolverPrecedence, context::ConfigRelativePath,
Filesystem, GlobalContext, IntoUrl,
};
use cargo_util::paths;
use cargo_util::paths::normalize_path;
use cargo_util_schemas::manifest;
@ -100,6 +103,7 @@ pub struct Workspace<'gctx> {
/// The resolver behavior specified with the `resolver` field.
resolve_behavior: ResolveBehavior,
resolve_honors_rust_version: bool,
honor_rust_version: Option<bool>,
/// Workspace-level custom metadata
@ -207,7 +211,7 @@ impl<'gctx> Workspace<'gctx> {
.load_workspace_config()?
.and_then(|cfg| cfg.custom_metadata);
ws.find_members()?;
ws.set_resolve_behavior();
ws.set_resolve_behavior()?;
ws.validate()?;
Ok(ws)
}
@ -230,6 +234,7 @@ impl<'gctx> Workspace<'gctx> {
loaded_packages: RefCell::new(HashMap::new()),
ignore_lock: false,
resolve_behavior: ResolveBehavior::V1,
resolve_honors_rust_version: false,
honor_rust_version: None,
custom_metadata: None,
}
@ -248,7 +253,7 @@ impl<'gctx> Workspace<'gctx> {
.packages
.insert(root_path, MaybePackage::Virtual(manifest));
ws.find_members()?;
ws.set_resolve_behavior();
ws.set_resolve_behavior()?;
// TODO: validation does not work because it walks up the directory
// tree looking for the root which is a fake file that doesn't exist.
Ok(ws)
@ -284,11 +289,11 @@ impl<'gctx> Workspace<'gctx> {
ws.members.push(ws.current_manifest.clone());
ws.member_ids.insert(id);
ws.default_members.push(ws.current_manifest.clone());
ws.set_resolve_behavior();
ws.set_resolve_behavior()?;
Ok(ws)
}
fn set_resolve_behavior(&mut self) {
fn set_resolve_behavior(&mut self) -> CargoResult<()> {
// - If resolver is specified in the workspace definition, use that.
// - If the root package specifies the resolver, use that.
// - If the root package specifies edition 2021, use v2.
@ -299,7 +304,36 @@ impl<'gctx> Workspace<'gctx> {
.resolve_behavior()
.unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
};
match self.gctx().get::<CargoResolverConfig>("resolver") {
Ok(CargoResolverConfig {
something_like_precedence: Some(precedence),
}) => {
if self.gctx().cli_unstable().msrv_policy {
self.resolve_honors_rust_version =
precedence == CargoResolverPrecedence::SomethingLikeRustVersion;
} else {
self.gctx()
.shell()
.warn("ignoring `resolver` config table without `-Zmsrv-policy`")?;
}
}
Ok(CargoResolverConfig {
something_like_precedence: None,
}) => {}
Err(err) => {
if self.gctx().cli_unstable().msrv_policy {
return Err(err);
} else {
self.gctx()
.shell()
.warn("ignoring `resolver` config table without `-Zmsrv-policy`")?;
}
}
}
Ok(())
}
/// Returns the current package of this workspace.
@ -616,7 +650,9 @@ impl<'gctx> Workspace<'gctx> {
}
pub fn resolve_honors_rust_version(&self) -> bool {
self.gctx().cli_unstable().msrv_policy && self.honor_rust_version.unwrap_or(true)
// Give CLI precedence
self.honor_rust_version
.unwrap_or(self.resolve_honors_rust_version)
}
pub fn custom_metadata(&self) -> Option<&toml::Value> {

View File

@ -2644,6 +2644,19 @@ impl BuildTargetConfig {
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CargoResolverConfig {
pub something_like_precedence: Option<CargoResolverPrecedence>,
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum CargoResolverPrecedence {
SomethingLikeMaximum,
SomethingLikeRustVersion,
}
#[derive(Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct TermConfig {

View File

@ -336,15 +336,26 @@ This was stabilized in 1.79 in [#13608](https://github.com/rust-lang/cargo/pull/
### MSRV-aware resolver
By default, `-Zmsrv-policy` enables an MSRV-aware resolver.
`-Zmsrv-policy` allows access to an MSRV-aware resolver which can be enabled with:
- `resolver.something-like-precedence` config field
The resolver will prefer dependencies with a `package.rust-version` that is the same or older than your project's MSRV.
Your project's MSRV is determined by taking the lowest `package.rust-version` set among your workspace members.
If there is none set, your toolchain version will be used with the intent to pick up the version from rustup's `rust-toolchain.toml`, if present.
MSRV-incompatible dependencies can still be selected by:
#### `resolver.something-like-precedence`
* Type: string
* Default: "something-like-maximum"
* Environment: `CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE`
Select which policy should be used when resolving dependencies. Values include
- `something-like-maximum`: prefer highest compatible versions of a package
- `something-like-rust-version`: prefer versions of packages compatible with your project's Rust version
Can be overridden with
- `--ignore-rust-version` CLI option
- Setting the dependency's version requirement too high
- Specifying the version to `cargo update` with `--precise`
- Passing `--ignore-rust-version`
## precise-pre-release

View File

@ -25,6 +25,10 @@ fn case() {
.arg("--ignore-rust-version")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.code(0)

View File

@ -27,6 +27,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.failure()

View File

@ -24,6 +24,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.success()

View File

@ -24,6 +24,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.success()

View File

@ -28,6 +28,10 @@ fn case() {
.arg("--ignore-rust-version")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.code(0)

View File

@ -21,6 +21,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.failure()

View File

@ -27,6 +27,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.success()

View File

@ -27,6 +27,10 @@ fn case() {
.arg("add")
.arg_line("rust-version-user")
.current_dir(cwd)
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.masquerade_as_nightly_cargo(&["msrv-policy"])
.assert()
.success()

View File

@ -1,6 +1,6 @@
//! Tests for targets with `rust-version`.
use cargo_test_support::{project, registry::Package};
use cargo_test_support::{cargo_process, project, registry::Package};
#[cargo_test]
fn rust_version_satisfied() {
@ -213,6 +213,10 @@ fn resolve_with_rust_version() {
.build();
p.cargo("generate-lockfile --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -233,6 +237,10 @@ foo v0.0.1 ([CWD])
.run();
p.cargo("generate-lockfile")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -289,6 +297,10 @@ fn resolve_with_rustc() {
.build();
p.cargo("generate-lockfile --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -309,6 +321,10 @@ foo v0.0.1 ([CWD])
.run();
p.cargo("generate-lockfile")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -363,6 +379,10 @@ fn resolve_with_backtracking() {
.build();
p.cargo("generate-lockfile --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -384,6 +404,10 @@ foo v0.0.1 ([CWD])
// Ideally we'd pick `has-rust-version` 1.6.0 which requires backtracking
p.cargo("generate-lockfile")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -462,6 +486,10 @@ fn resolve_with_multiple_rust_versions() {
.build();
p.cargo("generate-lockfile --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -482,6 +510,10 @@ higher v0.0.1 ([CWD])
.run();
p.cargo("generate-lockfile")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -503,6 +535,84 @@ higher v0.0.1 ([CWD])
.run();
}
#[cargo_test]
fn resolve_unstable_config_on_stable() {
Package::new("only-newer", "1.6.0")
.rust_version("1.65.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();
Package::new("newer-and-older", "1.5.0")
.rust_version("1.55.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();
Package::new("newer-and-older", "1.6.0")
.rust_version("1.65.0")
.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 = []
rust-version = "1.60.0"
[dependencies]
only-newer = "1.0.0"
newer-and-older = "1.0.0"
"#,
)
.file("src/main.rs", "fn main(){}")
.build();
p.cargo("generate-lockfile")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.with_stderr(
"\
[WARNING] ignoring `resolver` config table without `-Zmsrv-policy`
[UPDATING] `dummy-registry` index
[LOCKING] 3 packages to latest compatible versions
",
)
.run();
p.cargo("tree")
.with_stdout(
"\
foo v0.0.1 ([CWD])
newer-and-older v1.6.0
only-newer v1.6.0
",
)
.run();
p.cargo("generate-lockfile")
.env("CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE", "non-existent")
.with_stderr(
"\
[WARNING] ignoring `resolver` config table without `-Zmsrv-policy`
[UPDATING] `dummy-registry` index
[LOCKING] 3 packages to latest compatible versions
",
)
.run();
p.cargo("tree")
.with_stdout(
"\
foo v0.0.1 ([CWD])
newer-and-older v1.6.0
only-newer v1.6.0
",
)
.run();
}
#[cargo_test]
fn generate_lockfile_ignore_rust_version_is_unstable() {
Package::new("bar", "1.5.0")
@ -572,6 +682,10 @@ fn update_msrv_resolve() {
.build();
p.cargo("update")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -593,6 +707,10 @@ See https://github.com/rust-lang/cargo/issues/9930 for more information about th
)
.run();
p.cargo("update --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -605,6 +723,65 @@ See https://github.com/rust-lang/cargo/issues/9930 for more information about th
.run();
}
#[cargo_test]
fn update_precise_overrides_msrv_resolver() {
Package::new("bar", "1.5.0")
.rust_version("1.55.0")
.file("src/lib.rs", "fn other_stuff() {}")
.publish();
Package::new("bar", "1.6.0")
.rust_version("1.65.0")
.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 = []
rust-version = "1.60.0"
[dependencies]
bar = "1.0.0"
"#,
)
.file("src/main.rs", "fn main(){}")
.build();
p.cargo("update")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[LOCKING] 2 packages to latest Rust 1.60.0 compatible versions
[ADDING] bar v1.5.0 (latest: v1.6.0)
",
)
.run();
p.cargo("update --precise 1.6.0 bar")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[UPDATING] bar v1.5.0 -> v1.6.0
",
)
.run();
}
#[cargo_test]
fn check_msrv_resolve() {
Package::new("only-newer", "1.6.0")
@ -640,6 +817,10 @@ fn check_msrv_resolve() {
.build();
p.cargo("check --ignore-rust-version")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -668,6 +849,10 @@ foo v0.0.1 ([CWD])
std::fs::remove_file(p.root().join("Cargo.lock")).unwrap();
p.cargo("check")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
@ -693,3 +878,47 @@ foo v0.0.1 ([CWD])
)
.run();
}
#[cargo_test]
fn cargo_install_ignores_msrv_config() {
Package::new("dep", "1.0.0")
.rust_version("1.50")
.file("src/lib.rs", "fn hello() {}")
.publish();
Package::new("dep", "1.1.0")
.rust_version("1.70")
.file("src/lib.rs", "fn hello() {}")
.publish();
Package::new("foo", "0.0.1")
.rust_version("1.60")
.file("src/main.rs", "fn main() {}")
.dep("dep", "1")
.publish();
cargo_process("install foo")
.env(
"CARGO_RESOLVER_SOMETHING_LIKE_PRECEDENCE",
"something-like-rust-version",
)
.arg("-Zmsrv-policy")
.masquerade_as_nightly_cargo(&["msrv-policy"])
.with_stderr(
"\
[UPDATING] `[..]` index
[DOWNLOADING] crates ...
[DOWNLOADED] foo v0.0.1 (registry [..])
[INSTALLING] foo v0.0.1
[LOCKING] 2 packages to latest Rust 1.60 compatible versions
[ADDING] dep v1.0.0 (latest: v1.1.0)
[DOWNLOADING] crates ...
[DOWNLOADED] dep v1.0.0 (registry [..])
[COMPILING] dep v1.0.0
[COMPILING] foo v0.0.1
[FINISHED] `release` profile [optimized] target(s) in [..]
[INSTALLING] [CWD]/home/.cargo/bin/foo[EXE]
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries
",
)
.run();
}