Skip to content

Commit cee38cd

Browse files
committed
Auto merge of #4716 - Mark-Simulacrum:frozen-freezes-beta, r=alexcrichton
[beta] Do not update semantically equivalent lockfiles with --frozen/--locked. A previous patch in #4684 attempted to fix this, but didn't work for the case where the [root] crate wasn't the first crate in the sorted package array. Backport of #4714.
2 parents a296afa + a96d7fd commit cee38cd

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/cargo/core/resolver/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ mod encode;
7373
///
7474
/// Each instance of `Resolve` also understands the full set of features used
7575
/// for each package.
76+
#[derive(PartialEq)]
7677
pub struct Resolve {
7778
graph: Graph<PackageId>,
7879
replacements: HashMap<PackageId, PackageId>,

src/cargo/ops/lockfile.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
6969
// If the lockfile contents haven't changed so don't rewrite it. This is
7070
// helpful on read-only filesystems.
7171
if let Ok(orig) = orig {
72-
if are_equal_lockfiles(orig, &out, ws.config().lock_update_allowed()) {
72+
if are_equal_lockfiles(orig, &out, ws) {
7373
return Ok(())
7474
}
7575
}
@@ -91,20 +91,25 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
9191
})
9292
}
9393

94-
fn are_equal_lockfiles(mut orig: String, current: &str, lock_update_allowed: bool) -> bool {
94+
fn are_equal_lockfiles(mut orig: String, current: &str, ws: &Workspace) -> bool {
9595
if has_crlf_line_endings(&orig) {
9696
orig = orig.replace("\r\n", "\n");
9797
}
9898

99-
// Old lockfiles have unused `[root]` section,
100-
// just ignore it if we are in the `--frozen` mode.
101-
if !lock_update_allowed && orig.starts_with("[root]") {
102-
orig = orig.replacen("[root]", "[[package]]", 1);
103-
match (orig.parse::<toml::Value>(), current.parse::<toml::Value>()) {
104-
(Ok(ref a), Ok(ref b)) if a == b => return true,
105-
_ => {}
99+
// If we want to try and avoid updating the lockfile, parse both and
100+
// compare them; since this is somewhat expensive, don't do it in the
101+
// common case where we can update lockfiles.
102+
if !ws.config().lock_update_allowed() {
103+
let res: CargoResult<bool> = (|| {
104+
let old: resolver::EncodableResolve = toml::from_str(&orig)?;
105+
let new: resolver::EncodableResolve = toml::from_str(current)?;
106+
Ok(old.into_resolve(ws)? == new.into_resolve(ws)?)
107+
})();
108+
if let Ok(true) = res {
109+
return true;
106110
}
107111
}
112+
108113
current == orig
109114
}
110115

tests/lockfile-compat.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
2222

2323
let expected_lockfile =
2424
r#"[[package]]
25-
name = "bar"
25+
name = "foo"
26+
version = "0.1.0"
27+
source = "registry+https://github.com/rust-lang/crates.io-index"
28+
29+
[[package]]
30+
name = "zzz"
2631
version = "0.0.1"
2732
dependencies = [
2833
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
2934
]
3035
31-
[[package]]
32-
name = "foo"
33-
version = "0.1.0"
34-
source = "registry+https://github.com/rust-lang/crates.io-index"
35-
3636
[metadata]
3737
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
3838
"#;
3939

4040
let old_lockfile =
4141
r#"[root]
42-
name = "bar"
42+
name = "zzz"
4343
version = "0.0.1"
4444
dependencies = [
4545
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -54,7 +54,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
5454
let p = project("bar")
5555
.file("Cargo.toml", r#"
5656
[project]
57-
name = "bar"
57+
name = "zzz"
5858
version = "0.0.1"
5959
authors = []
6060
@@ -83,7 +83,7 @@ fn frozen_flag_preserves_old_lockfile() {
8383

8484
let old_lockfile =
8585
r#"[root]
86-
name = "bar"
86+
name = "zzz"
8787
version = "0.0.1"
8888
dependencies = [
8989
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -101,7 +101,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
101101
let p = project("bar")
102102
.file("Cargo.toml", r#"
103103
[project]
104-
name = "bar"
104+
name = "zzz"
105105
version = "0.0.1"
106106
authors = []
107107

0 commit comments

Comments
 (0)