Skip to content

Commit d02e36c

Browse files
committed
Only output field extern_name when using -Zbindeps
1 parent 690e6a4 commit d02e36c

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct MetadataResolveNode {
8282

8383
#[derive(Serialize)]
8484
struct Dep {
85+
// TODO(bindeps): after -Zbindeps gets stabilized,
86+
// mark this field as deprecated in the help manual of cargo-metadata
8587
name: InternedString,
8688
pkg: PackageId,
8789
dep_kinds: Vec<DepKindInfo>,
@@ -91,12 +93,14 @@ struct Dep {
9193
struct DepKindInfo {
9294
kind: DepKind,
9395
target: Option<Platform>,
96+
97+
// vvvvv The fields below are introduced for `-Z bindeps`.
9498
/// What the manifest calls the crate.
9599
///
96100
/// A renamed dependency will show the rename instead of original name.
97-
extern_name: InternedString,
98-
99-
// vvvvv The fields below are introduced for `-Z bindeps`.
101+
// TODO(bindeps): Remove `Option` after -Zbindeps get stabilized.
102+
#[serde(skip_serializing_if = "Option::is_none")]
103+
extern_name: Option<InternedString>,
100104
/// Artifact's crate type, e.g. staticlib, cdylib, bin...
101105
#[serde(skip_serializing_if = "Option::is_none")]
102106
artifact: Option<&'static str>,
@@ -240,11 +244,19 @@ fn build_resolve_graph_r(
240244
Some(a) if a.is_lib() => true,
241245
_ => false,
242246
};
247+
// TODO(bindeps): Cargo shouldn't have `extern_name` field
248+
// if the user is not using -Zbindeps.
249+
// Remove this condition ` after -Zbindeps gets stabilized.
250+
let extern_name = if dep.artifact().is_some() {
251+
Some(extern_name(target)?)
252+
} else {
253+
None
254+
};
243255
if included {
244256
dep_kinds.push(DepKindInfo {
245257
kind: dep.kind(),
246258
target: dep.platform().cloned(),
247-
extern_name: extern_name(target)?,
259+
extern_name,
248260
artifact: None,
249261
compile_target: None,
250262
bin_name: None,
@@ -275,7 +287,7 @@ fn build_resolve_graph_r(
275287
dep_kinds.push(DepKindInfo {
276288
kind: dep.kind(),
277289
target: dep.platform().cloned(),
278-
extern_name: extern_name(target)?,
290+
extern_name: extern_name(target).ok(),
279291
artifact: Some(kind.crate_type()),
280292
compile_target,
281293
bin_name: target.is_bin().then(|| target.name().to_string()),

tests/testsuite/git.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3393,7 +3393,6 @@ fn metadata_master_consistency() {
33933393
"pkg": "bar 1.0.0 (__BAR_SOURCE__#__BAR_HASH__)",
33943394
"dep_kinds": [
33953395
{
3396-
"extern_name": "bar",
33973396
"kind": null,
33983397
"target": null
33993398
}

tests/testsuite/metadata.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ fn cargo_metadata_with_deps_and_version() {
526526
{
527527
"dep_kinds": [
528528
{
529-
"extern_name": "baz",
530529
"kind": null,
531530
"target": null
532531
}
@@ -553,7 +552,6 @@ fn cargo_metadata_with_deps_and_version() {
553552
{
554553
"dep_kinds": [
555554
{
556-
"extern_name": "bar",
557555
"kind": null,
558556
"target": null
559557
}
@@ -564,7 +562,6 @@ fn cargo_metadata_with_deps_and_version() {
564562
{
565563
"dep_kinds": [
566564
{
567-
"extern_name": "foobar",
568565
"kind": "dev",
569566
"target": null
570567
}
@@ -1155,17 +1152,17 @@ fn workspace_metadata_with_dependencies_and_resolve() {
11551152
name = "bar"
11561153
version = "0.5.0"
11571154
authors = []
1158-
1155+
11591156
[build-dependencies]
11601157
artifact = { path = "../artifact/", artifact = "bin", target = "target" }
11611158
bin-only-artifact = { path = "../bin-only-artifact/", artifact = "bin", target = "$ALT_TARGET" }
11621159
non-artifact = { path = "../non-artifact" }
1163-
1160+
11641161
[dependencies]
11651162
artifact = { path = "../artifact/", artifact = ["cdylib", "staticlib", "bin:baz-name"], lib = true, target = "$ALT_TARGET" }
11661163
bin-only-artifact = { path = "../bin-only-artifact/", artifact = "bin:a-name" }
11671164
non-artifact = { path = "../non-artifact" }
1168-
1165+
11691166
[dev-dependencies]
11701167
artifact = { path = "../artifact/" }
11711168
non-artifact = { path = "../non-artifact" }
@@ -1654,7 +1651,6 @@ fn workspace_metadata_with_dependencies_and_resolve() {
16541651
"target": null
16551652
},
16561653
{
1657-
"extern_name": "artifact",
16581654
"kind": "dev",
16591655
"target": null
16601656
},
@@ -1717,17 +1713,14 @@ fn workspace_metadata_with_dependencies_and_resolve() {
17171713
{
17181714
"dep_kinds": [
17191715
{
1720-
"extern_name": "non_artifact",
17211716
"kind": null,
17221717
"target": null
17231718
},
17241719
{
1725-
"extern_name": "non_artifact",
17261720
"kind": "dev",
17271721
"target": null
17281722
},
17291723
{
1730-
"extern_name": "non_artifact",
17311724
"kind": "build",
17321725
"target": null
17331726
}
@@ -2764,7 +2757,6 @@ fn rename_dependency() {
27642757
{
27652758
"dep_kinds": [
27662759
{
2767-
"extern_name": "bar",
27682760
"kind": null,
27692761
"target": null
27702762
}
@@ -2775,7 +2767,6 @@ fn rename_dependency() {
27752767
{
27762768
"dep_kinds": [
27772769
{
2778-
"extern_name": "baz",
27792770
"kind": null,
27802771
"target": null
27812772
}
@@ -3392,7 +3383,6 @@ fn filter_platform() {
33923383
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
33933384
"dep_kinds": [
33943385
{
3395-
"extern_name": "alt_dep",
33963386
"kind": null,
33973387
"target": "$ALT_TRIPLE"
33983388
}
@@ -3403,7 +3393,6 @@ fn filter_platform() {
34033393
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
34043394
"dep_kinds": [
34053395
{
3406-
"extern_name": "cfg_dep",
34073396
"kind": null,
34083397
"target": "cfg(foobar)"
34093398
}
@@ -3414,7 +3403,6 @@ fn filter_platform() {
34143403
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
34153404
"dep_kinds": [
34163405
{
3417-
"extern_name": "host_dep",
34183406
"kind": null,
34193407
"target": "$HOST_TRIPLE"
34203408
}
@@ -3425,7 +3413,6 @@ fn filter_platform() {
34253413
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
34263414
"dep_kinds": [
34273415
{
3428-
"extern_name": "normal_dep",
34293416
"kind": null,
34303417
"target": null
34313418
}
@@ -3507,7 +3494,6 @@ fn filter_platform() {
35073494
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
35083495
"dep_kinds": [
35093496
{
3510-
"extern_name": "alt_dep",
35113497
"kind": null,
35123498
"target": "$ALT_TRIPLE"
35133499
}
@@ -3518,7 +3504,6 @@ fn filter_platform() {
35183504
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
35193505
"dep_kinds": [
35203506
{
3521-
"extern_name": "normal_dep",
35223507
"kind": null,
35233508
"target": null
35243509
}
@@ -3584,7 +3569,6 @@ fn filter_platform() {
35843569
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
35853570
"dep_kinds": [
35863571
{
3587-
"extern_name": "host_dep",
35883572
"kind": null,
35893573
"target": "$HOST_TRIPLE"
35903574
}
@@ -3595,7 +3579,6 @@ fn filter_platform() {
35953579
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
35963580
"dep_kinds": [
35973581
{
3598-
"extern_name": "normal_dep",
35993582
"kind": null,
36003583
"target": null
36013584
}
@@ -3677,7 +3660,6 @@ fn filter_platform() {
36773660
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
36783661
"dep_kinds": [
36793662
{
3680-
"extern_name": "cfg_dep",
36813663
"kind": null,
36823664
"target": "cfg(foobar)"
36833665
}
@@ -3688,7 +3670,6 @@ fn filter_platform() {
36883670
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
36893671
"dep_kinds": [
36903672
{
3691-
"extern_name": "host_dep",
36923673
"kind": null,
36933674
"target": "$HOST_TRIPLE"
36943675
}
@@ -3699,7 +3680,6 @@ fn filter_platform() {
36993680
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
37003681
"dep_kinds": [
37013682
{
3702-
"extern_name": "normal_dep",
37033683
"kind": null,
37043684
"target": null
37053685
}
@@ -3797,17 +3777,14 @@ fn dep_kinds() {
37973777
"pkg": "bar 0.1.0 [..]",
37983778
"dep_kinds": [
37993779
{
3800-
"extern_name": "bar",
38013780
"kind": null,
38023781
"target": null
38033782
},
38043783
{
3805-
"extern_name": "bar",
38063784
"kind": "dev",
38073785
"target": null
38083786
},
38093787
{
3810-
"extern_name": "bar",
38113788
"kind": "build",
38123789
"target": null
38133790
}
@@ -3818,7 +3795,6 @@ fn dep_kinds() {
38183795
"pkg": "winapi 0.1.0 [..]",
38193796
"dep_kinds": [
38203797
{
3821-
"extern_name": "winapi",
38223798
"kind": null,
38233799
"target": "cfg(windows)"
38243800
}
@@ -3909,7 +3885,6 @@ fn dep_kinds_workspace() {
39093885
"pkg": "foo 0.1.0 (path+file://[..]/foo)",
39103886
"dep_kinds": [
39113887
{
3912-
"extern_name": "foo",
39133888
"kind": null,
39143889
"target": null
39153890
}
@@ -3935,7 +3910,6 @@ fn dep_kinds_workspace() {
39353910
"pkg": "dep 0.5.0 (path+file://[..]/foo/dep)",
39363911
"dep_kinds": [
39373912
{
3938-
"extern_name": "dep",
39393913
"kind": null,
39403914
"target": null
39413915
}

tests/testsuite/update.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ fn update_precise_first_run() {
608608
{
609609
"dep_kinds": [
610610
{
611-
"extern_name": "serde",
612611
"kind": null,
613612
"target": null
614613
}

0 commit comments

Comments
 (0)