Skip to content

Commit aeda739

Browse files
authored
Sort crate dependencies before adding to index (#6317)
Also ensure that the sorting of dependencies uses all fields.
1 parent 32d4686 commit aeda739

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

cargo-registry-index/lib.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,26 @@ impl Ord for Dependency {
175175
// misinterpreted. With this manual `Ord` implementation we ensure that
176176
// `normal` dependencies are always first when multiple with the same
177177
// `name` exist.
178-
(&self.name, self.kind, &self.req).cmp(&(&other.name, other.kind, &other.req))
178+
(
179+
&self.name,
180+
self.kind,
181+
&self.req,
182+
self.optional,
183+
self.default_features,
184+
&self.target,
185+
&self.package,
186+
&self.features,
187+
)
188+
.cmp(&(
189+
&other.name,
190+
other.kind,
191+
&other.req,
192+
other.optional,
193+
other.default_features,
194+
&other.target,
195+
&other.package,
196+
&other.features,
197+
))
179198
}
180199
}
181200

src/controllers/krate/publish.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ pub fn add_dependencies(
409409
})
410410
.collect::<Result<Vec<_>, _>>()?;
411411

412-
let (git_deps, new_dependencies): (Vec<_>, Vec<_>) =
412+
let (mut git_deps, new_dependencies): (Vec<_>, Vec<_>) =
413413
git_and_new_dependencies.into_iter().unzip();
414+
git_deps.sort();
414415

415416
insert_into(dependencies)
416417
.values(&new_dependencies)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"request": {
4+
"uri": "http://alexcrichton-test.s3.amazonaws.com/crates/two-deps/two-deps-1.0.0.crate",
5+
"method": "PUT",
6+
"headers": [
7+
[
8+
"accept",
9+
"*/*"
10+
],
11+
[
12+
"accept-encoding",
13+
"gzip"
14+
],
15+
[
16+
"content-length",
17+
"35"
18+
],
19+
[
20+
"content-type",
21+
"application/gzip"
22+
]
23+
],
24+
"body": "H4sIAAAAAAAA/+3AAQEAAACCIP+vbkhQwKsBLq+17wAEAAA="
25+
},
26+
"response": {
27+
"status": 200,
28+
"headers": [],
29+
"body": ""
30+
}
31+
},
32+
{
33+
"request": {
34+
"uri": "http://alexcrichton-test.s3.amazonaws.com/tw/o-/two-deps",
35+
"method": "PUT",
36+
"headers": [
37+
[
38+
"accept",
39+
"*/*"
40+
],
41+
[
42+
"accept-encoding",
43+
"gzip"
44+
],
45+
[
46+
"content-length",
47+
"376"
48+
],
49+
[
50+
"content-type",
51+
"text/plain"
52+
]
53+
],
54+
"body": "eyJuYW1lIjoidHdvLWRlcHMiLCJ2ZXJzIjoiMS4wLjAiLCJkZXBzIjpbeyJuYW1lIjoiZGVwLWEiLCJyZXEiOiI+IDAiLCJmZWF0dXJlcyI6W10sIm9wdGlvbmFsIjpmYWxzZSwiZGVmYXVsdF9mZWF0dXJlcyI6dHJ1ZSwidGFyZ2V0IjpudWxsLCJraW5kIjoibm9ybWFsIn0seyJuYW1lIjoiZGVwLWIiLCJyZXEiOiI+IDAiLCJmZWF0dXJlcyI6W10sIm9wdGlvbmFsIjpmYWxzZSwiZGVmYXVsdF9mZWF0dXJlcyI6dHJ1ZSwidGFyZ2V0IjpudWxsLCJraW5kIjoibm9ybWFsIn1dLCJja3N1bSI6ImFjYjU2MDRiMTI2YWM4OTRjMWViMTFjNDU3NWJmMjA3MmZlYTYxMjMyYTg4OGU0NTM3NzBjNzlkN2VkNTY0MTkiLCJmZWF0dXJlcyI6e30sInlhbmtlZCI6ZmFsc2V9Cg=="
55+
},
56+
"response": {
57+
"status": 200,
58+
"headers": [],
59+
"body": ""
60+
}
61+
}
62+
]

src/tests/krate/publish.rs

+28
Original file line numberDiff line numberDiff line change
@@ -995,3 +995,31 @@ fn features_version_2() {
995995
)]);
996996
assert_eq!(crates[0].features2, Some(features2));
997997
}
998+
999+
#[test]
1000+
fn new_krate_sorts_deps() {
1001+
let (app, _, user, token) = TestApp::full().with_token();
1002+
1003+
app.db(|conn| {
1004+
// Insert crates directly into the database so that two-deps can depend on it
1005+
CrateBuilder::new("dep-a", user.as_model().id).expect_build(conn);
1006+
CrateBuilder::new("dep-b", user.as_model().id).expect_build(conn);
1007+
});
1008+
1009+
let dep_a = DependencyBuilder::new("dep-a");
1010+
let dep_b = DependencyBuilder::new("dep-b");
1011+
1012+
// Add the deps in reverse order to ensure they get sorted.
1013+
let crate_to_publish = PublishBuilder::new("two-deps")
1014+
.version("1.0.0")
1015+
.dependency(dep_b)
1016+
.dependency(dep_a);
1017+
token.publish_crate(crate_to_publish).good();
1018+
1019+
let crates = app.crates_from_index_head("two-deps");
1020+
assert!(crates.len() == 1);
1021+
let deps = &crates[0].deps;
1022+
assert!(deps.len() == 2);
1023+
assert_eq!(deps[0].name, "dep-a");
1024+
assert_eq!(deps[1].name, "dep-b");
1025+
}

0 commit comments

Comments
 (0)