Skip to content

Commit f0b06ca

Browse files
committed
Auto merge of #4571 - raytung:remove-root-from-cargo-lock, r=matklad
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)
2 parents 8b0131b + cab464e commit f0b06ca

11 files changed

+55
-54
lines changed

src/cargo/core/resolver/encode.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::Resolve;
1414
#[derive(Serialize, Deserialize, Debug)]
1515
pub struct EncodableResolve {
1616
package: Option<Vec<EncodableDependency>>,
17-
/// `root` is optional to allow forward compatibility.
17+
/// `root` is optional to allow backward compatibility.
1818
root: Option<EncodableDependency>,
1919
metadata: Option<Metadata>,
2020

@@ -311,7 +311,6 @@ impl<'de> de::Deserialize<'de> for EncodablePackageId {
311311
pub struct WorkspaceResolve<'a, 'cfg: 'a> {
312312
pub ws: &'a Workspace<'cfg>,
313313
pub resolve: &'a Resolve,
314-
pub use_root_key: bool,
315314
}
316315

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

324-
let root = self.ws.members().max_by_key(|member| {
325-
member.name()
326-
}).map(Package::package_id);
327-
328323
let encodable = ids.iter().filter_map(|&id| {
329-
if self.use_root_key && root.unwrap() == id {
330-
return None
331-
}
332-
333324
Some(encodable_resolve_node(id, self.resolve))
334325
}).collect::<Vec<_>>();
335326

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

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

350-
let root = match root {
351-
Some(root) if self.use_root_key => Some(encodable_resolve_node(root, self.resolve)),
352-
_ => None,
353-
};
354-
355341
let patch = Patch {
356342
unused: self.resolve.unused_patches().iter().map(|id| {
357343
EncodableDependency {
@@ -365,7 +351,7 @@ impl<'a, 'cfg> ser::Serialize for WorkspaceResolve<'a, 'cfg> {
365351
};
366352
EncodableResolve {
367353
package: Some(encodable),
368-
root: root,
354+
root: None,
369355
metadata: metadata,
370356
patch: patch,
371357
}.serialize(s)

src/cargo/ops/lockfile.rs

-17
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,13 @@ pub fn write_pkg_lockfile(ws: &Workspace, resolve: &Resolve) -> CargoResult<()>
4040
Ok(s)
4141
});
4242

43-
// Forward compatibility: if `orig` uses rootless format
44-
// from the future, do the same.
45-
let use_root_key = if let Ok(ref orig) = orig {
46-
!orig.starts_with("[[package]]")
47-
} else {
48-
true
49-
};
50-
5143
let toml = toml::Value::try_from(WorkspaceResolve {
5244
ws: ws,
5345
resolve: resolve,
54-
use_root_key: use_root_key,
5546
}).unwrap();
5647

5748
let mut out = String::new();
5849

59-
// Note that we do not use e.toml.to_string() as we want to control the
60-
// exact format the toml is in to ensure pretty diffs between updates to the
61-
// lockfile.
62-
if let Some(root) = toml.get("root") {
63-
out.push_str("[root]\n");
64-
emit_package(root.as_table().unwrap(), &mut out);
65-
}
66-
6750
let deps = toml["package"].as_array().unwrap();
6851
for dep in deps.iter() {
6952
let dep = dep.as_table().unwrap();

src/doc/book/src/guide/cargo-toml-vs-cargo-lock.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Cargo will take the latest commit and write that information out into our
7171
`Cargo.lock` when we build for the first time. That file will look like this:
7272

7373
```toml
74-
[root]
74+
[[package]]
7575
name = "hello_world"
7676
version = "0.1.0"
7777
dependencies = [

src/doc/guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ Cargo will take the latest commit and write that information out into our
328328
`Cargo.lock` when we build for the first time. That file will look like this:
329329

330330
```toml
331-
[root]
331+
[[package]]
332332
name = "hello_world"
333333
version = "0.1.0"
334334
dependencies = [

tests/bad-config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn duplicate_packages_in_cargo_lock() {
256256
"#)
257257
.file("src/lib.rs", "")
258258
.file("Cargo.lock", r#"
259-
[root]
259+
[[package]]
260260
name = "bar"
261261
version = "0.0.1"
262262
dependencies = [
@@ -300,7 +300,7 @@ fn bad_source_in_cargo_lock() {
300300
"#)
301301
.file("src/lib.rs", "")
302302
.file("Cargo.lock", r#"
303-
[root]
303+
[[package]]
304304
name = "bar"
305305
version = "0.0.1"
306306
dependencies = [
@@ -334,7 +334,7 @@ fn bad_dependency_in_lockfile() {
334334
"#)
335335
.file("src/lib.rs", "")
336336
.file("Cargo.lock", r#"
337-
[root]
337+
[[package]]
338338
name = "foo"
339339
version = "0.0.1"
340340
dependencies = [

tests/generate-lockfile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn preserve_line_endings_issue_2076() {
145145

146146
let lock0 = p.read_lockfile();
147147

148-
assert!(lock0.starts_with("[root]\n"));
148+
assert!(lock0.starts_with("[[package]]\n"));
149149

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

158158
let lock2 = p.read_lockfile();
159159

160-
assert!(lock2.starts_with("[root]\r\n"));
160+
assert!(lock2.starts_with("[[package]]\r\n"));
161161
assert_eq!(lock1, lock2);
162162
}
163163

tests/git.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ fn stale_cached_version() {
971971
let rev = repo.revparse_single("HEAD").unwrap().id();
972972

973973
File::create(&foo.root().join("Cargo.lock")).unwrap().write_all(format!(r#"
974-
[root]
974+
[[package]]
975975
name = "foo"
976976
version = "0.0.0"
977977
dependencies = [

tests/install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ fn git_with_lockfile() {
829829
"#)
830830
.file("bar/src/lib.rs", "fn main() {}")
831831
.file("Cargo.lock", r#"
832-
[root]
832+
[[package]]
833833
name = "foo"
834834
version = "0.1.0"
835835
dependencies = [ "bar 0.1.0" ]

tests/lockfile-compat.rs

+42-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,36 @@ use hamcrest::assert_that;
88

99
#[test]
1010
fn oldest_lockfile_still_works() {
11+
let cargo_commands = vec![
12+
"build",
13+
"update"
14+
];
15+
for cargo_command in cargo_commands {
16+
oldest_lockfile_still_works_with_command(cargo_command);
17+
}
18+
}
19+
20+
fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
1121
Package::new("foo", "0.1.0").publish();
1222

13-
let lockfile = r#"
23+
let expected_lockfile =
24+
r#"[[package]]
25+
name = "bar"
26+
version = "0.0.1"
27+
dependencies = [
28+
"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
29+
]
30+
31+
[[package]]
32+
name = "foo"
33+
version = "0.1.0"
34+
source = "registry+https://github.com/rust-lang/crates.io-index"
35+
36+
[metadata]
37+
"checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "[..]"
38+
"#;
39+
40+
let old_lockfile = r#"
1441
[root]
1542
name = "bar"
1643
version = "0.0.1"
@@ -35,14 +62,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
3562
foo = "0.1.0"
3663
"#)
3764
.file("src/lib.rs", "")
38-
.file("Cargo.lock", lockfile);
65+
.file("Cargo.lock", old_lockfile);
66+
3967
p.build();
4068

41-
assert_that(p.cargo("build"),
69+
assert_that(p.cargo(cargo_command),
4270
execs().with_status(0));
4371

4472
let lock = p.read_lockfile();
45-
assert!(lock.starts_with(lockfile.trim()));
73+
for (l, r) in expected_lockfile.lines().zip(lock.lines()) {
74+
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
75+
}
76+
77+
assert_eq!(lock.lines().count(), expected_lockfile.lines().count());
4678
}
4779

4880
#[test]
@@ -61,7 +93,7 @@ fn totally_wild_checksums_works() {
6193
"#)
6294
.file("src/lib.rs", "")
6395
.file("Cargo.lock", r#"
64-
[root]
96+
[[package]]
6597
name = "bar"
6698
version = "0.0.1"
6799
dependencies = [
@@ -85,7 +117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
85117

86118
let lock = p.read_lockfile();
87119
assert!(lock.starts_with(r#"
88-
[root]
120+
[[package]]
89121
name = "bar"
90122
version = "0.0.1"
91123
dependencies = [
@@ -117,7 +149,7 @@ fn wrong_checksum_is_an_error() {
117149
"#)
118150
.file("src/lib.rs", "")
119151
.file("Cargo.lock", r#"
120-
[root]
152+
[[package]]
121153
name = "bar"
122154
version = "0.0.1"
123155
dependencies = [
@@ -170,7 +202,7 @@ fn unlisted_checksum_is_bad_if_we_calculate() {
170202
"#)
171203
.file("src/lib.rs", "")
172204
.file("Cargo.lock", r#"
173-
[root]
205+
[[package]]
174206
name = "bar"
175207
version = "0.0.1"
176208
dependencies = [
@@ -230,7 +262,7 @@ fn listed_checksum_bad_if_we_cannot_compute() {
230262
"#, git.url()))
231263
.file("src/lib.rs", "")
232264
.file("Cargo.lock", &format!(r#"
233-
[root]
265+
[[package]]
234266
name = "bar"
235267
version = "0.0.1"
236268
dependencies = [
@@ -287,7 +319,7 @@ fn current_lockfile_format() {
287319
let actual = p.read_lockfile();
288320

289321
let expected = "\
290-
[root]
322+
[[package]]
291323
name = \"bar\"
292324
version = \"0.0.1\"
293325
dependencies = [

tests/overrides.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ fn override_an_override() {
802802
"serde:0.8.0" = { path = "serde" }
803803
"#)
804804
.file("Cargo.lock", r#"
805-
[root]
805+
[[package]]
806806
name = "local"
807807
version = "0.0.1"
808808
dependencies = [

tests/workspaces.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ fn lockfile_can_specify_nonexistant_members() {
961961
"#)
962962
.file("a/src/main.rs", "fn main() {}")
963963
.file("Cargo.lock", r#"
964-
[root]
964+
[[package]]
965965
name = "a"
966966
version = "0.1.0"
967967

0 commit comments

Comments
 (0)