rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.24k stars 12.71k forks source link

Remark `TMP`, `TEMP` and `USERPROFILE` env vars can influence `env::temp_dir` on Windows #125439

Open jieyouxu opened 5 months ago

jieyouxu commented 5 months ago

Location

Summary

Encountered in https://github.com/rust-lang/rust/pull/122580#issuecomment-2125755689.

In codegen we (transitively) use env::temp_dir() for some temporary files. If you unset (or provide a value that is not a writable directory) all three TMP, TEMP, USERPROFILE env vars (such as by calling Command::env_clear on some cargo invocation) on Windows, then env::temp_dir() can return the Windows directory which is often not writable.

This is documented in GetTempPath2W, but it might be worth a remark on the docs for env::temp_dir and Command::env_clear that modifying or unsetting TMP, TEMP and USERPROFILE can influence the result of env::temp_dir on Windows.

It is already pointed out that env::temp_dir returns value of TMPDIR on Linux.

ChrisDenton commented 5 months ago

A more general remark to add is that std does not check the returned path in any way. On any platform there's always the risk it might not be writeable by the current application. It might not even exist.