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

Remove root from cargo lock #4571

Merged
merged 7 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 1 addition & 24 deletions src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use super::Resolve;
#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableResolve {
package: Option<Vec<EncodableDependency>>,
/// `root` is optional to allow forward compatibility.
root: Option<EncodableDependency>,
metadata: Option<Metadata>,

#[serde(default, skip_serializing_if = "Patch::is_empty")]
Expand All @@ -33,13 +31,7 @@ impl EncodableResolve {
pub fn into_resolve(self, ws: &Workspace) -> CargoResult<Resolve> {
let path_deps = build_path_deps(ws);

let packages = {
let mut packages = self.package.unwrap_or_default();
if let Some(root) = self.root {
packages.insert(0, root);
}
packages
};
let packages = self.package.unwrap_or_default();

// `PackageId`s in the lock file don't include the `source` part
// for workspace members, so we reconstruct proper ids.
Expand Down Expand Up @@ -311,7 +303,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 +312,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 +330,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 +343,6 @@ impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {
};
EncodableResolve {
package: Some(encodable),
root: root,
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]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent catch! I haven't thought of the docs at all!

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]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it true that changes in tests are mostly cosmetics? That is, the tests pass with [root] as well as with [[package]]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it. I made the same change against master branch and the test passes without a hitch

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: 40 additions & 12 deletions tests/lockfile-compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@ use hamcrest::assert_that;
fn oldest_lockfile_still_works() {
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 +52,25 @@ 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"),
execs().with_status(0));
let cargo_commands = vec![
"build",
"update"
];

let lock = p.read_lockfile();
assert!(lock.starts_with(lockfile.trim()));
for cargo_command in cargo_commands {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think this won't work as expected, because the second time around the lockfile will already be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! of course. I'll probably create a helper method and loop through the vector.

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

let lock = p.read_lockfile();
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 +89,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 +113,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 +145,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 +198,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 +258,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 +315,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