Skip to content

Commit

Permalink
Auto merge of #4571 - raytung:remove-root-from-cargo-lock, r=matklad
Browse files Browse the repository at this point in the history
Remove root from cargo lock

Removing [root] section from Cargo.lock for #4563

Tests of interest has been updated accordingly, especially [tests/lockfile-compat.rs#oldest_lockfile_still_works](https://github.com/rust-lang/cargo/compare/master...raytung:remove-root-from-cargo-lock?expand=1#diff-48866d1891f97cb799dd0ca7148d1eefR10)
  • Loading branch information
bors committed Oct 4, 2017
2 parents 8b0131b + cab464e commit f0b06ca
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 54 deletions.
18 changes: 2 additions & 16 deletions src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::Resolve;
#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableResolve {
package: Option<Vec<EncodableDependency>>,
/// `root` is optional to allow forward compatibility.
/// `root` is optional to allow backward compatibility.
root: Option<EncodableDependency>,
metadata: Option<Metadata>,

Expand Down Expand Up @@ -311,7 +311,6 @@ impl<'de> de::Deserialize<'de> for EncodablePackageId {
pub struct WorkspaceResolve<'a, 'cfg: 'a> {
pub ws: &'a Workspace<'cfg>,
pub resolve: &'a Resolve,
pub use_root_key: bool,
}

impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {
Expand All @@ -321,15 +320,7 @@ impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {
let mut ids: Vec<&PackageId> = self.resolve.graph.iter().collect();
ids.sort();

let root = self.ws.members().max_by_key(|member| {
member.name()
}).map(Package::package_id);

let encodable = ids.iter().filter_map(|&id| {
if self.use_root_key && root.unwrap() == id {
return None
}

Some(encodable_resolve_node(id, self.resolve))
}).collect::<Vec<_>>();

Expand All @@ -347,11 +338,6 @@ impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {

let metadata = if metadata.is_empty() { None } else { Some(metadata) };

let root = match root {
Some(root) if self.use_root_key => Some(encodable_resolve_node(root, self.resolve)),
_ => None,
};

let patch = Patch {
unused: self.resolve.unused_patches().iter().map(|id| {
EncodableDependency {
Expand All @@ -365,7 +351,7 @@ impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {
};
EncodableResolve {
package: Some(encodable),
root: root,
root: None,
metadata: metadata,
patch: patch,
}.serialize(s)
Expand Down
17 changes: 0 additions & 17 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,13 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
Ok(s)
});

// Forward compatibility: if `orig` uses rootless format
// from the future, do the same.
let use_root_key = if let Ok(ref orig) = orig {
!orig.starts_with("[[package]]")
} else {
true
};

let toml = toml::Value::try_from(WorkspaceResolve {
ws: ws,
resolve: resolve,
use_root_key: use_root_key,
}).unwrap();

let mut out = String::new();

// Note that we do not use e.toml.to_string() as we want to control the
// exact format the toml is in to ensure pretty diffs between updates to the
// lockfile.
if let Some(root) = toml.get("root") {
out.push_str("[root]\n");
emit_package(root.as_table().unwrap(), &mut out);
}

let deps = toml["package"].as_array().unwrap();
for dep in deps.iter() {
let dep = dep.as_table().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book/src/guide/cargo-toml-vs-cargo-lock.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Cargo will take the latest commit and write that information out into our
`Cargo.lock` when we build for the first time. That file will look like this:

```toml
[root]
[[package]]
name = "hello_world"
version = "0.1.0"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ Cargo will take the latest commit and write that information out into our
`Cargo.lock` when we build for the first time. That file will look like this:

```toml
[root]
[[package]]
name = "hello_world"
version = "0.1.0"
dependencies = [
Expand Down
6 changes: 3 additions & 3 deletions tests/bad-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn duplicate_packages_in_cargo_lock() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -300,7 +300,7 @@ fn bad_source_in_cargo_lock() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -334,7 +334,7 @@ fn bad_dependency_in_lockfile() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "foo"
version = "0.0.1"
dependencies = [
Expand Down
4 changes: 2 additions & 2 deletions tests/generate-lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn preserve_line_endings_issue_2076() {

let lock0 = p.read_lockfile();

assert!(lock0.starts_with("[root]\n"));
assert!(lock0.starts_with("[[package]]\n"));

let lock1 = lock0.replace("\n", "\r\n");
{
Expand All @@ -157,7 +157,7 @@ fn preserve_line_endings_issue_2076() {

let lock2 = p.read_lockfile();

assert!(lock2.starts_with("[root]\r\n"));
assert!(lock2.starts_with("[[package]]\r\n"));
assert_eq!(lock1, lock2);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ fn stale_cached_version() {
let rev = repo.revparse_single("HEAD").unwrap().id();

File::create(&foo.root().join("Cargo.lock")).unwrap().write_all(format!(r#"
[root]
[[package]]
name = "foo"
version = "0.0.0"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion tests/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ fn git_with_lockfile() {
"#)
.file("bar/src/lib.rs", "fn main() {}")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "foo"
version = "0.1.0"
dependencies = [ "bar 0.1.0" ]
Expand Down
52 changes: 42 additions & 10 deletions tests/lockfile-compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,36 @@ use hamcrest::assert_that;

#[test]
fn oldest_lockfile_still_works() {
let cargo_commands = vec![
"build",
"update"
];
for cargo_command in cargo_commands {
oldest_lockfile_still_works_with_command(cargo_command);
}
}

fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
Package::new("foo", "0.1.0").publish();

let lockfile = r#"
let expected_lockfile =
r#"[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "foo"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
"#;

let old_lockfile = r#"
[root]
name = "bar"
version = "0.0.1"
Expand All @@ -35,14 +62,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
foo = "0.1.0"
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", lockfile);
.file("Cargo.lock", old_lockfile);

p.build();

assert_that(p.cargo("build"),
assert_that(p.cargo(cargo_command),
execs().with_status(0));

let lock = p.read_lockfile();
assert!(lock.starts_with(lockfile.trim()));
for (l, r) in expected_lockfile.lines().zip(lock.lines()) {
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
}

assert_eq!(lock.lines().count(), expected_lockfile.lines().count());
}

#[test]
Expand All @@ -61,7 +93,7 @@ fn totally_wild_checksums_works() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand All @@ -85,7 +117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"

let lock = p.read_lockfile();
assert!(lock.starts_with(r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -117,7 +149,7 @@ fn wrong_checksum_is_an_error() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -170,7 +202,7 @@ fn unlisted_checksum_is_bad_if_we_calculate() {
"#)
.file("src/lib.rs", "")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -230,7 +262,7 @@ fn listed_checksum_bad_if_we_cannot_compute() {
"#, git.url()))
.file("src/lib.rs", "")
.file("Cargo.lock", &format!(r#"
[root]
[[package]]
name = "bar"
version = "0.0.1"
dependencies = [
Expand Down Expand Up @@ -287,7 +319,7 @@ fn current_lockfile_format() {
let actual = p.read_lockfile();

let expected = "\
[root]
[[package]]
name = \"bar\"
version = \"0.0.1\"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion tests/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ fn override_an_override() {
"serde:0.8.0" = { path = "serde" }
"#)
.file("Cargo.lock", r#"
[root]
[[package]]
name = "local"
version = "0.0.1"
dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion tests/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ fn lockfile_can_specify_nonexistant_members() {
"#)
.file("a/src/main.rs", "fn main() {}")
.file("Cargo.lock", r#"
[root]
[[package]]
name = "a"
version = "0.1.0"
Expand Down

0 comments on commit f0b06ca

Please sign in to comment.