Skip to content

Commit 42612cc

Browse files
committed
fix(tree): use artifact target feature key
1 parent 593086c commit 42612cc

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/cargo/ops/tree/graph.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Code for building the graph used by `cargo tree`.
22
33
use super::TreeOptions;
4-
use crate::core::compiler::{CompileKind, RustcTargetData};
4+
use crate::core::compiler::{CompileKind, CompileTarget, RustcTargetData};
55
use crate::core::dependency::DepKind;
66
use crate::core::resolver::Resolve;
77
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
@@ -486,25 +486,24 @@ fn add_pkg(
486486
let dep_pkg = graph.package_map[&dep_id];
487487

488488
for dep in deps {
489-
let dep_features_for = match dep
490-
.artifact()
491-
.and_then(|artifact| artifact.target())
492-
.and_then(|target| target.to_resolved_compile_target(requested_kind))
493-
{
494-
// Dependency has a `{ …, target = <triple> }`
495-
Some(target) => FeaturesFor::ArtifactDep(target),
496-
// Get the information of the dependent crate from `features_for`.
497-
// If a dependent crate is
498-
//
499-
// * specified as an artifact dep with a `target`, or
500-
// * a host dep,
501-
//
502-
// its transitive deps, including build-deps, need to be built on that target.
503-
None if features_for != FeaturesFor::default() => features_for,
504-
// Dependent crate is a normal dep, then back to old rules:
505-
//
506-
// * normal deps, dev-deps -> inherited target
507-
// * build-deps -> host
489+
// Keep this in sync with `FeatureResolver::deps`: `cargo tree`
490+
// looks up already-resolved features, so it must use the same key.
491+
let dep_features_for = match dep.artifact().and_then(|artifact| artifact.target()) {
492+
// `target = "target"` with a host requested kind is keyed as
493+
// `ArtifactDep(<host triple>)`, not `HostDep`.
494+
Some(artifact_target) => {
495+
match artifact_target.to_resolved_compile_kind(requested_kind) {
496+
CompileKind::Target(target) => FeaturesFor::ArtifactDep(target),
497+
CompileKind::Host => {
498+
let host = CompileTarget::new(
499+
&target_data.rustc.host,
500+
target_data.gctx.cli_unstable().json_target_spec,
501+
)
502+
.expect("host target triple is always valid");
503+
FeaturesFor::ArtifactDep(host)
504+
}
505+
}
506+
}
508507
None => {
509508
if dep.is_build() || dep_pkg.proc_macro() {
510509
FeaturesFor::HostDep

tests/testsuite/artifact_dep.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,10 +1604,13 @@ fn tree_with_build_artifact_dep_inheriting_target() {
16041604

16051605
p.cargo("tree -Z bindeps")
16061606
.masquerade_as_nightly_cargo(&["bindeps"])
1607-
.with_status(101)
1608-
.with_stderr_contains(
1609-
"did not find features for (PackageId { name: \"bar\", version: \"0.5.0\", source: [..] }, HostDep) within activated_features:",
1610-
)
1607+
.with_stdout_data(str![[r#"
1608+
foo v0.0.0 ([ROOT]/foo)
1609+
[build-dependencies]
1610+
└── bar v0.5.0 ([ROOT]/foo/bar)
1611+
└── pm v0.1.0 (proc-macro) ([ROOT]/foo/pm)
1612+
1613+
"#]])
16111614
.run();
16121615
}
16131616

0 commit comments

Comments
 (0)