diff --git a/cargo/cargo_build_script_runner/bin.rs b/cargo/cargo_build_script_runner/bin.rs index 5c3efcb23b..5f43f27983 100644 --- a/cargo/cargo_build_script_runner/bin.rs +++ b/cargo/cargo_build_script_runner/bin.rs @@ -103,7 +103,10 @@ fn run_buildrs() -> Result<(), String> { // Bazel does not support byte strings so in order to correctly represent `CARGO_ENCODED_RUSTFLAGS` // the escaped `\x1f` sequences need to be unescaped if let Ok(encoded_rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") { - command.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags.replace("\\x1f", "\x1f")); + command.env( + "CARGO_ENCODED_RUSTFLAGS", + encoded_rustflags.replace("\\x1f", "\x1f"), + ); } if let Some(ar_path) = env::var_os("AR") { diff --git a/test/cargo_build_script/BUILD.bazel b/test/cargo_build_script/BUILD.bazel index 84fc960be2..34744cbabd 100644 --- a/test/cargo_build_script/BUILD.bazel +++ b/test/cargo_build_script/BUILD.bazel @@ -8,6 +8,8 @@ cargo_build_script( name = "tools_exec_build_rs", srcs = ["build.rs"], build_script_env = {"TOOL": "$(execpath :tool)"}, + # Add a flag to test that they're exposed to the build script + rustc_flags = ["--verbose"], tools = [":tool"], ) diff --git a/test/cargo_build_script/build.rs b/test/cargo_build_script/build.rs index 68cef72321..c17b38a7ba 100644 --- a/test/cargo_build_script/build.rs +++ b/test/cargo_build_script/build.rs @@ -1,4 +1,24 @@ +//! A Cargo build script binary used in unit tests for the Bazel `cargo_build_script` rule + +/// `cargo_build_script` should always set `CARGO_ENCODED_RUSTFLAGS` +fn test_encoded_rustflags() { + let encoded_rustflags = std::env::var("CARGO_ENCODED_RUSTFLAGS").unwrap(); + + let flags: Vec = encoded_rustflags + .split('\x1f') + .map(str::to_string) + .collect(); + assert_eq!(flags.len(), 2); + + assert!(flags[0].starts_with("--sysroot")); + assert!(flags[1].starts_with("--verbose")); +} + fn main() { + + // Perform some unit testing + test_encoded_rustflags(); + // Pass the TOOL_PATH along to the rust_test so we can assert on it. println!( "cargo:rustc-env=TOOL_PATH={}",