Skip to content

Commit

Permalink
Do not update semantically equivalent lockfiles with --frozen/--locked.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mark-Simulacrum committed Nov 12, 2017
1 parent 6463fc7 commit 89023dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod encode;
///
/// Each instance of `Resolve` also understands the full set of features used
/// for each package.
#[derive(PartialEq)]
pub struct Resolve {
graph: Graph<PackageId>,
replacements: HashMap<PackageId, PackageId>,
Expand Down
23 changes: 14 additions & 9 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
// If the lockfile contents haven't changed so don't rewrite it. This is
// helpful on read-only filesystems.
if let Ok(orig) = orig {
if are_equal_lockfiles(orig, &out, ws.config().lock_update_allowed()) {
if are_equal_lockfiles(orig, &out, ws) {
return Ok(())
}
}
Expand All @@ -91,20 +91,25 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
})
}

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

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

current == orig
}

Expand Down
20 changes: 10 additions & 10 deletions tests/lockfile-compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ fn oldest_lockfile_still_works_with_command(cargo_command: &str) {

let expected_lockfile =
r#"[[package]]
name = "bar"
name = "foo"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "zzz"
version = "0.0.1"
dependencies = [
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "foo"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
"#;

let old_lockfile =
r#"[root]
name = "bar"
name = "zzz"
version = "0.0.1"
dependencies = [
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
Expand All @@ -54,7 +54,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
let p = project("bar")
.file("Cargo.toml", r#"
[project]
name = "bar"
name = "zzz"
version = "0.0.1"
authors = []
Expand Down Expand Up @@ -83,7 +83,7 @@ fn frozen_flag_preserves_old_lockfile() {

let old_lockfile =
r#"[root]
name = "bar"
name = "zzz"
version = "0.0.1"
dependencies = [
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
Expand All @@ -101,7 +101,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
let p = project("bar")
.file("Cargo.toml", r#"
[project]
name = "bar"
name = "zzz"
version = "0.0.1"
authors = []
Expand Down

0 comments on commit 89023dc

Please sign in to comment.