From 067e9a61e87608b90b59a04668b532b0e7819e5b Mon Sep 17 00:00:00 2001 From: Chili Palmer Date: Wed, 19 Aug 2026 21:26:39 +0200 Subject: [PATCH] Run the debug CLI without Cargo loader paths - Add the Rust target library directory to the Linux debug CLI rpath - Test that the built CLI runs without `LD_LIBRARY_PATH` --- crates/bds-cli/build.rs | 30 ++++++++++++++++++++++++++++++ crates/bds-cli/tests/process.rs | 17 +++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 crates/bds-cli/build.rs diff --git a/crates/bds-cli/build.rs b/crates/bds-cli/build.rs new file mode 100644 index 0000000..1364026 --- /dev/null +++ b/crates/bds-cli/build.rs @@ -0,0 +1,30 @@ +use std::{env, process::Command}; + +fn main() { + println!("cargo:rerun-if-env-changed=RUSTC"); + + if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("linux") + || env::var("PROFILE").as_deref() != Ok("debug") + { + return; + } + + let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()); + let target = env::var("TARGET").expect("Cargo must provide the build target"); + let output = Command::new(rustc) + .args(["--target", &target, "--print", "target-libdir"]) + .output() + .expect("query Rust target library directory"); + assert!( + output.status.success(), + "rustc --print target-libdir failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + let target_libdir = + String::from_utf8(output.stdout).expect("Rust target library directory must be UTF-8"); + + println!( + "cargo:rustc-link-arg-bin=bds-cli=-Wl,-rpath,{}", + target_libdir.trim() + ); +} diff --git a/crates/bds-cli/tests/process.rs b/crates/bds-cli/tests/process.rs index e498b09..c60f51e 100644 --- a/crates/bds-cli/tests/process.rs +++ b/crates/bds-cli/tests/process.rs @@ -8,6 +8,23 @@ fn cli(home: &std::path::Path, args: &[&str]) -> std::process::Output { .unwrap() } +#[cfg(target_os = "linux")] +#[test] +fn built_cli_runs_without_cargo_loader_paths() { + let output = Command::new(env!("CARGO_BIN_EXE_bds-cli")) + .env_remove("LD_LIBRARY_PATH") + .arg("--help") + .output() + .unwrap(); + + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(String::from_utf8_lossy(&output.stdout).contains("rebuild")); +} + #[test] fn process_exit_codes_help_and_shared_state_roundtrip() { let root = tempfile::tempdir().unwrap();