Skip to content

Commit

Permalink
Auto merge of #6534 - ehuss:meta-deps-name-null, r=alexcrichton
Browse files Browse the repository at this point in the history
cargo metadata: Don't show `null` deps.

If a package has a dependency without a library target, the "name" field was
showing up as null in `resolve.nodes.deps`. At this time (AFAIK), binary-only
dependencies are always ignored. Instead of making users filter out this entry
(or more commonly, crash), just don't include it.
  • Loading branch information
bors committed Mar 26, 2019
2 parents bae3daf + c3f4b0d commit b3b65c3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
{
#[derive(Serialize)]
struct Dep {
name: Option<String>,
name: String,
pkg: PackageId,
}

Expand All @@ -123,13 +123,12 @@ where
dependencies: resolve.deps(id).map(|(pkg, _deps)| pkg).collect(),
deps: resolve
.deps(id)
.map(|(pkg, _deps)| {
let name = packages
.filter_map(|(pkg, _deps)| {
packages
.get(&pkg)
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
.and_then(|lib_target| resolve.extern_crate_name(id, pkg, lib_target).ok());

Dep { name, pkg }
.and_then(|lib_target| resolve.extern_crate_name(id, pkg, lib_target).ok())
.map(|name| Dep { name, pkg })
})
.collect(),
features: resolve.features_sorted(id),
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/cargo-metadata.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ The output has the following format:
*/
"deps": [
{
/* The name of the dependency.
/* The name of the dependency's library target.
If this is a renamed dependency, this is the new
name.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/generated/cargo-metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
*/
"deps": [
{
/* The name of the dependency.
/* The name of the dependency's library target.
If this is a renamed dependency, this is the new
name.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/etc/man/cargo-metadata.1
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ The output has the following format:
*/
"deps": [
{
/* The name of the dependency.
/* The name of the dependency\(aqs library target.
If this is a renamed dependency, this is the new
name.
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,3 +1669,32 @@ fn metadata_links() {
)
.run()
}

#[test]
fn deps_with_bin_only() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bdep = { path = "bdep" }
"#,
)
.file("src/lib.rs", "")
.file("bdep/Cargo.toml", &basic_bin_manifest("bdep"))
.file("bdep/src/main.rs", "fn main() {}")
.build();

let output = p
.cargo("metadata")
.exec_with_output()
.expect("cargo metadata failed");
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let meta: serde_json::Value = serde_json::from_str(stdout).expect("failed to parse json");
let nodes = &meta["resolve"]["nodes"];
assert!(nodes[0]["deps"].as_array().unwrap().is_empty());
assert!(nodes[1]["deps"].as_array().unwrap().is_empty());
}

0 comments on commit b3b65c3

Please sign in to comment.