Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marking Cargo.lock as generated #6548

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
{
Expand All @@ -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);
}

Expand Down
15 changes: 12 additions & 3 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 = [
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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\"
Expand All @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions tests/testsuite/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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]
Expand Down