about summary refs log tree commit diff
path: root/library/std/src/env.rs
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2024-11-30 12:57:33 +0800
committerGitHub <noreply@github.com>2024-11-30 12:57:33 +0800
commit9eeae42de250ab0d6e880e0b04d6b348b803b41e (patch)
treec161c8ae2fd0100ffde6500b949a44946dad9e99 /library/std/src/env.rs
parent1aa01927d3b8eeec06db1cd72e23b08983aa3104 (diff)
parent4b7f56ac9dd3db6163f26ab7b5e28b55756fef79 (diff)
downloadrust-9eeae42de250ab0d6e880e0b04d6b348b803b41e.tar.gz
rust-9eeae42de250ab0d6e880e0b04d6b348b803b41e.zip
Rollup merge of #132515 - kornelski:home_fix, r=jhpratt
Fix and undeprecate home_dir()

`home_dir()` has been deprecated for 6 years due to using `HOME` env var on Windows.

It's been a long time, and having a perpetually buggy and deprecated function in the standard library is not useful. I propose fixing and undeprecating it.

6 years seems more than long enough to warn users against relying on this function. The change in behavior is minor, and it's more of a bug fix than breakage. The old behavior is unlikely to be useful, and even if anybody actually needed to specifically use the non-standard `HOME` on Windows, they can trivially mitigate this change by reading the env var themselves.

----

Use of `USERPROFILE` is in line with the `home` crate: https://github.com/rust-lang/cargo/blob/37bc5f0232a0bb72dedd2c14149614fd8cdae649/crates/home/src/windows.rs#L12

The `home` crate uses `SHGetKnownFolderPath` instead of `GetUserProfileDirectoryW`. AFAIK it doesn't make any difference in practice, because `SHGetKnownFolderPath` merely adds support for more kinds of folders, including virtual (non-filesystem) folders identified by a GUID, but the specific case of [`FOLDERID_Profile`](https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid#FOLDERID_Profile) is documented as a FIXED folder (a regular filesystem path). Just in case, I've added a note to documentation that the use of `GetUserProfileDirectoryW` can change.

I've used `CURRENT_RUSTC_VERSION` in a doccomment. `replace-version-placeholder` tool seems to perform a simple string replacement, so hopefully it'll get updated.
Diffstat (limited to 'library/std/src/env.rs')
-rw-r--r--library/std/src/env.rs16
1 files changed, 6 insertions, 10 deletions
diff --git a/library/std/src/env.rs b/library/std/src/env.rs
index 27f4daba44b..043747d0bc5 100644
--- a/library/std/src/env.rs
+++ b/library/std/src/env.rs
@@ -608,20 +608,16 @@ impl Error for JoinPathsError {
 ///
 /// # Windows
 ///
-/// - Returns the value of the 'HOME' environment variable if it is set
-///   (including to an empty string).
-/// - Otherwise, returns the value of the 'USERPROFILE' environment variable if it is set
-///   (including to an empty string).
-/// - If both do not exist, [`GetUserProfileDirectory`][msdn] is used to return the path.
+/// - Returns the value of the 'USERPROFILE' environment variable if it is set, and is not an empty string.
+/// - Otherwise, [`GetUserProfileDirectory`][msdn] is used to return the path. This may change in the future.
 ///
 /// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/userenv/nf-userenv-getuserprofiledirectorya
 ///
-/// # Deprecation
+/// In UWP (Universal Windows Platform) targets this function is unimplemented and always returns `None`.
 ///
-/// This function is deprecated because the behavior on Windows is not correct.
-/// The 'HOME' environment variable is not standard on Windows, and may not produce
-/// desired results; for instance, under Cygwin or Mingw it will return `/home/you`
-/// when it should return `C:\Users\you`.
+/// Before Rust CURRENT_RUSTC_VERSION, this function used to return the value of the 'HOME' environment variable
+/// on Windows, which in Cygwin or Mingw environments could return non-standard paths like `/home/you`
+/// instead of `C:\Users\you`.
 ///
 /// # Examples
 ///