Skip to content

Commit 96a2a7a

Browse files
EliahKaganclaude
andcommitted
Use the gix binary's embedded target_triple rather than spawning rustc
`tests/licenses_parity.rs` used `Command::new("rustc").arg("-vV")` parsed for its `host:` line to pick the `--filter-platform` value for the cargo-metadata probe. That was wrong in two ways: * It hardcoded the command `rustc` literally — whichever compiler happens to be first on `PATH` — not the compiler this cargo invocation actually resolved. A rustup override, `cargo +nightly`, or a custom `RUSTC` setting at the cargo level would silently diverge the test from the compiler that built the binary under test. * The fix that worked for `build.rs` (`std::env::var("RUSTC")`) doesn't apply here: cargo sets `RUSTC` for build scripts but not for integration tests. Empirically `std::env::var_os("RUSTC")` returns `None` inside a running test binary. Switch to reading `target_triple` from the binary's own `gix licenses --format json` output. `build.rs` populates that field from the `TARGET` env var cargo sets for the build script, so it is the authoritative record of what target the binary under test was built for. No subprocess to `rustc` is needed at all; the `gix` binary is already being spawned for the crate-set probe further down in the same test. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2aff1e1 commit 96a2a7a

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

tests/licenses_parity.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,40 @@
2424
use std::collections::BTreeSet;
2525
use std::process::Command;
2626

27-
/// Host triple as reported by the `rustc` this `cargo test` is using.
28-
fn host_target() -> String {
29-
let out = Command::new("rustc").arg("-vV").output().expect("run `rustc -vV`");
30-
let text = String::from_utf8(out.stdout).expect("rustc output is UTF-8");
31-
match text.lines().find_map(|l| l.strip_prefix("host: ")) {
32-
Some(host) => host.trim().to_string(),
33-
None => panic!("no `host:` line in rustc -vV output:\n{text}"),
34-
}
27+
/// Target triple the built `gix` binary was compiled for, as self-reported
28+
/// by its embedded manifest (`build.rs` populates this from the `TARGET`
29+
/// env var cargo sets for build scripts).
30+
///
31+
/// Asking the binary itself — rather than spawning `rustc -vV` — avoids
32+
/// two failure modes: (1) `rustc` on `PATH` may not be the compiler the
33+
/// current cargo invocation resolved, and (2) the `RUSTC` env var
34+
/// cargo exports to build scripts is *not* exported to integration
35+
/// tests, so there is no reliable way to look it up here. The binary's
36+
/// embedded `target_triple` is the authoritative answer because it was
37+
/// recorded at build time by the same cargo run that produced the
38+
/// binary under test.
39+
fn target_triple_of_gix() -> String {
40+
let gix = env!("CARGO_BIN_EXE_gix");
41+
let output = Command::new(gix)
42+
.args(["licenses", "--format", "json"])
43+
.output()
44+
.expect("run `gix licenses --format json`");
45+
assert!(
46+
output.status.success(),
47+
"`gix licenses --format json` failed ({}):\nstderr: {}",
48+
output.status,
49+
String::from_utf8_lossy(&output.stderr),
50+
);
51+
let data: serde_json::Value = serde_json::from_slice(&output.stdout).expect("parse `gix licenses` JSON");
52+
let triple = data["target_triple"]
53+
.as_str()
54+
.expect("manifest has a target_triple string")
55+
.to_string();
56+
assert!(
57+
!triple.is_empty(),
58+
"gix binary reports an empty target_triple; build.rs's TARGET passthrough may have regressed",
59+
);
60+
triple
3561
}
3662

3763
/// The top-level feature-profile flags this test binary was compiled with.
@@ -281,7 +307,7 @@ fn gix_manifest_crate_names() -> BTreeSet<String> {
281307
#[test]
282308
fn third_party_crates_match_cargo_metadata() {
283309
let features = enabled_feature_profiles();
284-
let host = host_target();
310+
let host = target_triple_of_gix();
285311
let (workspace_names, from_cargo) = cargo_metadata_sets(&features, &host);
286312
let from_binary = gix_manifest_crate_names();
287313

0 commit comments

Comments
 (0)