Skip to content

Commit 366ae3f

Browse files
committed
Turn the new lock file format on by default
This commit enables the support added in #7070 by default. This means that gradually over time all `Cargo.lock` files will be migrated to the new format. Cargo shipped with Rust 1.38.0 supports this new lock file format, so any project using Rust 1.38.0 or above will converge quickly onto the new lock file format and continue working. The main benefit of the new format is to be more friendly to git merge conflicts. Information is deduplicated throughout the lock file to avoid verbose `depedencies` lists and the `checksum` data is all listed inline with `[[package]]`. This has been deployed with rust-lang/rust for some time now and it subjectively at least seems to have greatly reduced the amount of bouncing that happens for touching `Cargo.lock`.
1 parent 8280633 commit 366ae3f

File tree

4 files changed

+68
-50
lines changed

4 files changed

+68
-50
lines changed

src/cargo/core/resolver/resolve.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use super::encode::Metadata;
1515
///
1616
/// Each instance of `Resolve` also understands the full set of features used
1717
/// for each package.
18-
#[derive(PartialEq)]
1918
pub struct Resolve {
2019
/// A graph, whose vertices are packages and edges are dependency specifications
2120
/// from `Cargo.toml`. We need a `Vec` here because the same package
@@ -358,6 +357,26 @@ unable to verify that `{0}` is the same as when the lockfile was generated
358357
}
359358
}
360359

360+
impl PartialEq for Resolve {
361+
fn eq(&self, other: &Resolve) -> bool {
362+
macro_rules! compare {
363+
($($fields:ident)* | $($ignored:ident)*) => {
364+
let Resolve { $($fields,)* $($ignored,)* } = self;
365+
$(drop($ignored);)*
366+
$($fields == &other.$fields)&&*
367+
}
368+
}
369+
compare! {
370+
// fields to compare
371+
graph replacements reverse_replacements empty_features features
372+
checksums metadata unused_patches public_dependencies
373+
|
374+
// fields to ignore
375+
version
376+
}
377+
}
378+
}
379+
361380
impl fmt::Debug for Resolve {
362381
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
363382
writeln!(fmt, "graph: {:?}", self.graph)?;
@@ -376,7 +395,7 @@ impl ResolveVersion {
376395
/// previous `Cargo.lock` files, and generally matches with what we want to
377396
/// encode.
378397
pub fn default() -> ResolveVersion {
379-
ResolveVersion::V1
398+
ResolveVersion::V2
380399
}
381400

382401
/// Returns whether this encoding version is "from the future".
@@ -385,7 +404,7 @@ impl ResolveVersion {
385404
/// intended to become the default "soon".
386405
pub fn from_the_future(&self) -> bool {
387406
match self {
388-
ResolveVersion::V2 => true,
407+
ResolveVersion::V2 => false,
389408
ResolveVersion::V1 => false,
390409
}
391410
}

tests/testsuite/lockfile_compat.rs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ fn oldest_lockfile_still_works() {
1010
}
1111
}
1212

13+
fn assert_lockfiles_eq(expected: &str, actual: &str) {
14+
for (l, r) in expected.lines().zip(actual.lines()) {
15+
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
16+
}
17+
18+
assert_eq!(expected.lines().count(), actual.lines().count());
19+
}
20+
1321
fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
1422
Package::new("bar", "0.1.0").publish();
1523

@@ -19,16 +27,14 @@ fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
1927
name = "bar"
2028
version = "0.1.0"
2129
source = "registry+https://github.com/rust-lang/crates.io-index"
30+
checksum = "[..]"
2231
2332
[[package]]
2433
name = "foo"
2534
version = "0.0.1"
2635
dependencies = [
27-
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
36+
"bar",
2837
]
29-
30-
[metadata]
31-
"checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
3238
"#;
3339

3440
let old_lockfile = r#"
@@ -65,11 +71,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
6571
p.cargo(cargo_command).run();
6672

6773
let lock = p.read_lockfile();
68-
for (l, r) in expected_lockfile.lines().zip(lock.lines()) {
69-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
70-
}
71-
72-
assert_eq!(lock.lines().count(), expected_lockfile.lines().count());
74+
assert_lockfiles_eq(expected_lockfile, &lock);
7375
}
7476

7577
#[cargo_test]
@@ -115,11 +117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
115117
p.cargo("build --locked").run();
116118

117119
let lock = p.read_lockfile();
118-
for (l, r) in old_lockfile.lines().zip(lock.lines()) {
119-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
120-
}
121-
122-
assert_eq!(lock.lines().count(), old_lockfile.lines().count());
120+
assert_lockfiles_eq(&old_lockfile, &lock);
123121
}
124122

125123
#[cargo_test]
@@ -166,26 +164,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
166164
p.cargo("build").run();
167165

168166
let lock = p.read_lockfile();
169-
assert!(lock.starts_with(
170-
r#"
171-
# This file is automatically @generated by Cargo.
167+
assert_lockfiles_eq(
168+
r#"# This file is automatically @generated by Cargo.
172169
# It is not intended for manual editing.
173170
[[package]]
174171
name = "bar"
175172
version = "0.1.0"
176173
source = "registry+https://github.com/rust-lang/crates.io-index"
174+
checksum = "[..]"
177175
178176
[[package]]
179177
name = "foo"
180178
version = "0.0.1"
181179
dependencies = [
182-
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
180+
"bar",
183181
]
184-
185-
[metadata]
186-
"#
187-
.trim()
188-
));
182+
"#,
183+
&lock,
184+
);
189185
}
190186

191187
#[cargo_test]
@@ -413,22 +409,16 @@ fn current_lockfile_format() {
413409
name = \"bar\"
414410
version = \"0.1.0\"
415411
source = \"registry+https://github.com/rust-lang/crates.io-index\"
412+
checksum = \"[..]\"
416413
417414
[[package]]
418415
name = \"foo\"
419416
version = \"0.0.1\"
420417
dependencies = [
421-
\"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)\",
418+
\"bar\",
422419
]
423-
424-
[metadata]
425-
\"checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)\" = \"[..]\"";
426-
427-
for (l, r) in expected.lines().zip(actual.lines()) {
428-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
429-
}
430-
431-
assert_eq!(actual.lines().count(), expected.lines().count());
420+
";
421+
assert_lockfiles_eq(expected, &actual);
432422
}
433423

434424
#[cargo_test]
@@ -447,7 +437,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
447437
name = "foo"
448438
version = "0.0.1"
449439
dependencies = [
450-
"bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
440+
"bar",
451441
]
452442
"#;
453443

@@ -472,7 +462,24 @@ dependencies = [
472462
p.cargo("build").run();
473463

474464
let lock = p.read_lockfile();
475-
assert!(lock.starts_with(lockfile.trim()));
465+
assert_lockfiles_eq(
466+
r#"# [..]
467+
# [..]
468+
[[package]]
469+
name = "bar"
470+
version = "0.1.0"
471+
source = "registry+https://github.com/rust-lang/crates.io-index"
472+
checksum = "[..]"
473+
474+
[[package]]
475+
name = "foo"
476+
version = "0.0.1"
477+
dependencies = [
478+
"bar",
479+
]
480+
"#,
481+
&lock,
482+
);
476483
}
477484

478485
#[cargo_test]
@@ -549,11 +556,7 @@ dependencies = [
549556
p.cargo("fetch").run();
550557

551558
let lock = p.read_lockfile();
552-
for (l, r) in lockfile.lines().zip(lock.lines()) {
553-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
554-
}
555-
556-
assert_eq!(lock.lines().count(), lockfile.lines().count());
559+
assert_lockfiles_eq(&lockfile, &lock);
557560
}
558561

559562
#[cargo_test]
@@ -624,9 +627,5 @@ dependencies = [
624627
p.cargo("fetch").run();
625628

626629
let lock = p.read_lockfile();
627-
for (l, r) in lockfile.lines().zip(lock.lines()) {
628-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
629-
}
630-
631-
assert_eq!(lock.lines().count(), lockfile.lines().count());
630+
assert_lockfiles_eq(&lockfile, &lock);
632631
}

tests/testsuite/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ fn publish_git_with_version() {
11711171
name = \"foo\"\n\
11721172
version = \"0.1.0\"\n\
11731173
dependencies = [\n\
1174-
\x20\"dep1 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)\",\n\
1174+
\x20\"dep1\",\n\
11751175
]\n\
11761176
[..]",
11771177
),

tests/testsuite/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ fn minimal_version_cli() {
3030

3131
let lock = p.read_lockfile();
3232

33-
assert!(lock.contains("dep 1.0.0"));
33+
assert!(!lock.contains("1.1.0"));
3434
}

0 commit comments

Comments
 (0)