Skip to content

Commit 306bd5e

Browse files
committed
Avoid using env::temp when linking a binary
This keeps all build artefacts (even temporary ones) within the build directory.
1 parent eb171a2 commit 306bd5e

File tree

3 files changed

+13
-14
lines changed
  • compiler
  • tests/run-make/invalid-tmpdir-env-var

3 files changed

+13
-14
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,18 @@ pub fn link_binary(
103103
});
104104

105105
if outputs.outputs.should_link() {
106-
let tmpdir = TempDirBuilder::new()
107-
.prefix("rustc")
108-
.tempdir()
109-
.unwrap_or_else(|error| sess.dcx().emit_fatal(errors::CreateTempDir { error }));
110-
let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps);
111106
let output = out_filename(
112107
sess,
113108
crate_type,
114109
outputs,
115110
codegen_results.crate_info.local_crate_name,
116111
);
112+
let tmpdir = TempDirBuilder::new()
113+
.prefix("rustc")
114+
.tempdir_in(output.parent().unwrap_or_else(|| Path::new(".")))
115+
.unwrap_or_else(|error| sess.dcx().emit_fatal(errors::CreateTempDir { error }));
116+
let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps);
117+
117118
let crate_name = format!("{}", codegen_results.crate_info.local_crate_name);
118119
let out_filename = output.file_for_writing(
119120
outputs,

compiler/rustc_fs_util/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::{CString, OsStr};
22
use std::path::{Path, PathBuf, absolute};
3-
use std::{env, fs, io};
3+
use std::{fs, io};
44

55
use tempfile::TempDir;
66

@@ -139,8 +139,4 @@ impl<'a, 'b> TempDirBuilder<'a, 'b> {
139139
}
140140
self.builder.tempdir_in(dir)
141141
}
142-
143-
pub fn tempdir(&self) -> io::Result<TempDir> {
144-
self.tempdir_in(env::temp_dir())
145-
}
146142
}

tests/run-make/invalid-tmpdir-env-var/rmake.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//@ needs-target-std
22
//
33
// When the TMP (on Windows) or TMPDIR (on Unix) variable is set to an invalid
4-
// or non-existing directory, this used to cause an internal compiler error (ICE). After the
5-
// addition of proper error handling in #28430, this test checks that the expected message is
6-
// printed.
4+
// or non-existing directory, this used to cause an internal compiler error (ICE).
75
// See https://github.com/rust-lang/rust/issues/14698
86

97
use run_make_support::{is_windows, rustc};
@@ -18,5 +16,9 @@ fn main() {
1816
} else {
1917
rustc.env("TMPDIR", "fake");
2018
}
21-
rustc.input("foo.rs").run_fail().assert_stderr_contains("couldn't create a temp dir");
19+
let result = rustc.input("foo.rs").run_unchecked();
20+
// Ensure that rustc doesn't ICE.
21+
if !result.status().success() {
22+
result.assert_stderr_not_contains("internal compiler error");
23+
}
2224
}

0 commit comments

Comments
 (0)