diff --git a/src/tempfile.rs b/src/tempfile.rs index ab0ac8774..d9df2c227 100644 --- a/src/tempfile.rs +++ b/src/tempfile.rs @@ -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 { + self.file.take() } } diff --git a/src/tool.rs b/src/tool.rs index ea0f36fad..facc258d0 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -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!( @@ -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()),