Skip to content

Commit b3b65c3

Browse files
committed
Auto merge of #6534 - ehuss:meta-deps-name-null, r=alexcrichton
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.
2 parents bae3daf + c3f4b0d commit b3b65c3

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105
{
106106
#[derive(Serialize)]
107107
struct Dep {
108-
name: Option<String>,
108+
name: String,
109109
pkg: PackageId,
110110
}
111111

@@ -123,13 +123,12 @@ where
123123
dependencies: resolve.deps(id).map(|(pkg, _deps)| pkg).collect(),
124124
deps: resolve
125125
.deps(id)
126-
.map(|(pkg, _deps)| {
127-
let name = packages
126+
.filter_map(|(pkg, _deps)| {
127+
packages
128128
.get(&pkg)
129129
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
130-
.and_then(|lib_target| resolve.extern_crate_name(id, pkg, lib_target).ok());
131-
132-
Dep { name, pkg }
130+
.and_then(|lib_target| resolve.extern_crate_name(id, pkg, lib_target).ok())
131+
.map(|name| Dep { name, pkg })
133132
})
134133
.collect(),
135134
features: resolve.features_sorted(id),

src/doc/man/cargo-metadata.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ The output has the following format:
212212
*/
213213
"deps": [
214214
{
215-
/* The name of the dependency.
215+
/* The name of the dependency's library target.
216216
If this is a renamed dependency, this is the new
217217
name.
218218
*/

src/doc/man/generated/cargo-metadata.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
219219
*/
220220
"deps": [
221221
{
222-
/* The name of the dependency.
222+
/* The name of the dependency's library target.
223223
If this is a renamed dependency, this is the new
224224
name.
225225
*/

src/etc/man/cargo-metadata.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ The output has the following format:
233233
*/
234234
"deps": [
235235
{
236-
/* The name of the dependency.
236+
/* The name of the dependency\(aqs library target.
237237
If this is a renamed dependency, this is the new
238238
name.
239239
*/

tests/testsuite/metadata.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,32 @@ fn metadata_links() {
16691669
)
16701670
.run()
16711671
}
1672+
1673+
#[test]
1674+
fn deps_with_bin_only() {
1675+
let p = project()
1676+
.file(
1677+
"Cargo.toml",
1678+
r#"
1679+
[package]
1680+
name = "foo"
1681+
version = "0.1.0"
1682+
[dependencies]
1683+
bdep = { path = "bdep" }
1684+
"#,
1685+
)
1686+
.file("src/lib.rs", "")
1687+
.file("bdep/Cargo.toml", &basic_bin_manifest("bdep"))
1688+
.file("bdep/src/main.rs", "fn main() {}")
1689+
.build();
1690+
1691+
let output = p
1692+
.cargo("metadata")
1693+
.exec_with_output()
1694+
.expect("cargo metadata failed");
1695+
let stdout = std::str::from_utf8(&output.stdout).unwrap();
1696+
let meta: serde_json::Value = serde_json::from_str(stdout).expect("failed to parse json");
1697+
let nodes = &meta["resolve"]["nodes"];
1698+
assert!(nodes[0]["deps"].as_array().unwrap().is_empty());
1699+
assert!(nodes[1]["deps"].as_array().unwrap().is_empty());
1700+
}

0 commit comments

Comments
 (0)