diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index c739eb88d5b..d2f06d06a43 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -43,13 +43,32 @@ 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 + // At the start of the file we notify the reader that the file is generated. + // Specifically Phabricator ignores files containing "@generated", so we use that. + let marker_line = "# This file is automatically @generated by Cargo."; + let extra_line = "# It is not intended for manual editing."; + out.push_str(marker_line); + out.push('\n'); + out.push_str(extra_line); + out.push('\n'); + // and preserve any other top comments 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 mut comments = orig.lines().take_while(|line| line.starts_with('#')); + if let Some(first) = comments.next() { + if first != marker_line { + out.push_str(first); + out.push('\n'); + } + if let Some(second) = comments.next() { + if second != extra_line { + out.push_str(second); + out.push('\n'); + } + for line in comments { + out.push_str(line); + out.push('\n'); + } + } } } diff --git a/tests/testsuite/generate_lockfile.rs b/tests/testsuite/generate_lockfile.rs index d494dd93244..449f44e68db 100644 --- a/tests/testsuite/generate_lockfile.rs +++ b/tests/testsuite/generate_lockfile.rs @@ -144,7 +144,7 @@ fn preserve_line_endings_issue_2076() { let lock0 = p.read_lockfile(); - assert!(lock0.starts_with("[[package]]\n")); + assert!(lock0.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n")); let lock1 = lock0.replace("\n", "\r\n"); { @@ -158,7 +158,7 @@ fn preserve_line_endings_issue_2076() { let lock2 = p.read_lockfile(); - assert!(lock2.starts_with("[[package]]\r\n")); + assert!(lock2.starts_with("# This file is automatically @generated by Cargo.\r\n# It is not intended for manual editing.\r\n")); assert_eq!(lock1, lock2); } diff --git a/tests/testsuite/lockfile_compat.rs b/tests/testsuite/lockfile_compat.rs index 5b36eda436b..959db7bf5c1 100644 --- a/tests/testsuite/lockfile_compat.rs +++ b/tests/testsuite/lockfile_compat.rs @@ -13,7 +13,9 @@ fn oldest_lockfile_still_works() { fn oldest_lockfile_still_works_with_command(cargo_command: &str) { Package::new("bar", "0.1.0").publish(); - let expected_lockfile = r#"[[package]] + let expected_lockfile = r#"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] name = "bar" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -29,7 +31,8 @@ dependencies = [ "checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]" "#; - let old_lockfile = r#"[root] + let old_lockfile = r#" +[root] name = "foo" version = "0.0.1" dependencies = [ @@ -165,6 +168,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" let lock = p.read_lockfile(); assert!(lock.starts_with( r#" +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "bar" version = "0.1.0" @@ -404,6 +409,7 @@ fn current_lockfile_format() { let actual = p.read_lockfile(); let expected = "\ +# This file is automatically @generated by Cargo.\n# It is not intended for manual editing. [[package]] name = \"bar\" version = \"0.1.0\" @@ -430,7 +436,10 @@ dependencies = [ fn lockfile_without_root() { Package::new("bar", "0.1.0").publish(); - let lockfile = r#"[[package]] + let lockfile = r#" +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] name = "bar" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 6d6c95dc6b0..91b22fb6ca4 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -401,19 +401,23 @@ fn preserve_top_comment() { 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"); + let lockfile = p.read_lockfile(); + assert!(lockfile.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n")); + + let mut lines = lockfile.lines().collect::>(); + lines.insert(2, "# some other comment"); + let mut lockfile = lines.join("\n"); + lockfile.push_str("\n"); // .lines/.join loses the last newline println!("saving Cargo.lock contents:\n{}", lockfile); p.change_file("Cargo.lock", &lockfile); p.cargo("update").run(); - let lockfile2 = p.read_file("Cargo.lock"); + let lockfile2 = p.read_lockfile(); println!("loaded Cargo.lock contents:\n{}", lockfile2); - assert!(lockfile == lockfile2); + assert_eq!(lockfile, lockfile2); } #[test]