Skip to content

Commit 4028aa7

Browse files
authored
crate_universe: error on empty host triples in cargo tree resolver (bazelbuild#3979)
When `supported_platform_triples` contained no platforms with host tools, `TreeResolver::execute_cargo_tree` would silently produce no `cargo tree` output, and `crate_universe` would generate BUILD files with empty `crate_features` and no optional dependencies. Bail with an actionable error instead, pointing the user at adding Bazel's execution platform triple to `supported_platform_triples`. Fixes bazelbuild#3907
1 parent ae1be23 commit 4028aa7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

crate_universe/src/metadata/cargo_tree_resolver.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ impl TreeResolver {
138138
.map(|n| n.get())
139139
.unwrap_or(4);
140140

141+
// Without any host triples, no `cargo tree` invocations would run and
142+
// the resulting BUILD files would silently be missing `crate_features`
143+
// and optional dependencies. Bail with an actionable message instead.
144+
if cargo_host_triples.is_empty() {
145+
bail!(
146+
"`supported_platform_triples` contains no platforms with host tools, so \
147+
`cargo tree` cannot be invoked and feature resolution cannot be performed. \
148+
Bazel's execution platform triple typically should be included in \
149+
`supported_platform_triples` (e.g. `x86_64-unknown-linux-gnu`). \
150+
target_triples=[{}]",
151+
cargo_target_triples
152+
.keys()
153+
.cloned()
154+
.collect::<Vec<_>>()
155+
.join(", "),
156+
);
157+
}
158+
141159
// Prepare all unique jobs: (cargo_host, cargo_target)
142160
let mut jobs = Vec::<(String, String)>::new();
143161
for cargo_host in cargo_host_triples.keys() {
@@ -146,6 +164,10 @@ impl TreeResolver {
146164
}
147165
}
148166

167+
if jobs.is_empty() {
168+
bail!("No `cargo tree` invocations to run: no target triples were provided.");
169+
}
170+
149171
// Spawn workers up to the cap; join one whenever the cap is reached.
150172
let mut in_flight =
151173
Vec::<thread::JoinHandle<anyhow::Result<(String, String, Output)>>>::new();
@@ -1722,6 +1744,44 @@ mod test {
17221744
);
17231745
}
17241746

1747+
#[test]
1748+
fn execute_cargo_tree_errors_when_no_host_triples() {
1749+
let (_, tempdir) =
1750+
crate::test::test_tempdir("execute_cargo_tree_errors_when_no_host_triples");
1751+
1752+
let resolver = TreeResolver::new(Cargo::new(
1753+
tempdir.join("nonexistent-cargo"),
1754+
tempdir.join("nonexistent-rustc"),
1755+
));
1756+
1757+
let target_triples: BTreeSet<TargetTriple> =
1758+
BTreeSet::from([TargetTriple::from_bazel("thumbv6m-none-eabi".to_owned())]);
1759+
let host_triples: BTreeSet<TargetTriple> = BTreeSet::new();
1760+
1761+
let err = resolver
1762+
.execute_cargo_tree(
1763+
&tempdir.join("Cargo.toml"),
1764+
&host_triples,
1765+
&target_triples,
1766+
&tempdir.join("rustc_wrapper"),
1767+
)
1768+
.expect_err("expected an error when no host triples are supplied");
1769+
1770+
let msg = err.to_string();
1771+
assert!(
1772+
msg.contains("supported_platform_triples"),
1773+
"error message should mention `supported_platform_triples`, got: {msg}",
1774+
);
1775+
assert!(
1776+
msg.contains("host tools"),
1777+
"error message should mention host tools, got: {msg}",
1778+
);
1779+
assert!(
1780+
msg.contains("thumbv6m-none-eabi"),
1781+
"error message should list the configured target triples, got: {msg}",
1782+
);
1783+
}
1784+
17251785
#[test]
17261786
fn parse_cargo_tree_output_same_level_host_deps() {
17271787
let autocfg_id = CrateId {

0 commit comments

Comments
 (0)