Skip to content

Commit 8ab6462

Browse files
committed
Fix panic when running cargo tree on a package with a cross compiled bindep
1 parent 4e792c2 commit 8ab6462

File tree

3 files changed

+89
-30
lines changed

3 files changed

+89
-30
lines changed

src/cargo/core/resolver/features.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,32 @@ impl ResolvedFeatures {
319319
pkg_id: PackageId,
320320
features_for: FeaturesFor,
321321
) -> Vec<InternedString> {
322-
self.activated_features_int(pkg_id, features_for)
323-
.expect("activated_features for invalid package")
322+
let fk = features_for.apply_opts(&self.opts);
323+
let key = (pkg_id, fk);
324+
if let Some(fs) = self.activated_features.get(&key) {
325+
fs.iter().cloned().collect()
326+
} else {
327+
panic!(
328+
"did not find features for {key:?} within activated_features:\n{:#?}",
329+
self.activated_features
330+
)
331+
}
332+
}
333+
334+
/// Variant of `activated_features` that returns `None` if this is
335+
/// not a valid pkg_id/is_build combination. Used in places which do
336+
/// not know which packages are activated (like `cargo clean`).
337+
pub fn activated_features_unverified(
338+
&self,
339+
pkg_id: PackageId,
340+
features_for: FeaturesFor,
341+
) -> Option<Vec<InternedString>> {
342+
let fk = features_for.apply_opts(&self.opts);
343+
if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) {
344+
Some(fs.iter().cloned().collect())
345+
} else {
346+
None
347+
}
324348
}
325349

326350
/// Returns if the given dependency should be included.
@@ -340,30 +364,6 @@ impl ResolvedFeatures {
340364
.unwrap_or(false)
341365
}
342366

343-
/// Variant of `activated_features` that returns `None` if this is
344-
/// not a valid pkg_id/is_build combination. Used in places which do
345-
/// not know which packages are activated (like `cargo clean`).
346-
pub fn activated_features_unverified(
347-
&self,
348-
pkg_id: PackageId,
349-
features_for: FeaturesFor,
350-
) -> Option<Vec<InternedString>> {
351-
self.activated_features_int(pkg_id, features_for).ok()
352-
}
353-
354-
fn activated_features_int(
355-
&self,
356-
pkg_id: PackageId,
357-
features_for: FeaturesFor,
358-
) -> CargoResult<Vec<InternedString>> {
359-
let fk = features_for.apply_opts(&self.opts);
360-
if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) {
361-
Ok(fs.iter().cloned().collect())
362-
} else {
363-
bail!("features did not find {:?} {:?}", pkg_id, fk)
364-
}
365-
}
366-
367367
/// Compares the result against the original resolver behavior.
368368
///
369369
/// Used by `cargo fix --edition` to display any differences.

src/cargo/ops/tree/graph.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,20 @@ fn add_pkg(
391391
let dep_pkg = graph.package_map[&dep_id];
392392

393393
for dep in deps {
394-
let dep_features_for = if dep.is_build() || dep_pkg.proc_macro() {
395-
FeaturesFor::HostDep
396-
} else {
397-
features_for
394+
let dep_features_for = match dep
395+
.artifact()
396+
.and_then(|artifact| artifact.target())
397+
.and_then(|target| target.to_resolved_compile_target(requested_kind))
398+
{
399+
// Dependency has a `{ …, target = <triple> }`
400+
Some(target) => FeaturesFor::ArtifactDep(target),
401+
None => {
402+
if dep.is_build() || dep_pkg.proc_macro() {
403+
FeaturesFor::HostDep
404+
} else {
405+
features_for
406+
}
407+
}
398408
};
399409
let dep_index = add_pkg(
400410
graph,

tests/testsuite/artifact_dep.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,55 @@ foo v0.0.0 ([CWD])
14451445
)
14461446
.run();
14471447
}
1448+
1449+
#[cargo_test]
1450+
fn artifact_dep_target_specified() {
1451+
if cross_compile::disabled() {
1452+
return;
1453+
}
1454+
let target = cross_compile::alternate();
1455+
1456+
let p = project()
1457+
.file(
1458+
"Cargo.toml",
1459+
&r#"
1460+
[package]
1461+
name = "foo"
1462+
version = "0.0.0"
1463+
authors = []
1464+
resolver = "2"
1465+
1466+
[dependencies]
1467+
bindep = { path = "bindep", artifact = "bin", target = "$TARGET" }
1468+
"#
1469+
.replace("$TARGET", target),
1470+
)
1471+
.file("src/lib.rs", "")
1472+
.file("bindep/Cargo.toml", &basic_manifest("bindep", "0.0.0"))
1473+
.file("bindep/src/main.rs", "fn main() {}")
1474+
.build();
1475+
1476+
p.cargo("check -Z bindeps")
1477+
.masquerade_as_nightly_cargo(&["bindeps"])
1478+
.with_stderr_contains(
1479+
r#"[COMPILING] bindep v0.0.0 ([CWD]/bindep)
1480+
[CHECKING] foo v0.0.0 ([CWD])
1481+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]"#,
1482+
)
1483+
.with_status(0)
1484+
.run();
1485+
1486+
p.cargo("tree -Z bindeps")
1487+
.masquerade_as_nightly_cargo(&["bindeps"])
1488+
.with_stdout(
1489+
"\
1490+
foo v0.0.0 ([CWD])
1491+
└── bindep v0.0.0 ([CWD]/bindep)",
1492+
)
1493+
.with_status(0)
1494+
.run();
1495+
}
1496+
14481497
#[cargo_test]
14491498
fn targets_are_picked_up_from_non_workspace_artifact_deps() {
14501499
if cross_compile::disabled() {

0 commit comments

Comments
 (0)