Skip to content

Commit 8522a88

Browse files
committed
Auto merge of #6181 - dwijnand:preserve-lockfile-top-comment, r=alexcrichton
Preserve lockfile top comment Refs #6180
2 parents cbde1c6 + 32fbfe3 commit 8522a88

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
4343

4444
let mut out = String::new();
4545

46+
// Preserve the top comments in the lockfile
47+
// This is in preparation for marking it as generated
48+
// https://github.com/rust-lang/cargo/issues/6180
49+
if let Ok(orig) = &orig {
50+
for line in orig.lines().take_while(|line| line.starts_with("#")) {
51+
out.push_str(line);
52+
out.push_str("\n");
53+
}
54+
}
55+
4656
let deps = toml["package"].as_array().unwrap();
4757
for dep in deps.iter() {
4858
let dep = dep.as_table().unwrap();

tests/testsuite/update.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,24 @@ fn update_precise() {
374374
",
375375
).run();
376376
}
377+
378+
#[test]
379+
fn preserve_top_comment() {
380+
let p = project().file("src/lib.rs", "").build();
381+
382+
p.cargo("update").run();
383+
384+
let mut lockfile = p.read_file("Cargo.lock");
385+
lockfile.insert_str(0, "# @generated\n");
386+
lockfile.insert_str(0, "# some other comment\n");
387+
println!("saving Cargo.lock contents:\n{}", lockfile);
388+
389+
p.change_file("Cargo.lock", &lockfile);
390+
391+
p.cargo("update").run();
392+
393+
let lockfile2 = p.read_file("Cargo.lock");
394+
println!("loaded Cargo.lock contents:\n{}", lockfile2);
395+
396+
assert!(lockfile == lockfile2);
397+
}

0 commit comments

Comments
 (0)