@@ -2,7 +2,7 @@ use std::io::prelude::*;
2
2
3
3
use toml;
4
4
5
- use crate :: core:: { resolver, Resolve , Workspace } ;
5
+ use crate :: core:: { resolver, Resolve , ResolveVersion , Workspace } ;
6
6
use crate :: util:: errors:: { CargoResult , CargoResultExt } ;
7
7
use crate :: util:: toml as cargo_toml;
8
8
use crate :: util:: Filesystem ;
@@ -122,10 +122,7 @@ fn resolve_to_string_orig(
122
122
}
123
123
124
124
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 {
129
126
let dep = dep. as_table ( ) . unwrap ( ) ;
130
127
131
128
out. push_str ( "[[package]]\n " ) ;
@@ -135,17 +132,31 @@ fn resolve_to_string_orig(
135
132
if let Some ( patch) = toml. get ( "patch" ) {
136
133
let list = patch[ "unused" ] . as_array ( ) . unwrap ( ) ;
137
134
for entry in list {
138
- out. push_str ( "\n [[patch.unused]]\n " ) ;
135
+ out. push_str ( "[[patch.unused]]\n " ) ;
139
136
emit_package ( entry. as_table ( ) . unwrap ( ) , & mut out) ;
137
+ out. push_str ( "\n " ) ;
140
138
}
141
139
}
142
140
143
141
if let Some ( meta) = toml. get ( "metadata" ) {
144
- out. push_str ( "\n " ) ;
145
142
out. push_str ( "[metadata]\n " ) ;
146
143
out. push_str ( & meta. to_string ( ) ) ;
147
144
}
148
145
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
+
149
160
Ok ( ( orig. ok ( ) , out, ws_root) )
150
161
}
151
162
@@ -203,7 +214,8 @@ fn emit_package(dep: &toml::value::Table, out: &mut String) {
203
214
204
215
out. push_str ( "]\n " ) ;
205
216
}
217
+ out. push_str ( "\n " ) ;
206
218
} 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" ] ) ) ;
208
220
}
209
221
}
0 commit comments