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 Oct 20, 2023
1 parent 4ddacef commit 5e1f115
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub fn resolve(
Some(config) => config.cli_unstable().direct_minimal_versions,
None => false,
};

let resolve_version = ResolveVersion::with_rust_version(max_rust_version);

if !config
.map(|c| c.cli_unstable().msrv_policy)
.unwrap_or(false)
Expand Down Expand Up @@ -203,7 +206,7 @@ pub fn resolve(
cksums,
BTreeMap::new(),
Vec::new(),
ResolveVersion::default(),
resolve_version,
summaries,
);

Expand Down
16 changes: 16 additions & 0 deletions src/cargo/core/resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::core::{Dependency, PackageId, PackageIdSpec, Summary, Target};
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::Graph;
use crate::util::RustVersion;
use std::borrow::Borrow;
use std::collections::{HashMap, HashSet};
use std::fmt;
Expand Down Expand Up @@ -97,6 +98,21 @@ 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();
};

if rust_version >= &"1.53".parse::<RustVersion>().unwrap() {
ResolveVersion::V3
} else if rust_version >= &"1.41".parse::<RustVersion>().unwrap() {
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
8 changes: 4 additions & 4 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,22 +1220,22 @@ dependencies = [

let cases = [
// v1
("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),
("1.38", Some(1), 1),
("1.38", Some(2), 2),
("1.38", Some(3), 3),
// v2 became 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),
("1.47", Some(1), 1),
("1.47", Some(2), 2),
("1.47", Some(3), 3),
Expand Down

0 comments on commit 5e1f115

Please sign in to comment.