Skip to content

Commit 06178d7

Browse files
committed
Auto merge of #11359 - LucioFranco:lucio/home-crate, r=ehuss
Add `home` crate to cargo crates ### What does this PR try to resolve? The `home` crate is the crate used by both `rustup` and `cargo` to define their home directories. The crate is quite small but plays an important role in gluing rust tooling together. This PR adds this crate the `crates` folder within cargo to allow the cargo team and the rust project to maintain this small crate. ### Additional information I've also added both `rust-lang-owners` and `ehuss` as owners of the crate. Once, merged I will archive the old repo. Reference zulip conversation: https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Moving.20the.20home.20crate.20into.20rust-lang.20and.20under.20cargo/near/299975263 cc `@weihanglo` `@ehuss`
2 parents 7779606 + 18a06cc commit 06178d7

File tree

9 files changed

+430
-0
lines changed

9 files changed

+430
-0
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
CARGO_TARGET_DIR: target
100100
- run: cargo test -p cargo-platform
101101
- run: cargo test -p cargo-util
102+
- run: cargo test --manifest-path crates/home/Cargo.toml
102103
- run: cargo test --manifest-path crates/mdman/Cargo.toml
103104
- run: cargo build --manifest-path crates/credential/cargo-credential-1password/Cargo.toml
104105
- run: cargo build --manifest-path crates/credential/cargo-credential-gnome-secret/Cargo.toml

crates/home/CHANGELOG.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
<!-- ## [Unreleased] -->
8+
9+
## [0.5.4] - 2022-10-10
10+
- Add `_with_env` variants of functions to support in-process threaded tests for
11+
rustup.
12+
13+
## [0.5.3] - 2020-01-07
14+
15+
Use Rust 1.36.0 as minimum Rust version.
16+
17+
## [0.5.2] - 2020-01-05
18+
19+
*YANKED since it cannot be built on Rust 1.36.0*
20+
21+
### Changed
22+
- Check for emptiness of `CARGO_HOME` and `RUSTUP_HOME` environment variables.
23+
- Windows: Use `SHGetFolderPath` to replace `GetUserProfileDirectory` syscall.
24+
* Remove `scopeguard` dependency.
25+
26+
## [0.5.1] - 2019-10-12
27+
### Changed
28+
- Disable unnecessary features for `scopeguard`. Thanks @mati865.
29+
30+
## [0.5.0] - 2019-08-21
31+
### Added
32+
- Add `home_dir` implementation for Windows UWP platforms.
33+
34+
### Fixed
35+
- Fix `rustup_home` implementation when `RUSTUP_HOME` is an absolute directory.
36+
- Fix `cargo_home` implementation when `CARGO_HOME` is an absolute directory.
37+
38+
### Removed
39+
- Remove support for `multirust` folder used in old version of `rustup`.
40+
41+
[Unreleased]: https://github.com/brson/home/compare/v0.5.4...HEAD
42+
[0.5.4]: https://github.com/brson/home/compare/v0.5.3...v0.5.4
43+
[0.5.3]: https://github.com/brson/home/compare/v0.5.2...v0.5.3
44+
[0.5.2]: https://github.com/brson/home/compare/v0.5.1...v0.5.2
45+
[0.5.1]: https://github.com/brson/home/compare/v0.5.0...v0.5.1
46+
[0.5.0]: https://github.com/brson/home/compare/0.4.2...v0.5.0

crates/home/Cargo.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "home"
3+
version = "0.5.4" # also update `html_root_url` in `src/lib.rs`
4+
authors = [ "Brian Anderson <[email protected]>" ]
5+
documentation = "https://docs.rs/home"
6+
edition = "2018"
7+
include = [
8+
"/src",
9+
"/Cargo.toml",
10+
"/CHANGELOG",
11+
"/LICENSE-*",
12+
"/README.md",
13+
]
14+
license = "MIT OR Apache-2.0"
15+
readme = "README.md"
16+
repository = "https://github.com/brson/home"
17+
description = "Shared definitions of home directories"
18+
19+
[target."cfg(windows)".dependencies.winapi]
20+
version = "0.3"
21+
features = [
22+
"shlobj",
23+
"std",
24+
"winerror",
25+
]

crates/home/LICENSE-APACHE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-APACHE

crates/home/LICENSE-MIT

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-MIT

crates/home/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[![Documentation](https://docs.rs/home/badge.svg)](https://docs.rs/home)
2+
[![Crates.io](https://img.shields.io/crates/v/home.svg)](https://crates.io/crates/home)
3+
4+
Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
5+
6+
This provides the definition of `home_dir` used by Cargo and rustup,
7+
as well functions to find the correct value of `CARGO_HOME` and
8+
`RUSTUP_HOME`.
9+
10+
The definition of `home_dir` provided by the standard library is
11+
incorrect because it considers the `HOME` environment variable on
12+
Windows. This causes surprising situations where a Rust program will
13+
behave differently depending on whether it is run under a Unix
14+
emulation environment like Cygwin or MinGW. Neither Cargo nor rustup
15+
use the standard libraries definition - they use the definition here.
16+
17+
This crate further provides two functions, `cargo_home` and
18+
`rustup_home`, which are the canonical way to determine the location
19+
that Cargo and rustup store their data.
20+
21+
See [rust-lang/rust#43321].
22+
23+
[rust-lang/rust#43321]: https://github.com/rust-lang/rust/issues/43321
24+
25+
## License
26+
27+
MIT OR Apache-2.0

crates/home/src/env.rs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//! Lower-level utilities for mocking the process environment.
2+
3+
use std::{
4+
ffi::OsString,
5+
io,
6+
path::{Path, PathBuf},
7+
};
8+
9+
/// Permits parameterizing the home functions via the _from variants - used for
10+
/// in-process unit testing by rustup.
11+
pub trait Env {
12+
/// Return the path to the the users home dir, or None if any error occurs:
13+
/// see home_inner.
14+
fn home_dir(&self) -> Option<PathBuf>;
15+
/// Return the current working directory.
16+
fn current_dir(&self) -> io::Result<PathBuf>;
17+
/// Get an environment variable, as per std::env::var_os.
18+
fn var_os(&self, key: &str) -> Option<OsString>;
19+
}
20+
21+
/// Implements Env for the OS context, both Unix style and Windows.
22+
///
23+
/// This is trait permits in-process testing by providing a control point to
24+
/// allow in-process divergence on what is normally process wide state.
25+
///
26+
/// Implementations should be provided by whatever testing framework the caller
27+
/// is using. Code that is not performing in-process threaded testing requiring
28+
/// isolated rustup/cargo directories does not need this trait or the _from
29+
/// functions.
30+
pub struct OsEnv;
31+
impl Env for OsEnv {
32+
fn home_dir(&self) -> Option<PathBuf> {
33+
crate::home_dir_inner()
34+
}
35+
fn current_dir(&self) -> io::Result<PathBuf> {
36+
std::env::current_dir()
37+
}
38+
fn var_os(&self, key: &str) -> Option<OsString> {
39+
std::env::var_os(key)
40+
}
41+
}
42+
43+
pub const OS_ENV: OsEnv = OsEnv {};
44+
45+
/// Returns the path of the current user's home directory from [`Env::home_dir`].
46+
pub fn home_dir_with_env(env: &dyn Env) -> Option<PathBuf> {
47+
env.home_dir()
48+
}
49+
50+
/// Variant of cargo_home where the environment source is parameterized. This is
51+
/// specifically to support in-process testing scenarios as environment
52+
/// variables and user home metadata are normally process global state. See the
53+
/// [`Env`] trait.
54+
pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
55+
let cwd = env.current_dir()?;
56+
cargo_home_with_cwd_env(env, &cwd)
57+
}
58+
59+
/// Variant of cargo_home_with_cwd where the environment source is
60+
/// parameterized. This is specifically to support in-process testing scenarios
61+
/// as environment variables and user home metadata are normally process global
62+
/// state. See the OsEnv trait.
63+
pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
64+
match env.var_os("CARGO_HOME").filter(|h| !h.is_empty()) {
65+
Some(home) => {
66+
let home = PathBuf::from(home);
67+
if home.is_absolute() {
68+
Ok(home)
69+
} else {
70+
Ok(cwd.join(&home))
71+
}
72+
}
73+
_ => home_dir_with_env(env)
74+
.map(|p| p.join(".cargo"))
75+
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")),
76+
}
77+
}
78+
79+
/// Variant of cargo_home_with_cwd where the environment source is
80+
/// parameterized. This is specifically to support in-process testing scenarios
81+
/// as environment variables and user home metadata are normally process global
82+
/// state. See the OsEnv trait.
83+
pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
84+
let cwd = env.current_dir()?;
85+
rustup_home_with_cwd_env(env, &cwd)
86+
}
87+
88+
/// Variant of cargo_home_with_cwd where the environment source is
89+
/// parameterized. This is specifically to support in-process testing scenarios
90+
/// as environment variables and user home metadata are normally process global
91+
/// state. See the OsEnv trait.
92+
pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
93+
match env.var_os("RUSTUP_HOME").filter(|h| !h.is_empty()) {
94+
Some(home) => {
95+
let home = PathBuf::from(home);
96+
if home.is_absolute() {
97+
Ok(home)
98+
} else {
99+
Ok(cwd.join(&home))
100+
}
101+
}
102+
_ => home_dir_with_env(env)
103+
.map(|d| d.join(".rustup"))
104+
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find rustup home dir")),
105+
}
106+
}

crates/home/src/lib.rs

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//! Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
2+
//!
3+
//! This provides the definition of `home_dir` used by Cargo and
4+
//! rustup, as well functions to find the correct value of
5+
//! `CARGO_HOME` and `RUSTUP_HOME`.
6+
//!
7+
//! See also the [`dirs`](https://docs.rs/dirs) crate.
8+
//!
9+
//! _Note that as of 2019/08/06 it appears that cargo uses this crate. And
10+
//! rustup has used this crate since 2019/08/21._
11+
//!
12+
//! The definition of `home_dir` provided by the standard library is
13+
//! incorrect because it considers the `HOME` environment variable on
14+
//! Windows. This causes surprising situations where a Rust program
15+
//! will behave differently depending on whether it is run under a
16+
//! Unix emulation environment like Cygwin or MinGW. Neither Cargo nor
17+
//! rustup use the standard libraries definition - they use the
18+
//! definition here.
19+
//!
20+
//! This crate further provides two functions, `cargo_home` and
21+
//! `rustup_home`, which are the canonical way to determine the
22+
//! location that Cargo and rustup store their data.
23+
//!
24+
//! See also this [discussion].
25+
//!
26+
//! [discussion]: https://github.com/rust-lang/rust/pull/46799#issuecomment-361156935
27+
28+
#![doc(html_root_url = "https://docs.rs/home/0.5.4")]
29+
#![deny(rust_2018_idioms)]
30+
31+
pub mod env;
32+
33+
#[cfg(windows)]
34+
mod windows;
35+
36+
use std::io;
37+
use std::path::{Path, PathBuf};
38+
39+
/// Returns the path of the current user's home directory if known.
40+
///
41+
/// # Unix
42+
///
43+
/// Returns the value of the `HOME` environment variable if it is set
44+
/// and not equal to the empty string. Otherwise, it tries to determine the
45+
/// home directory by invoking the `getpwuid_r` function on the UID of the
46+
/// current user.
47+
///
48+
/// # Windows
49+
///
50+
/// Returns the value of the `USERPROFILE` environment variable if it
51+
/// is set and not equal to the empty string. If both do not exist,
52+
/// [`SHGetFolderPathW`][msdn] is used to return the appropriate path.
53+
///
54+
/// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
55+
///
56+
/// # Examples
57+
///
58+
/// ```
59+
/// match home::home_dir() {
60+
/// Some(path) => println!("{}", path.display()),
61+
/// None => println!("Impossible to get your home dir!"),
62+
/// }
63+
/// ```
64+
pub fn home_dir() -> Option<PathBuf> {
65+
env::home_dir_with_env(&env::OS_ENV)
66+
}
67+
68+
#[cfg(windows)]
69+
use windows::home_dir_inner;
70+
71+
#[cfg(any(unix, target_os = "redox"))]
72+
fn home_dir_inner() -> Option<PathBuf> {
73+
#[allow(deprecated)]
74+
std::env::home_dir()
75+
}
76+
77+
/// Returns the storage directory used by Cargo, often knowns as
78+
/// `.cargo` or `CARGO_HOME`.
79+
///
80+
/// It returns one of the following values, in this order of
81+
/// preference:
82+
///
83+
/// - The value of the `CARGO_HOME` environment variable, if it is
84+
/// an absolute path.
85+
/// - The value of the current working directory joined with the value
86+
/// of the `CARGO_HOME` environment variable, if `CARGO_HOME` is a
87+
/// relative directory.
88+
/// - The `.cargo` directory in the user's home directory, as reported
89+
/// by the `home_dir` function.
90+
///
91+
/// # Errors
92+
///
93+
/// This function fails if it fails to retrieve the current directory,
94+
/// or if the home directory cannot be determined.
95+
///
96+
/// # Examples
97+
///
98+
/// ```
99+
/// match home::cargo_home() {
100+
/// Ok(path) => println!("{}", path.display()),
101+
/// Err(err) => eprintln!("Cannot get your cargo home dir: {:?}", err),
102+
/// }
103+
/// ```
104+
pub fn cargo_home() -> io::Result<PathBuf> {
105+
env::cargo_home_with_env(&env::OS_ENV)
106+
}
107+
108+
/// Returns the storage directory used by Cargo within `cwd`.
109+
/// For more details, see [`cargo_home`](fn.cargo_home.html).
110+
pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
111+
env::cargo_home_with_cwd_env(&env::OS_ENV, cwd)
112+
}
113+
114+
/// Returns the storage directory used by rustup, often knowns as
115+
/// `.rustup` or `RUSTUP_HOME`.
116+
///
117+
/// It returns one of the following values, in this order of
118+
/// preference:
119+
///
120+
/// - The value of the `RUSTUP_HOME` environment variable, if it is
121+
/// an absolute path.
122+
/// - The value of the current working directory joined with the value
123+
/// of the `RUSTUP_HOME` environment variable, if `RUSTUP_HOME` is a
124+
/// relative directory.
125+
/// - The `.rustup` directory in the user's home directory, as reported
126+
/// by the `home_dir` function.
127+
///
128+
/// # Errors
129+
///
130+
/// This function fails if it fails to retrieve the current directory,
131+
/// or if the home directory cannot be determined.
132+
///
133+
/// # Examples
134+
///
135+
/// ```
136+
/// match home::rustup_home() {
137+
/// Ok(path) => println!("{}", path.display()),
138+
/// Err(err) => eprintln!("Cannot get your rustup home dir: {:?}", err),
139+
/// }
140+
/// ```
141+
pub fn rustup_home() -> io::Result<PathBuf> {
142+
env::rustup_home_with_env(&env::OS_ENV)
143+
}
144+
145+
/// Returns the storage directory used by rustup within `cwd`.
146+
/// For more details, see [`rustup_home`](fn.rustup_home.html).
147+
pub fn rustup_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
148+
env::rustup_home_with_cwd_env(&env::OS_ENV, cwd)
149+
}

0 commit comments

Comments
 (0)