Skip to content

Commit

Permalink
Auto merge of #7262 - alexcrichton:fix-line-endings, r=ehuss
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Aug 20, 2019
2 parents 027ab62 + b850342 commit be431f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
28 changes: 20 additions & 8 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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))
}

Expand Down Expand Up @@ -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"]));
}
}

0 comments on commit be431f4

Please sign in to comment.