Skip to content

Commit 100b84c

Browse files
snthCopilot
andcommitted
fix(ci): pass --target and --target-dir in on-demand prqlc build
When CI runs tests with --target=<triple>, cargo places artifacts in target/<triple>/debug/ instead of target/debug/. The on-demand build in test_utils::prqlc_bin_path() was running `cargo build --bin prqlc` without --target, so the binary landed in the wrong directory and insta-cmd tests could not find it. Fix by: - Emitting PRQLC_BUILD_TARGET from build.rs (the TARGET env var is only available in build scripts) - Detecting the target triple from the test binary path and passing --target to the on-demand build when needed - Also passing --target-dir to handle custom target directories like those used by cargo-llvm-cov Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bb073fd commit 100b84c

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

prqlc/prqlc-cli/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ use vergen_gitcl::{Emitter, GitclBuilder as GitBuilder};
77
pub fn main() -> Result<(), Box<dyn Error>> {
88
let git = GitBuilder::default().describe(true, true, None).build()?;
99
Emitter::default().add_instructions(&git)?.emit()?;
10+
11+
// Expose the target triple to the main crate so test_utils can pass the
12+
// correct --target flag when building the prqlc binary on demand.
13+
println!(
14+
"cargo:rustc-env=PRQLC_BUILD_TARGET={}",
15+
std::env::var("TARGET").unwrap()
16+
);
17+
1018
Ok(())
1119
}

prqlc/prqlc-cli/src/cli/test_utils.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ fn prqlc_bin_path() -> PathBuf {
2424
}
2525

2626
// Locate the target directory from the test binary path.
27+
// With --target: target/<triple>/debug/deps/prqlc-<hash>
28+
// Without: target/debug/deps/prqlc-<hash>
2729
let test_bin = std::env::current_exe().expect("cannot determine test binary path");
2830
let mut dir = test_bin.parent().unwrap().to_path_buf();
2931
if dir.ends_with("deps") {
@@ -38,8 +40,38 @@ fn prqlc_bin_path() -> PathBuf {
3840
if !bin_path.exists() {
3941
BUILD_PRQLC.call_once(|| {
4042
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
41-
let status = Command::new(cargo)
42-
.args(["build", "--bin", "prqlc"])
43+
let mut cmd = Command::new(cargo);
44+
cmd.args(["build", "--bin", "prqlc"]);
45+
46+
// When tests are compiled with an explicit --target flag, artifacts
47+
// go into target/<triple>/debug/ instead of target/debug/. Detect
48+
// this and pass the same --target so the binary lands where we
49+
// expect it. Also handle custom --target-dir (e.g. cargo-llvm-cov).
50+
//
51+
// Path layouts we handle:
52+
// target/debug/ (default)
53+
// target/<triple>/debug/ (--target)
54+
// <custom-target-dir>/debug/ (--target-dir)
55+
// <custom-target-dir>/<triple>/debug/ (--target + --target-dir)
56+
let compile_target = env!("PRQLC_BUILD_TARGET");
57+
if let Some(parent) = dir.parent() {
58+
let is_target_dir =
59+
parent.file_name().and_then(|n| n.to_str()) == Some(compile_target);
60+
61+
if is_target_dir {
62+
cmd.args(["--target", compile_target]);
63+
// The target-dir root is the grandparent.
64+
if let Some(target_dir) = parent.parent() {
65+
cmd.arg("--target-dir").arg(target_dir);
66+
}
67+
} else {
68+
// No explicit --target, but may be a custom --target-dir
69+
// (e.g. cargo-llvm-cov uses target/llvm-cov-target/).
70+
cmd.arg("--target-dir").arg(parent);
71+
}
72+
}
73+
74+
let status = cmd
4375
.status()
4476
.expect("failed to run `cargo build --bin prqlc`");
4577
assert!(status.success(), "failed to build prqlc binary");

0 commit comments

Comments
 (0)