Skip to content

Commit 6a192d8

Browse files
authored
prevent env vars leaking into install (#97)
* prevent env vars from leaking into install * fix clippy * fix clippy in test code
1 parent e574460 commit 6a192d8

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

crates/cargo-gpu/src/install.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::target_specs::update_target_specs_files;
77
use crate::{cache_dir, spirv_source::SpirvSource};
88
use anyhow::Context as _;
99
use spirv_builder::SpirvBuilder;
10+
use std::env;
1011
use std::path::{Path, PathBuf};
1112

1213
/// Represents a functional backend installation, whether it was cached or just installed.
@@ -279,20 +280,33 @@ package = "rustc_codegen_spirv"
279280
}
280281

281282
crate::user_output!("Compiling `rustc_codegen_spirv` from source {}\n", source,);
282-
let mut build_command = std::process::Command::new("cargo");
283-
build_command
283+
let mut cargo = std::process::Command::new("cargo");
284+
cargo
284285
.current_dir(&install_dir)
285286
.arg(format!("+{toolchain_channel}"))
286-
.args(["build", "--release"])
287-
.env_remove("RUSTC")
288-
.env_remove("RUSTFLAGS");
287+
.args(["build", "--release"]);
289288
if source.is_path() {
290-
build_command.args(["-p", "rustc_codegen_spirv", "--lib"]);
289+
cargo.args(["-p", "rustc_codegen_spirv", "--lib"]);
291290
}
292291

293-
log::debug!("building artifacts with `{build_command:?}`");
292+
// Clear Cargo environment variables that we don't want to leak into the
293+
// inner invocation of Cargo and mess with our `rustc_codegen_spirv` build.
294+
for (key, _) in env::vars_os() {
295+
let remove = key.to_str().is_some_and(|st| {
296+
st.starts_with("CARGO_FEATURES_") || st.starts_with("CARGO_CFG_")
297+
});
298+
if remove {
299+
cargo.env_remove(key);
300+
}
301+
}
302+
cargo
303+
.env_remove("RUSTC")
304+
.env_remove("RUSTFLAGS")
305+
// ignore any externally supplied target dir, we want to build it in our cache dir
306+
.env_remove("CARGO_TARGET_DIR");
294307

295-
build_command
308+
log::debug!("building artifacts with `{cargo:?}`");
309+
cargo
296310
.stdout(std::process::Stdio::inherit())
297311
.stderr(std::process::Stdio::inherit())
298312
.output()

crates/cargo-gpu/src/spirv_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ mod test {
294294
url: "https://github.com/Rust-GPU/rust-gpu".to_owned(),
295295
rev: "86fc4803".to_owned(),
296296
}
297-
)
297+
);
298298
}
299299

300300
#[test_log::test]
@@ -309,15 +309,15 @@ mod test {
309309
url: "https://github.com/Rust-GPU/rust-gpu.git".to_owned(),
310310
rev: "6e2c84d4fe64e32df4c060c5a7f3e35a32e45421".to_owned(),
311311
}
312-
)
312+
);
313313
}
314314

315315
fn parse_git(source: &str) -> SpirvSource {
316316
let package = PackageBuilder::new(
317317
"spirv-std",
318318
Version::new(0, 9, 0),
319319
PackageId {
320-
repr: "".to_owned(),
320+
repr: String::new(),
321321
},
322322
"",
323323
)

0 commit comments

Comments
 (0)