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.
This commit is contained in:
Eric Huss 2019-01-09 21:04:16 -08:00
parent b296129ab8
commit c3f4b0db2d
5 changed files with 37 additions and 9 deletions

View File

@ -105,7 +105,7 @@ where
{
#[derive(Serialize)]
struct Dep {
name: Option<String>,
name: String,
pkg: PackageId,
}
@ -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),

View File

@ -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.
*/

View File

@ -219,7 +219,7 @@ for a Rust API for reading the metadata.</p>
*/
"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.
*/

View File

@ -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.
*/

View File

@ -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());
}