Skip to content

[beta] Fix old lockfile encoding wrt newlines #7274

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

Merged
merged 1 commit into from
Aug 20, 2019
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
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"]));
}
}