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 metadata dep_kinds duplicates. #7756

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct Dep {
dep_kinds: Vec<DepKindInfo>,
}

#[derive(Serialize)]
#[derive(Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct DepKindInfo {
kind: dependency::Kind,
target: Option<Platform>,
Expand Down Expand Up @@ -184,14 +184,19 @@ fn build_resolve_graph_r(
None => true,
})
.filter_map(|(dep_id, deps)| {
let mut dep_kinds: Vec<_> = deps.iter().map(DepKindInfo::from).collect();
// Duplicates may appear if the same package is used by different
// members of a workspace with different features selected.
dep_kinds.sort_unstable();
dep_kinds.dedup();
Copy link
Member

Choose a reason for hiding this comment

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

The vec needs to be sorted for dedup to work properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, thanks!

package_map
.get(&dep_id)
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
.and_then(|lib_target| resolve.extern_crate_name(pkg_id, dep_id, lib_target).ok())
.map(|name| Dep {
name,
pkg: dep_id,
dep_kinds: deps.iter().map(DepKindInfo::from).collect(),
dep_kinds,
})
})
.collect();
Expand Down
110 changes: 110 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2623,3 +2623,113 @@ fn dep_kinds() {
)
.run();
}

#[cargo_test]
fn dep_kinds_workspace() {
// Check for bug with duplicate dep kinds in a workspace.
// If different members select different features for the same package,
// they show up multiple times in the resolver `deps`.
//
// Here:
// foo -> dep
// bar -> foo[feat1] -> dep
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[features]
feat1 = []

[dependencies]
dep = { path="dep" }

[workspace]
members = ["bar"]
"#,
)
.file("src/lib.rs", "")
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"

[dependencies]
foo = { path="..", features=["feat1"] }
"#,
)
.file("bar/src/lib.rs", "")
.file("dep/Cargo.toml", &basic_lib_manifest("dep"))
.file("dep/src/lib.rs", "")
.build();

p.cargo("metadata")
.with_json(
r#"
{
"packages": "{...}",
"workspace_members": "{...}",
"target_directory": "[..]/foo/target",
"version": 1,
"workspace_root": "[..]/foo",
"resolve": {
"nodes": [
{
"id": "dep 0.5.0 (path+file://[..]/foo/dep)",
"dependencies": [],
"deps": [],
"features": []
},
{
"id": "bar 0.1.0 (path+file://[..]/foo/bar)",
"dependencies": [
"foo 0.1.0 (path+file://[..]/foo)"
],
"deps": [
{
"name": "foo",
"pkg": "foo 0.1.0 (path+file://[..]/foo)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
},
{
"id": "foo 0.1.0 (path+file://[..]/foo)",
"dependencies": [
"dep 0.5.0 (path+file://[..]/foo/dep)"
],
"deps": [
{
"name": "dep",
"pkg": "dep 0.5.0 (path+file://[..]/foo/dep)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": [
"feat1"
]
}
],
"root": "foo 0.1.0 (path+file://[..]/foo)"
}
}
"#,
)
.run();
}