Skip to content

Commit

Permalink
feat: respect rust-version when generating lockfile
Browse files Browse the repository at this point in the history
MSRV-aware resolver has been implemented in PR 12560.
Based on that effort, generating lockfile can now respect `rust-version`
so that a package with an old MSRV will never get an incompatible
lockfile, even using the latest Cargo.
  • Loading branch information
weihanglo committed Jan 20, 2024
1 parent c4e5d33 commit db1b1f9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::time::Instant;
use cargo::core::dependency::DepKind;
use cargo::core::resolver::{self, ResolveOpts, VersionOrdering, VersionPreferences};
use cargo::core::Resolve;
use cargo::core::ResolveVersion;
use cargo::core::{Dependency, PackageId, Registry, Summary};
use cargo::core::{GitReference, SourceId};
use cargo::sources::source::QueryKind;
Expand Down Expand Up @@ -174,6 +175,7 @@ pub fn resolve_with_config_raw(
&[],
&mut registry,
&version_prefs,
ResolveVersion::default(),
Some(config),
);

Expand Down
5 changes: 4 additions & 1 deletion src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ mod version_prefs;
/// * `version_prefs` - this represents a preference for some versions over others,
/// based on the lock file or other reasons such as `[patch]`es.
///
/// * `resolve_version` - this controls how the lockfile will be serialized.
///
/// * `config` - a location to print warnings and such, or `None` if no warnings
/// should be printed
pub fn resolve(
summaries: &[(Summary, ResolveOpts)],
replacements: &[(PackageIdSpec, Dependency)],
registry: &mut dyn Registry,
version_prefs: &VersionPreferences,
resolve_version: ResolveVersion,
config: Option<&Config>,
) -> CargoResult<Resolve> {
let _p = profile::start("resolving");
Expand Down Expand Up @@ -169,7 +172,7 @@ pub fn resolve(
cksums,
BTreeMap::new(),
Vec::new(),
ResolveVersion::default(),
resolve_version,
summaries,
);

Expand Down
37 changes: 37 additions & 0 deletions src/cargo/core/resolver/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use cargo_util_schemas::core::PartialVersion;
use cargo_util_schemas::manifest::RustVersion;

use super::encode::Metadata;
use crate::core::dependency::DepKind;
use crate::core::{Dependency, PackageId, PackageIdSpec, PackageIdSpecQuery, Summary, Target};
Expand Down Expand Up @@ -97,6 +100,40 @@ impl ResolveVersion {
pub fn max_stable() -> ResolveVersion {
ResolveVersion::V3
}

/// Gets the default lockfile version for the given Rust version.
pub fn with_rust_version(rust_version: Option<&RustVersion>) -> Self {
let Some(rust_version) = rust_version else {
return ResolveVersion::default();
};

let rust_1_41 = PartialVersion {
major: 1,
minor: Some(41),
patch: None,
pre: None,
build: None,
}
.try_into()
.expect("PartialVersion 1.41");
let rust_1_53 = PartialVersion {
major: 1,
minor: Some(53),
patch: None,
pre: None,
build: None,
}
.try_into()
.expect("PartialVersion 1.53");

if rust_version >= &rust_1_53 {
ResolveVersion::V3
} else if rust_version >= &rust_1_41 {
ResolveVersion::V2
} else {
ResolveVersion::V1
}
}
}

impl Resolve {
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
// out lock file updates as they're otherwise already updated, and changes
// which don't touch dependencies won't seemingly spuriously update the lock
// file.
let default_version = ResolveVersion::default();
let default_version = ResolveVersion::with_rust_version(ws.rust_version());
let current_version = resolve.version();
let next_lockfile_bump = ws.config().cli_unstable().next_lockfile_bump;
tracing::debug!("lockfile - current: {current_version:?}, default: {default_version:?}");

if current_version < default_version {
resolve.set_version(default_version);
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ pub fn resolve_with_previous<'cfg>(
&replace,
registry,
&version_prefs,
ResolveVersion::with_rust_version(ws.rust_version()),
Some(ws.config()),
)?;
let patches: Vec<_> = registry
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,23 +1220,23 @@ dependencies = [

let cases = [
// v1 is the default
("1.37", None, 3),
("1.37", None, 1),
("1.37", Some(1), 1),
("1.37", Some(2), 2),
("1.37", Some(3), 3),
// v2 introduced
("1.38", None, 3),
("1.38", None, 1),
// last version of v1 as the default
("1.40", None, 3),
("1.40", None, 1),
// v2 is the default
("1.41", None, 3),
("1.41", None, 2),
("1.41", Some(1), 1),
("1.41", Some(2), 2),
("1.41", Some(3), 3),
// v3 introduced
("1.47", None, 3),
("1.47", None, 2),
// last version of v2 as the default
("1.48", None, 3),
("1.48", None, 2),
// v3 is the default
("1.53", None, 3),
("1.53", Some(1), 1),
Expand Down

0 comments on commit db1b1f9

Please sign in to comment.