Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/home/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ license.workspace = true
repository.workspace = true
description = "Shared definitions of home directories."

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_UI_Shell", "Win32_System_Com"] }

[lints]
workspace = true
18 changes: 4 additions & 14 deletions crates/home/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@ This provides the definition of `home_dir` used by Cargo and rustup,
as well functions to find the correct value of `CARGO_HOME` and
`RUSTUP_HOME`.

The definition of [`home_dir`] provided by the standard library is
incorrect because it considers the `HOME` environment variable on
Windows. This causes surprising situations where a Rust program will
behave differently depending on whether it is run under a Unix
emulation environment like Cygwin or MinGW. Neither Cargo nor rustup
use the standard library's definition - they use the definition here.

**Note:** This has been fixed in Rust 1.85 to no longer use the `HOME`
environment variable on Windows. If you are still using this crate for the
purpose of getting a home directory, you are strongly encouraged to switch to
using the standard library's [`home_dir`] instead. It is planned to have the
deprecation notice removed in 1.87.
This crate previously used its own definition of `home_dir` on Windows to avoid
[rust-lang/rust#43321], but this was fixed in [1.85.0] so this crate now
simply forwards to [`home_dir`].

This crate further provides two functions, `cargo_home` and
`rustup_home`, which are the canonical way to determine the location
that Cargo and rustup store their data.

See [rust-lang/rust#43321].

> This crate is maintained by the Cargo team, primarily for use by Cargo and Rustup
> and not intended for external use. This
> crate may make major changes to its APIs or be deprecated without warning.
[rust-lang/rust#43321]: https://github.com/rust-lang/rust/issues/43321
[`home_dir`]: https://doc.rust-lang.org/nightly/std/env/fn.home_dir.html
[1.85.0]: https://blog.rust-lang.org/2025/02/20/Rust-1.85.0/#updates-to-std-env-home-dir

## License

Expand Down
2 changes: 1 addition & 1 deletion crates/home/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait Env {
pub struct OsEnv;
impl Env for OsEnv {
fn home_dir(&self) -> Option<PathBuf> {
crate::home_dir_inner()
std::env::home_dir()
}
fn current_dir(&self) -> io::Result<PathBuf> {
std::env::current_dir()
Expand Down
31 changes: 1 addition & 30 deletions crates/home/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,13 @@

pub mod env;

#[cfg(target_os = "windows")]
mod windows;

use std::io;
use std::path::{Path, PathBuf};

/// Returns the path of the current user's home directory using environment
/// variables or OS-specific APIs.
///
/// # Unix
///
/// Returns the value of the `HOME` environment variable if it is set
/// **even** if it is an empty string. Otherwise, it tries to determine the
/// home directory by invoking the [`getpwuid_r`][getpwuid] function with
/// the UID of the current user.
///
/// [getpwuid]: https://linux.die.net/man/3/getpwuid_r
///
/// # Windows
///
/// Returns the value of the `USERPROFILE` environment variable if it is set
/// **and** it is not an empty string. Otherwise, it tries to determine the
/// home directory by invoking the [`SHGetKnownFolderPath`][shgkfp] function with
/// [`FOLDERID_Profile`][knownfolderid].
///
/// [shgkfp]: https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
/// [knownfolderid]: https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
/// This function is just a wrapper around [`std::env::home_dir`](https://doc.rust-lang.org/std/env/fn.home_dir.html)
///
/// # Examples
///
Expand All @@ -66,15 +46,6 @@ pub fn home_dir() -> Option<PathBuf> {
env::home_dir_with_env(&env::OS_ENV)
}

#[cfg(windows)]
use windows::home_dir_inner;

#[cfg(unix)]
fn home_dir_inner() -> Option<PathBuf> {
#[allow(deprecated)]
std::env::home_dir()
}

/// Returns the storage directory used by Cargo, often known as
/// `.cargo` or `CARGO_HOME`.
///
Expand Down
84 changes: 0 additions & 84 deletions crates/home/src/windows.rs

This file was deleted.