Skip to content

Commit be431f4

Browse files
committed
Auto merge of #7262 - alexcrichton:fix-line-endings, r=ehuss
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
2 parents 027ab62 + b850342 commit be431f4

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/cargo/core/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use self::package::{Package, PackageSet};
1010
pub use self::package_id::PackageId;
1111
pub use self::package_id_spec::PackageIdSpec;
1212
pub use self::registry::Registry;
13-
pub use self::resolver::Resolve;
13+
pub use self::resolver::{Resolve, ResolveVersion};
1414
pub use self::shell::{Shell, Verbosity};
1515
pub use self::source::{GitReference, Source, SourceId, SourceMap};
1616
pub use self::summary::{FeatureMap, FeatureValue, Summary};

src/cargo/ops/lockfile.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::prelude::*;
22

33
use toml;
44

5-
use crate::core::{resolver, Resolve, Workspace};
5+
use crate::core::{resolver, Resolve, ResolveVersion, Workspace};
66
use crate::util::errors::{CargoResult, CargoResultExt};
77
use crate::util::toml as cargo_toml;
88
use crate::util::Filesystem;
@@ -122,10 +122,7 @@ fn resolve_to_string_orig(
122122
}
123123

124124
let deps = toml["package"].as_array().unwrap();
125-
for (i, dep) in deps.iter().enumerate() {
126-
if i > 0 {
127-
out.push_str("\n");
128-
}
125+
for dep in deps {
129126
let dep = dep.as_table().unwrap();
130127

131128
out.push_str("[[package]]\n");
@@ -135,17 +132,31 @@ fn resolve_to_string_orig(
135132
if let Some(patch) = toml.get("patch") {
136133
let list = patch["unused"].as_array().unwrap();
137134
for entry in list {
138-
out.push_str("\n[[patch.unused]]\n");
135+
out.push_str("[[patch.unused]]\n");
139136
emit_package(entry.as_table().unwrap(), &mut out);
137+
out.push_str("\n");
140138
}
141139
}
142140

143141
if let Some(meta) = toml.get("metadata") {
144-
out.push_str("\n");
145142
out.push_str("[metadata]\n");
146143
out.push_str(&meta.to_string());
147144
}
148145

146+
// Historical versions of Cargo in the old format accidentally left trailing
147+
// blank newlines at the end of files, so we just leave that as-is. For all
148+
// encodings going forward, though, we want to be sure that our encoded lock
149+
// file doesn't contain any trailing newlines so trim out the extra if
150+
// necessary.
151+
match resolve.version() {
152+
ResolveVersion::V1 => {}
153+
_ => {
154+
while out.ends_with("\n\n") {
155+
out.pop();
156+
}
157+
}
158+
}
159+
149160
Ok((orig.ok(), out, ws_root))
150161
}
151162

@@ -203,7 +214,8 @@ fn emit_package(dep: &toml::value::Table, out: &mut String) {
203214

204215
out.push_str("]\n");
205216
}
217+
out.push_str("\n");
206218
} else if dep.contains_key("replace") {
207-
out.push_str(&format!("replace = {}\n", &dep["replace"]));
219+
out.push_str(&format!("replace = {}\n\n", &dep["replace"]));
208220
}
209221
}

0 commit comments

Comments
 (0)