Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[beta] Do not update semantically equivalent lockfiles with --frozen/--locked. #4716

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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