From 458d607da1bcf635d520a02d0f8a4a7ba71c658d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 19 Aug 2019 09:06:56 -0700 Subject: [PATCH] Fix old lockfile encoding wrt newlines This commit fixes "old lockfile" encodings back to what they were prior to #7070 with respect to newlines. The changes in #7070 accidentally introduced a change where old lockfiles might have some extraneous newlines removed unintentionally. In #7070 it was attempted to clean up how newlines are emitted to ensure a trailing blank newline never shows up at the end of the file, but this acccidentally regressed cases where today we actually do have blank newlines at the end of lock files. This commit instead restores all the previous newline handling, and then scopes the "remove trailing newlines" fix to specifically just the new encoding. Closes #7254 --- src/cargo/core/mod.rs | 2 +- src/cargo/ops/lockfile.rs | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/cargo/core/mod.rs b/src/cargo/core/mod.rs index 9e285b6c266..b73c546493b 100644 --- a/src/cargo/core/mod.rs +++ b/src/cargo/core/mod.rs @@ -10,7 +10,7 @@ pub use self::package::{Package, PackageSet}; pub use self::package_id::PackageId; pub use self::package_id_spec::PackageIdSpec; pub use self::registry::Registry; -pub use self::resolver::Resolve; +pub use self::resolver::{Resolve, ResolveVersion}; pub use self::shell::{Shell, Verbosity}; pub use self::source::{GitReference, Source, SourceId, SourceMap}; pub use self::summary::{FeatureMap, FeatureValue, Summary}; diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index c0a27a9965b..4c7c03c1d18 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -2,7 +2,7 @@ use std::io::prelude::*; use toml; -use crate::core::{resolver, Resolve, Workspace}; +use crate::core::{resolver, Resolve, ResolveVersion, Workspace}; use crate::util::errors::{CargoResult, CargoResultExt}; use crate::util::toml as cargo_toml; use crate::util::Filesystem; @@ -122,10 +122,7 @@ fn resolve_to_string_orig( } let deps = toml["package"].as_array().unwrap(); - for (i, dep) in deps.iter().enumerate() { - if i > 0 { - out.push_str("\n"); - } + for dep in deps { let dep = dep.as_table().unwrap(); out.push_str("[[package]]\n"); @@ -135,17 +132,31 @@ fn resolve_to_string_orig( if let Some(patch) = toml.get("patch") { let list = patch["unused"].as_array().unwrap(); for entry in list { - out.push_str("\n[[patch.unused]]\n"); + out.push_str("[[patch.unused]]\n"); emit_package(entry.as_table().unwrap(), &mut out); + out.push_str("\n"); } } if let Some(meta) = toml.get("metadata") { - out.push_str("\n"); out.push_str("[metadata]\n"); out.push_str(&meta.to_string()); } + // Historical versions of Cargo in the old format accidentally left trailing + // blank newlines at the end of files, so we just leave that as-is. For all + // encodings going forward, though, we want to be sure that our encoded lock + // file doesn't contain any trailing newlines so trim out the extra if + // necessary. + match resolve.version() { + ResolveVersion::V1 => {} + _ => { + while out.ends_with("\n\n") { + out.pop(); + } + } + } + Ok((orig.ok(), out, ws_root)) } @@ -203,7 +214,8 @@ fn emit_package(dep: &toml::value::Table, out: &mut String) { out.push_str("]\n"); } + out.push_str("\n"); } else if dep.contains_key("replace") { - out.push_str(&format!("replace = {}\n", &dep["replace"])); + out.push_str(&format!("replace = {}\n\n", &dep["replace"])); } }