Skip to content

Commit

Permalink
Eagerly close tempfile to fix #1082 (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanna-kruppe committed Jun 8, 2024
1 parent 78ee75b commit 04e4443
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl NamedTempfile {
&self.path
}

pub(super) fn file(&self) -> &File {
self.file.as_ref().unwrap()
pub(super) fn take_file(&mut self) -> Option<File> {
self.file.take()
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Tool {
.into(),
})?;

let tmp =
let mut tmp =
NamedTempfile::new(&out_dir, "detect_compiler_family.c").map_err(|err| Error {
kind: ErrorKind::IOError,
message: format!(
Expand All @@ -132,8 +132,13 @@ impl Tool {
)
.into(),
})?;
tmp.file()
.write_all(include_bytes!("detect_compiler_family.c"))?;
let mut tmp_file = tmp.take_file().unwrap();
tmp_file.write_all(include_bytes!("detect_compiler_family.c"))?;
// Close the file handle *now*, otherwise the compiler may fail to open it on Windows
// (#1082). The file stays on disk and its path remains valid until `tmp` is dropped.
tmp_file.flush()?;
tmp_file.sync_data()?;
drop(tmp_file);

let stdout = run_output(
Command::new(path).arg("-E").arg(tmp.path()),
Expand Down

0 comments on commit 04e4443

Please sign in to comment.