Skip to content

Commit 3de188f

Browse files
committed
Auto merge of #6548 - dwijnand:mark-lockfile-as-generated, r=alexcrichton
Marking Cargo.lock as generated 12 weeks have passed, let's mark lock files as generated! Fixes #6180
2 parents 3e59f03 + bd0e4a0 commit 3de188f

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,32 @@ 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
46+
// At the start of the file we notify the reader that the file is generated.
47+
// Specifically Phabricator ignores files containing "@generated", so we use that.
48+
let marker_line = "# This file is automatically @generated by Cargo.";
49+
let extra_line = "# It is not intended for manual editing.";
50+
out.push_str(marker_line);
51+
out.push('\n');
52+
out.push_str(extra_line);
53+
out.push('\n');
54+
// and preserve any other top comments
4955
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");
56+
let mut comments = orig.lines().take_while(|line| line.starts_with('#'));
57+
if let Some(first) = comments.next() {
58+
if first != marker_line {
59+
out.push_str(first);
60+
out.push('\n');
61+
}
62+
if let Some(second) = comments.next() {
63+
if second != extra_line {
64+
out.push_str(second);
65+
out.push('\n');
66+
}
67+
for line in comments {
68+
out.push_str(line);
69+
out.push('\n');
70+
}
71+
}
5372
}
5473
}
5574

tests/testsuite/generate_lockfile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn preserve_line_endings_issue_2076() {
144144

145145
let lock0 = p.read_lockfile();
146146

147-
assert!(lock0.starts_with("[[package]]\n"));
147+
assert!(lock0.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
148148

149149
let lock1 = lock0.replace("\n", "\r\n");
150150
{
@@ -158,7 +158,7 @@ fn preserve_line_endings_issue_2076() {
158158

159159
let lock2 = p.read_lockfile();
160160

161-
assert!(lock2.starts_with("[[package]]\r\n"));
161+
assert!(lock2.starts_with("# This file is automatically @generated by Cargo.\r\n# It is not intended for manual editing.\r\n"));
162162
assert_eq!(lock1, lock2);
163163
}
164164

tests/testsuite/lockfile_compat.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ fn oldest_lockfile_still_works() {
1313
fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
1414
Package::new("bar", "0.1.0").publish();
1515

16-
let expected_lockfile = r#"[[package]]
16+
let expected_lockfile = r#"# This file is automatically @generated by Cargo.
17+
# It is not intended for manual editing.
18+
[[package]]
1719
name = "bar"
1820
version = "0.1.0"
1921
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -29,7 +31,8 @@ dependencies = [
2931
"checksum bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
3032
"#;
3133

32-
let old_lockfile = r#"[root]
34+
let old_lockfile = r#"
35+
[root]
3336
name = "foo"
3437
version = "0.0.1"
3538
dependencies = [
@@ -165,6 +168,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
165168
let lock = p.read_lockfile();
166169
assert!(lock.starts_with(
167170
r#"
171+
# This file is automatically @generated by Cargo.
172+
# It is not intended for manual editing.
168173
[[package]]
169174
name = "bar"
170175
version = "0.1.0"
@@ -404,6 +409,7 @@ fn current_lockfile_format() {
404409
let actual = p.read_lockfile();
405410

406411
let expected = "\
412+
# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.
407413
[[package]]
408414
name = \"bar\"
409415
version = \"0.1.0\"
@@ -430,7 +436,10 @@ dependencies = [
430436
fn lockfile_without_root() {
431437
Package::new("bar", "0.1.0").publish();
432438

433-
let lockfile = r#"[[package]]
439+
let lockfile = r#"
440+
# This file is automatically @generated by Cargo.
441+
# It is not intended for manual editing.
442+
[[package]]
434443
name = "bar"
435444
version = "0.1.0"
436445
source = "registry+https://github.com/rust-lang/crates.io-index"

tests/testsuite/update.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,19 +401,23 @@ fn preserve_top_comment() {
401401

402402
p.cargo("update").run();
403403

404-
let mut lockfile = p.read_file("Cargo.lock");
405-
lockfile.insert_str(0, "# @generated\n");
406-
lockfile.insert_str(0, "# some other comment\n");
404+
let lockfile = p.read_lockfile();
405+
assert!(lockfile.starts_with("# This file is automatically @generated by Cargo.\n# It is not intended for manual editing.\n"));
406+
407+
let mut lines = lockfile.lines().collect::<Vec<_>>();
408+
lines.insert(2, "# some other comment");
409+
let mut lockfile = lines.join("\n");
410+
lockfile.push_str("\n"); // .lines/.join loses the last newline
407411
println!("saving Cargo.lock contents:\n{}", lockfile);
408412

409413
p.change_file("Cargo.lock", &lockfile);
410414

411415
p.cargo("update").run();
412416

413-
let lockfile2 = p.read_file("Cargo.lock");
417+
let lockfile2 = p.read_lockfile();
414418
println!("loaded Cargo.lock contents:\n{}", lockfile2);
415419

416-
assert!(lockfile == lockfile2);
420+
assert_eq!(lockfile, lockfile2);
417421
}
418422

419423
#[test]

0 commit comments

Comments
 (0)