diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 91eb0b4d81e..5547a75059c 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -43,6 +43,16 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()> let mut out = String::new(); + // Preserve the top comments in the lockfile + // This is in preparation for marking it as generated + // https://github.com/rust-lang/cargo/issues/6180 + if let Ok(orig) = &orig { + for line in orig.lines().take_while(|line| line.starts_with("#")) { + out.push_str(line); + out.push_str("\n"); + } + } + let deps = toml["package"].as_array().unwrap(); for dep in deps.iter() { let dep = dep.as_table().unwrap(); diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 8cdae253e0f..be527b288db 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -374,3 +374,24 @@ fn update_precise() { ", ).run(); } + +#[test] +fn preserve_top_comment() { + let p = project().file("src/lib.rs", "").build(); + + p.cargo("update").run(); + + let mut lockfile = p.read_file("Cargo.lock"); + lockfile.insert_str(0, "# @generated\n"); + lockfile.insert_str(0, "# some other comment\n"); + println!("saving Cargo.lock contents:\n{}", lockfile); + + p.change_file("Cargo.lock", &lockfile); + + p.cargo("update").run(); + + let lockfile2 = p.read_file("Cargo.lock"); + println!("loaded Cargo.lock contents:\n{}", lockfile2); + + assert!(lockfile == lockfile2); +}