Skip to content

Commit 7acf376

Browse files
committedDec 30, 2019
Remove metadata dep_kinds duplicates.
1 parent 4111e1a commit 7acf376

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed
 

‎src/cargo/ops/cargo_output_metadata.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct Dep {
8888
dep_kinds: Vec<DepKindInfo>,
8989
}
9090

91-
#[derive(Serialize)]
91+
#[derive(Serialize, PartialEq, Eq, PartialOrd, Ord)]
9292
struct DepKindInfo {
9393
kind: dependency::Kind,
9494
target: Option<Platform>,
@@ -184,14 +184,19 @@ fn build_resolve_graph_r(
184184
None => true,
185185
})
186186
.filter_map(|(dep_id, deps)| {
187+
let mut dep_kinds: Vec<_> = deps.iter().map(DepKindInfo::from).collect();
188+
// Duplicates may appear if the same package is used by different
189+
// members of a workspace with different features selected.
190+
dep_kinds.sort_unstable();
191+
dep_kinds.dedup();
187192
package_map
188193
.get(&dep_id)
189194
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
190195
.and_then(|lib_target| resolve.extern_crate_name(pkg_id, dep_id, lib_target).ok())
191196
.map(|name| Dep {
192197
name,
193198
pkg: dep_id,
194-
dep_kinds: deps.iter().map(DepKindInfo::from).collect(),
199+
dep_kinds,
195200
})
196201
})
197202
.collect();

‎tests/testsuite/metadata.rs

+110
Original file line numberDiff line numberDiff line change
@@ -2623,3 +2623,113 @@ fn dep_kinds() {
26232623
)
26242624
.run();
26252625
}
2626+
2627+
#[cargo_test]
2628+
fn dep_kinds_workspace() {
2629+
// Check for bug with duplicate dep kinds in a workspace.
2630+
// If different members select different features for the same package,
2631+
// they show up multiple times in the resolver `deps`.
2632+
//
2633+
// Here:
2634+
// foo -> dep
2635+
// bar -> foo[feat1] -> dep
2636+
let p = project()
2637+
.file(
2638+
"Cargo.toml",
2639+
r#"
2640+
[package]
2641+
name = "foo"
2642+
version = "0.1.0"
2643+
2644+
[features]
2645+
feat1 = []
2646+
2647+
[dependencies]
2648+
dep = { path="dep" }
2649+
2650+
[workspace]
2651+
members = ["bar"]
2652+
"#,
2653+
)
2654+
.file("src/lib.rs", "")
2655+
.file(
2656+
"bar/Cargo.toml",
2657+
r#"
2658+
[package]
2659+
name = "bar"
2660+
version = "0.1.0"
2661+
2662+
[dependencies]
2663+
foo = { path="..", features=["feat1"] }
2664+
"#,
2665+
)
2666+
.file("bar/src/lib.rs", "")
2667+
.file("dep/Cargo.toml", &basic_lib_manifest("dep"))
2668+
.file("dep/src/lib.rs", "")
2669+
.build();
2670+
2671+
p.cargo("metadata")
2672+
.with_json(
2673+
r#"
2674+
{
2675+
"packages": "{...}",
2676+
"workspace_members": "{...}",
2677+
"target_directory": "[..]/foo/target",
2678+
"version": 1,
2679+
"workspace_root": "[..]/foo",
2680+
"resolve": {
2681+
"nodes": [
2682+
{
2683+
"id": "dep 0.5.0 (path+file://[..]/foo/dep)",
2684+
"dependencies": [],
2685+
"deps": [],
2686+
"features": []
2687+
},
2688+
{
2689+
"id": "bar 0.1.0 (path+file://[..]/foo/bar)",
2690+
"dependencies": [
2691+
"foo 0.1.0 (path+file://[..]/foo)"
2692+
],
2693+
"deps": [
2694+
{
2695+
"name": "foo",
2696+
"pkg": "foo 0.1.0 (path+file://[..]/foo)",
2697+
"dep_kinds": [
2698+
{
2699+
"kind": null,
2700+
"target": null
2701+
}
2702+
]
2703+
}
2704+
],
2705+
"features": []
2706+
},
2707+
{
2708+
"id": "foo 0.1.0 (path+file://[..]/foo)",
2709+
"dependencies": [
2710+
"dep 0.5.0 (path+file://[..]/foo/dep)"
2711+
],
2712+
"deps": [
2713+
{
2714+
"name": "dep",
2715+
"pkg": "dep 0.5.0 (path+file://[..]/foo/dep)",
2716+
"dep_kinds": [
2717+
{
2718+
"kind": null,
2719+
"target": null
2720+
}
2721+
]
2722+
}
2723+
],
2724+
"features": [
2725+
"feat1"
2726+
]
2727+
}
2728+
],
2729+
"root": "foo 0.1.0 (path+file://[..]/foo)"
2730+
}
2731+
}
2732+
"#,
2733+
)
2734+
.run();
2735+
}

0 commit comments

Comments
 (0)
Please sign in to comment.