From b1928bac4cb832c539957596a80d7418761c8d3f Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 14 May 2025 14:25:47 -0700 Subject: [PATCH] Remove probe output files --- src/lib.rs | 14 ++++++++++++-- tests/tests.rs | 13 +++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fc3c59b..bf2d3f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -272,10 +272,11 @@ impl AutoCfg { } fn probe_fmt<'a>(&self, source: Arguments<'a>) -> Result<(), Error> { + let crate_name = self.new_crate_name(); let mut command = self.rustc.command(); command .arg("--crate-name") - .arg(self.new_crate_name()) + .arg(&crate_name) .arg("--crate-type=lib") .arg("--out-dir") .arg(&self.out_dir) @@ -295,7 +296,16 @@ impl AutoCfg { drop(stdin); match child.wait() { - Ok(status) if status.success() => Ok(()), + Ok(status) if status.success() => { + // Try to remove the output file so it doesn't look like a build product for + // systems like bazel -- but this is best-effort, so we can ignore failure. + // The probe itself is already considered successful at this point. + let mut file = self.out_dir.join(crate_name); + file.set_extension("ll"); + let _ = fs::remove_file(file); + + Ok(()) + } Ok(status) => Err(error::from_exit(status)), Err(error) => Err(error::from_io(error)), } diff --git a/tests/tests.rs b/tests/tests.rs index de2b18c..8948d32 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -136,3 +136,16 @@ fn probe_raw() { .probe_raw(&f("#![deny(dead_code)] pub fn x() {}")) .is_ok()); } + +#[test] +fn probe_cleanup() { + let dir = support::out_dir().join("autocfg_test_probe_cleanup"); + std::fs::create_dir(&dir).unwrap(); + + let ac = AutoCfg::with_dir(&dir).unwrap(); + assert!(ac.probe_type("i32")); + + // NB: this is not `remove_dir_all`, so it will only work if the directory + // is empty -- i.e. the probe should have removed any output files. + std::fs::remove_dir(&dir).unwrap(); +}