Skip to content

Commit 04e4443

Browse files
authored
Eagerly close tempfile to fix #1082 (#1087)
1 parent 78ee75b commit 04e4443

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/tempfile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl NamedTempfile {
6969
&self.path
7070
}
7171

72-
pub(super) fn file(&self) -> &File {
73-
self.file.as_ref().unwrap()
72+
pub(super) fn take_file(&mut self) -> Option<File> {
73+
self.file.take()
7474
}
7575
}
7676

src/tool.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Tool {
122122
.into(),
123123
})?;
124124

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

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

0 commit comments

Comments
 (0)