Skip to content

Commit 1e5bad3

Browse files
committed
Auto merge of #14593 - elchukc:fix_cargo_tree_bindep_crosscompile, r=weihanglo
Fix panic when running cargo tree on a package with a cross compiled bindep ### What does this PR try to resolve? This is an attempt to close out `@rukai's` [PR](#13207 (comment)) for #12358 and #10593 by adjusting the new integration test and resolving merge conflicts. I have also separated the changes into atomic commits as per [previous review](#13207 (comment)). ### How should we test and review this PR? The integration test that has been edited here is sufficient, plus the new integration test that confirms a more specific case where `cargo tree` throws an error. ### Additional information I have confirmed the test `artifact_dep_target_specified` fails on master branch and succeeds on this branch. The first commit fixes the panic and the integration test. Commits 2 and 3 add other tests that confirm behaviour mentioned in related issues. Commits: 1. [Fix panic when running cargo tree on a package with a cross compiled bindep](5c5ea78) - fixes some panics and changes the integration test to succeed 2. [test: cargo tree panic on artifact dep target deactivated](ed294ab) - adds test to confirm the behaviour for the specific panic from [#10539 (comment)](#10593 (comment))
2 parents 1ea02ff + ed294ab commit 1e5bad3

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

src/cargo/ops/tree/graph.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,32 @@ 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+
// Get the information of the dependent crate from `features_for`.
402+
// If a dependent crate is
403+
//
404+
// * specified as an artifact dep with a `target`, or
405+
// * a host dep,
406+
//
407+
// its transitive deps, including build-deps, need to be built on that target.
408+
None if features_for != FeaturesFor::default() => features_for,
409+
// Dependent crate is a normal dep, then back to old rules:
410+
//
411+
// * normal deps, dev-deps -> inherited target
412+
// * build-deps -> host
413+
None => {
414+
if dep.is_build() || dep_pkg.proc_macro() {
415+
FeaturesFor::HostDep
416+
} else {
417+
features_for
418+
}
419+
}
398420
};
399421
let dep_index = add_pkg(
400422
graph,

tests/testsuite/artifact_dep.rs

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,14 +1568,93 @@ fn artifact_dep_target_specified() {
15681568
.with_status(0)
15691569
.run();
15701570

1571-
// TODO: This command currently fails due to a bug in cargo but it should be fixed so that it succeeds in the future.
15721571
p.cargo("tree -Z bindeps")
15731572
.masquerade_as_nightly_cargo(&["bindeps"])
1574-
.with_stdout_data("")
1575-
.with_stderr_data(r#"...
1576-
[..]did not find features for (PackageId { name: "bindep", version: "0.0.0", source: "[..]" }, NormalOrDev) within activated_features:[..]
1573+
.with_stdout_data(str![[r#"
1574+
foo v0.0.0 ([ROOT]/foo)
1575+
└── bindep v0.0.0 ([ROOT]/foo/bindep)
1576+
1577+
"#]])
1578+
.with_status(0)
1579+
.run();
1580+
}
1581+
1582+
/// From issue #10593
1583+
/// The case where:
1584+
/// * artifact dep is { target = <specified> }
1585+
/// * dependency of that artifact dependency specifies the same target
1586+
/// * the target is not activated.
1587+
#[cargo_test]
1588+
fn dep_of_artifact_dep_same_target_specified() {
1589+
if cross_compile::disabled() {
1590+
return;
1591+
}
1592+
let target = cross_compile::alternate();
1593+
let p = project()
1594+
.file(
1595+
"Cargo.toml",
1596+
&format!(
1597+
r#"
1598+
[package]
1599+
name = "foo"
1600+
version = "0.1.0"
1601+
edition = "2015"
1602+
resolver = "2"
1603+
1604+
[dependencies]
1605+
bar = {{ path = "bar", artifact = "bin", target = "{target}" }}
1606+
"#,
1607+
),
1608+
)
1609+
.file("src/lib.rs", "")
1610+
.file(
1611+
"bar/Cargo.toml",
1612+
&format!(
1613+
r#"
1614+
[package]
1615+
name = "bar"
1616+
version = "0.1.0"
1617+
1618+
[target.{target}.dependencies]
1619+
baz = {{ path = "../baz" }}
1620+
"#,
1621+
),
1622+
)
1623+
.file("bar/src/main.rs", "fn main() {}")
1624+
.file(
1625+
"baz/Cargo.toml",
1626+
r#"
1627+
[package]
1628+
name = "baz"
1629+
version = "0.1.0"
1630+
1631+
"#,
1632+
)
1633+
.file("baz/src/lib.rs", "")
1634+
.build();
1635+
1636+
p.cargo("check -Z bindeps")
1637+
.masquerade_as_nightly_cargo(&["bindeps"])
1638+
.with_stderr_data(str![[r#"
1639+
[LOCKING] 2 packages to latest compatible versions
1640+
[COMPILING] baz v0.1.0 ([ROOT]/foo/baz)
1641+
[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
1642+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
1643+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1644+
1645+
"#]])
1646+
.with_status(0)
1647+
.run();
1648+
1649+
// TODO This command currently fails due to a bug in cargo but it should be fixed so that it succeeds in the future.
1650+
p.cargo("tree -Z bindeps")
1651+
.masquerade_as_nightly_cargo(&["bindeps"])
1652+
.with_stderr_data(
1653+
r#"...
1654+
no entry found for key
15771655
...
1578-
"#)
1656+
"#,
1657+
)
15791658
.with_status(101)
15801659
.run();
15811660
}

0 commit comments

Comments
 (0)