Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly close tempfile to fix #1082 #1087

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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