-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Set TMPDIR / TMP+TEMP environment variables
#16426
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
base: master
Are you sure you want to change the base?
Conversation
When invoking rustc, rustdoc and build scripts, set `TMP` and `TEMP` (on Windows) or `TMPDIR` (everywhere else) to a project-local temporary directory.
32d48a7 to
57a906e
Compare
| /// Returns the directory which build scripts should use for temporary | ||
| /// files. | ||
| /// `/path/to/target/{debug,release}/build/PKG-HASH/tmp` | ||
| pub fn build_script_tmp_dir(&self, unit: &Unit) -> PathBuf { | ||
| self.build_script_run_dir(unit).join("tmp") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option here would be to simply use self.build_script_run_dir(unit) given that people using std::env::temp_dir() already know that they have to generate a unique name for anything they want to write there.
|
Note that our contribution process asks that things like this be discussed and approved in issues before starting work on PRs. |
Right, sorry. Do you want me to open an issue so we can discuss it there first? |
|
Yes. |
|
I opened #16427, and marked this PR as a draft. |
Fixes #16427.
What does this PR try to resolve?
Build scripts, procedural macros, the linker and even the compiler itself1 may output temporary files to
std::env::temp_dir()during compilation. It is possible to request that programs use a different temporary directory by setting theTMPDIR(Unix) orTMPandTEMP(Windows) environment variables.This PR makes Cargo set these environment variables to a temporary directory within the
targetdirectory.This is useful when debugging, as it allows more easily inspecting the state the compiler was working with when something went wrong, especially if using
-Csave-temps=yes, as relevant files can more easily be found in the project directory instead of being bundled in/tmpwith everything else. It also makes it easier to clean up after compilation is finished (cargo cleanwould remove temporary files as well, instead of the user having to reboot for/tmpto be cleared).Additionally, this is useful for preventing information leakage:
/tmpis global and world readable2, and this means that a different user on the same machine could figure out possibly sensitive details about the project you were building. This is even an issue on macOS where the temporary directory is scoped to the current user (getconf DARWIN_USER_TEMP_DIR), since you'd still be susceptible to Man-in-the-middle attacks by less privileged processes by the same user.Finally, this makes build scripts and proc-macros more self-contained, which is useful when sandboxing them, such as that which I'm working on in
cargo-sandbox.Implementation notes
When running build scripts, the temporary directory we request is
/path/to/target/{debug,release}/build/PKG-HASH/tmp(same as$OUT_DIR/../tmp). Forrustc/rustdocinvocations, the directory we request is/path/to/target/tmp(same as$CARGO_TARGET_TMPDIR), see #9814 for breakage introduced in the past by trying to make it/path/to/target/{debug,release}/tmp.I had a look at the standard library's implementation of
std::env::temp_dir, it seems that only Hermit and Motor OS don't respect any environment variable when considering the temporary directory, so the test that I've added should work on all host platforms but those (I'd argue the onus is on those niche platforms to figure out a standard environment variable that they want to use for this, but I can submit bugs upstream if you want me to?)How to test and review this PR?
cargo rustc -- -Csave-temps=yes, and verify that thesymbols.ofile shows up in the project-local temporary directory.Footnotes
Cargo has a pretty good understanding of which files the compiler outputs, though it is incomplete in certain areas, especially areas that the compiler doesn't (yet) want to make public, such as the
symbols.otrick. Such files are placed in the temporary directory. ↩Though often only writable by the same user that created the file. ↩