Skip to content

Commit 3bab084

Browse files
committed
Auto merge of #4687 - matklad:rust-1.22.0-backport, r=alexcrichton
[beta] Don't update lockfiles from previous Cargo versions if `--locked` is passed
2 parents c1dd25a + a4ea687 commit 3bab084

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
4040
Ok(s)
4141
});
4242

43-
let toml = toml::Value::try_from(WorkspaceResolve {
44-
ws: ws,
45-
resolve: resolve,
46-
}).unwrap();
43+
let toml = toml::Value::try_from(WorkspaceResolve { ws, resolve }).unwrap();
4744

4845
let mut out = String::new();
4946

@@ -72,10 +69,7 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
7269
// If the lockfile contents haven't changed so don't rewrite it. This is
7370
// helpful on read-only filesystems.
7471
if let Ok(orig) = orig {
75-
if has_crlf_line_endings(&orig) {
76-
out = out.replace("\n", "\r\n");
77-
}
78-
if out == orig {
72+
if are_equal_lockfiles(orig, &out, ws.config().lock_update_allowed()) {
7973
return Ok(())
8074
}
8175
}
@@ -97,6 +91,23 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
9791
})
9892
}
9993

94+
fn are_equal_lockfiles(mut orig: String, current: &str, lock_update_allowed: bool) -> bool {
95+
if has_crlf_line_endings(&orig) {
96+
orig = orig.replace("\r\n", "\n");
97+
}
98+
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+
_ => {}
106+
}
107+
}
108+
current == orig
109+
}
110+
100111
fn has_crlf_line_endings(s: &str) -> bool {
101112
// Only check the first line.
102113
if let Some(lf) = s.find('\n') {

tests/lockfile-compat.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
3737
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
3838
"#;
3939

40-
let old_lockfile = r#"
41-
[root]
40+
let old_lockfile =
41+
r#"[root]
4242
name = "bar"
4343
version = "0.0.1"
4444
dependencies = [
@@ -62,9 +62,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
6262
foo = "0.1.0"
6363
"#)
6464
.file("src/lib.rs", "")
65-
.file("Cargo.lock", old_lockfile);
66-
67-
let p = p.build();
65+
.file("Cargo.lock", old_lockfile)
66+
.build();
6867

6968
assert_that(p.cargo(cargo_command),
7069
execs().with_status(0));
@@ -77,6 +76,54 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
7776
assert_eq!(lock.lines().count(), expected_lockfile.lines().count());
7877
}
7978

79+
80+
#[test]
81+
fn frozen_flag_preserves_old_lockfile() {
82+
Package::new("foo", "0.1.0").publish();
83+
84+
let old_lockfile =
85+
r#"[root]
86+
name = "bar"
87+
version = "0.0.1"
88+
dependencies = [
89+
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
90+
]
91+
92+
[[package]]
93+
name = "foo"
94+
version = "0.1.0"
95+
source = "registry+https://github.com/rust-lang/crates.io-index"
96+
97+
[metadata]
98+
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f9e0a16bdf5c05435698fa27192d89e331b22a26a972c34984f560662544453b"
99+
"#;
100+
101+
let p = project("bar")
102+
.file("Cargo.toml", r#"
103+
[project]
104+
name = "bar"
105+
version = "0.0.1"
106+
authors = []
107+
108+
[dependencies]
109+
foo = "0.1.0"
110+
"#)
111+
.file("src/lib.rs", "")
112+
.file("Cargo.lock", old_lockfile)
113+
.build();
114+
115+
assert_that(p.cargo("build").arg("--locked"),
116+
execs().with_status(0));
117+
118+
let lock = p.read_lockfile();
119+
for (l, r) in old_lockfile.lines().zip(lock.lines()) {
120+
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
121+
}
122+
123+
assert_eq!(lock.lines().count(), old_lockfile.lines().count());
124+
}
125+
126+
80127
#[test]
81128
fn totally_wild_checksums_works() {
82129
Package::new("foo", "0.1.0").publish();

0 commit comments

Comments
 (0)