Skip to content
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

Add home crate to cargo crates #11359

Merged
merged 101 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
25592d9
Initial
brson Jul 18, 2017
b1a9fe1
Link to upstream issue
brson Jul 18, 2017
cf5dd41
Add .gitignore
brson Jul 19, 2017
759a15b
Add cwd-accepting versions of cargo_home and rustup_home
brson Jul 19, 2017
c643270
Add cargo compat code
brson Jul 19, 2017
abf65ad
Redox OS
ids1024 Jul 30, 2017
c10b8d2
Bump scopeguard to 0.3
Eijebong Mar 2, 2018
1825f0a
Bump winapi to 0.3
Eijebong Mar 2, 2018
bfaf1f1
Bump version number
Eijebong Mar 2, 2018
d4d9ac8
Merge pull request #3 from Eijebong/bump
alexcrichton Mar 19, 2018
5673c9a
Bump to 0.3.2
alexcrichton Mar 19, 2018
152407f
Depend on only one version of winapi
andjo403 May 10, 2018
2e01bd4
Merge pull request #4 from andjo403/master
alexcrichton May 10, 2018
5744640
Bump to 0.3.3
andjo403 May 11, 2018
a0520b0
Merge pull request #5 from andjo403/bump
alexcrichton May 11, 2018
4f887cf
Merge pull request #1 from ids1024/redox
brson Jan 15, 2019
f5dd969
cargo: Split winapi to separate table
tesuji Jun 1, 2019
71afeb2
cargo: Bump scopeguard to 1.0.0
tesuji Jun 1, 2019
41fb21b
Update to 2018 edition
tesuji Jun 1, 2019
2a4ac15
Lazy ok_or_else
tesuji Jun 1, 2019
72d8872
Lazy calculate user_home
tesuji Jun 1, 2019
f39302f
cargo fmt
tesuji Jun 1, 2019
51ec02e
Merge pull request #6 from lzutao/bump-scopeguard
brson Aug 6, 2019
97baa41
Bump to 0.4
brson Aug 6, 2019
645457e
Allow deprecated std::env::home_dir
brson Aug 6, 2019
b4a6db2
Mention dirs crate
brson Aug 6, 2019
2f74993
Bump to 0.4.1
brson Aug 6, 2019
64de940
Fix windows build failure
tesuji Aug 7, 2019
ddad9bf
Merge pull request #8 from lzutao/fix-windows-ci
brson Aug 8, 2019
c2a4f95
Bump to 0.4.2
brson Aug 8, 2019
38ba4ef
Add travis config
brson Aug 15, 2019
2c718c9
Add Cargo.lock to gitignore
brson Aug 15, 2019
2002aee
Add LICENSE text file
tesuji Aug 16, 2019
e658ff7
ci: Add Mac and Windows builds
tesuji Aug 16, 2019
865e4b9
Merge pull request #10 from lzutao/ci/add-mac-winnt-builds
tesuji Aug 16, 2019
ae79f9f
chore: Split Windows impl to a separate module
tesuji Aug 16, 2019
e9d14ba
Add home_dir impl for Windows UWP platforms
tesuji Aug 16, 2019
7bb09dd
Remove multirust support
tesuji Aug 16, 2019
180b334
Add more examples to show how API works
tesuji Aug 16, 2019
623419d
Merge pull request #11 from lzutao/cleanup
tesuji Aug 21, 2019
46003d5
Bump home to 0.5.0
tesuji Aug 21, 2019
2f0fe8a
Merge pull request #12 from lzutao/bump-v0.5.0
tesuji Aug 21, 2019
e40e332
Merge pull request #9 from lzutao/add-license
brson Aug 22, 2019
08eec12
State that rustup uses this crate
tesuji Aug 29, 2019
3e824d2
Update changelog version changed link
tesuji Sep 17, 2019
f44f3f2
Disable unnecessary features for scopeguard
mati865 Oct 11, 2019
6e7ea32
Merge pull request #15 from mati865/scopeguard
tesuji Oct 11, 2019
3f94c5a
Remove redundant closure
tesuji Oct 4, 2019
ef18523
Bump new version: v0.5.1
tesuji Oct 11, 2019
cfc445e
Use rustup minimal profile to speed up Rust installation time
tesuji Oct 15, 2019
29d9c3c
build(travis): fix build stuck issue on Windows build jobs
tesuji Oct 21, 2019
1886c07
ci: Try github actions
tesuji Oct 15, 2019
38c2d2b
actions: Install rustup only (and not install toolchain)
tesuji Jan 2, 2020
23bc439
Add why we cannot use Miri
tesuji Jan 2, 2020
854cbf9
Run clippy on Windows only
tesuji Jan 2, 2020
d1ffd6e
Add default .rustfmt.toml
tesuji Dec 30, 2019
77ea3e8
Correct links
tesuji Dec 30, 2019
a596a75
Remove incorrect documents about multirust
tesuji Dec 30, 2019
0d01112
Check for emptiness of env vars
tesuji Jan 2, 2020
fdca596
Add some behavior tests for home_dir on Windows
tesuji Dec 30, 2019
1c5772b
actions: Cache CARGO_HOME
tesuji Jan 5, 2020
12645bc
Specialized handling for our cases
tesuji Dec 30, 2019
2fbb290
Use SHGetFolderPath to replace GetUserProfileDirectory
tesuji Dec 30, 2019
d23fd46
Use Vec instead of MaybeUninit which is complex
tesuji Jan 5, 2020
c03a7bc
Add doc to *_with_cwd functions
tesuji Oct 4, 2019
69258b0
Remove needless cfg gate
tesuji Jan 5, 2020
f6cccd5
Bump to 0.5.2
tesuji Jan 5, 2020
61576ab
Remove Travis template as GitHub Actions is enough
tesuji Jan 5, 2020
0a52e71
actions: Trigger builds on PR or commit for master branch
tesuji Jan 5, 2020
410d426
Include only necessary components in package
tesuji Jan 6, 2020
c97f8f0
readme: Add badge for docs.rs and crates.io
tesuji Jan 6, 2020
1d9393e
Use README in crates.io
tesuji Jan 6, 2020
fd7d425
actions: Add before cache step to clean it up
tesuji Jan 6, 2020
cd27296
Use Rust 1.36.0 as minimum Rust version
tesuji Jan 7, 2020
5a577f7
actions: Use beta toolchain for Clippy
tesuji Jan 7, 2020
4607d98
Bump to 0.5.3
tesuji Jan 7, 2020
3a6eccd
doc: Now we use SHGetFolderPathW
tesuji Jan 7, 2020
af762b0
Remove redundant CI scripts
tesuji Jun 13, 2020
32044e5
Improve CI caching by add Cargo.lock
tesuji Jun 13, 2020
0719a86
Revert "Improve CI caching by add Cargo.lock"
LucioFranco Sep 19, 2022
deefa5a
chore: Update CI and remove lockfile
LucioFranco Sep 19, 2022
21df461
Merge pull request #28 from brson/lucio/update-ci
LucioFranco Sep 19, 2022
0467bc2
Add `_from` variants to support threaded tests
rbtcollins Jun 7, 2020
660ccbc
Publically export OS_ENV
rbtcollins Jul 1, 2020
c77b640
Review feedback
rbtcollins Jul 3, 2020
e3e570d
Add mockable home fn and `Env` trait
LucioFranco Sep 19, 2022
38c610a
update changelog
LucioFranco Sep 19, 2022
eed5978
chore: Improve CI
LucioFranco Sep 19, 2022
6d79d95
remove unwanted ci tests
LucioFranco Sep 19, 2022
0f6aad6
remove code cov
LucioFranco Sep 19, 2022
9afbd90
Merge pull request #30 from brson/refactor-ci
LucioFranco Sep 19, 2022
fb34fe9
Merge remote-tracking branch 'origin/master' into lucio/env
LucioFranco Sep 19, 2022
de6dd85
Merge pull request #29 from brson/lucio/env
LucioFranco Oct 10, 2022
e697b7b
chore: Prepare `v0.5.4`
LucioFranco Oct 10, 2022
f0d2362
Merge pull request #31 from brson/lucio/prepare-0.5.4
LucioFranco Oct 11, 2022
dbb8dad
Add crate
LucioFranco Dec 12, 2022
6a1a9ed
Merge branch 'homemerge' into lucio/home-crate
LucioFranco Dec 12, 2022
98a3d4e
Add home crate to CI
LucioFranco Dec 12, 2022
ac9da4b
home: Remove github ci, link license, remove others
LucioFranco Dec 12, 2022
faa9b31
Remove home crate path dep
LucioFranco Dec 12, 2022
18a06cc
fix ci to test local home crate
LucioFranco Dec 13, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
CARGO_TARGET_DIR: target
- run: cargo test -p cargo-platform
- run: cargo test -p cargo-util
- run: cargo test --manifest-path crates/home/Cargo.toml
- run: cargo test --manifest-path crates/mdman/Cargo.toml
- run: cargo build --manifest-path crates/credential/cargo-credential-1password/Cargo.toml
- run: cargo build --manifest-path crates/credential/cargo-credential-gnome-secret/Cargo.toml
Expand Down
46 changes: 46 additions & 0 deletions crates/home/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- ## [Unreleased] -->

## [0.5.4] - 2022-10-10
- Add `_with_env` variants of functions to support in-process threaded tests for
rustup.

## [0.5.3] - 2020-01-07

Use Rust 1.36.0 as minimum Rust version.

## [0.5.2] - 2020-01-05

*YANKED since it cannot be built on Rust 1.36.0*

### Changed
- Check for emptiness of `CARGO_HOME` and `RUSTUP_HOME` environment variables.
- Windows: Use `SHGetFolderPath` to replace `GetUserProfileDirectory` syscall.
* Remove `scopeguard` dependency.

## [0.5.1] - 2019-10-12
### Changed
- Disable unnecessary features for `scopeguard`. Thanks @mati865.

## [0.5.0] - 2019-08-21
### Added
- Add `home_dir` implementation for Windows UWP platforms.

### Fixed
- Fix `rustup_home` implementation when `RUSTUP_HOME` is an absolute directory.
- Fix `cargo_home` implementation when `CARGO_HOME` is an absolute directory.

### Removed
- Remove support for `multirust` folder used in old version of `rustup`.

[Unreleased]: https://github.com/brson/home/compare/v0.5.4...HEAD
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO I think for now it makes sense to keep these linked to brson, but on the next release we can update the tag. Not sure what the process in cargo is. That said, I doubt we will need a release for a long time so something that can be figured out then. The brson/home links should continue to work even if its archived iirc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no such a tagging policy for sub-crates in rust-lang/cargo. Tagging cargo itself is a separate process ran by Release team as well I believe (but currently broken 😅 #11345).

[0.5.4]: https://github.com/brson/home/compare/v0.5.3...v0.5.4
[0.5.3]: https://github.com/brson/home/compare/v0.5.2...v0.5.3
[0.5.2]: https://github.com/brson/home/compare/v0.5.1...v0.5.2
[0.5.1]: https://github.com/brson/home/compare/v0.5.0...v0.5.1
[0.5.0]: https://github.com/brson/home/compare/0.4.2...v0.5.0
25 changes: 25 additions & 0 deletions crates/home/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "home"
version = "0.5.4" # also update `html_root_url` in `src/lib.rs`
authors = [ "Brian Anderson <andersrb@gmail.com>" ]
documentation = "https://docs.rs/home"
edition = "2018"
include = [
"/src",
"/Cargo.toml",
"/CHANGELOG",
"/LICENSE-*",
"/README.md",
]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/brson/home"
description = "Shared definitions of home directories"

[target."cfg(windows)".dependencies.winapi]
version = "0.3"
features = [
"shlobj",
"std",
"winerror",
]
1 change: 1 addition & 0 deletions crates/home/LICENSE-APACHE
1 change: 1 addition & 0 deletions crates/home/LICENSE-MIT
27 changes: 27 additions & 0 deletions crates/home/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[![Documentation](https://docs.rs/home/badge.svg)](https://docs.rs/home)
[![Crates.io](https://img.shields.io/crates/v/home.svg)](https://crates.io/crates/home)

Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.

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 libraries definition - they use the definition here.

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].

[rust-lang/rust#43321]: https://github.com/rust-lang/rust/issues/43321

## License

MIT OR Apache-2.0
106 changes: 106 additions & 0 deletions crates/home/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! Lower-level utilities for mocking the process environment.

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

/// Permits parameterizing the home functions via the _from variants - used for
/// in-process unit testing by rustup.
pub trait Env {
/// Return the path to the the users home dir, or None if any error occurs:
/// see home_inner.
fn home_dir(&self) -> Option<PathBuf>;
/// Return the current working directory.
fn current_dir(&self) -> io::Result<PathBuf>;
/// Get an environment variable, as per std::env::var_os.
fn var_os(&self, key: &str) -> Option<OsString>;
}

/// Implements Env for the OS context, both Unix style and Windows.
///
/// This is trait permits in-process testing by providing a control point to
/// allow in-process divergence on what is normally process wide state.
///
/// Implementations should be provided by whatever testing framework the caller
/// is using. Code that is not performing in-process threaded testing requiring
/// isolated rustup/cargo directories does not need this trait or the _from
/// functions.
pub struct OsEnv;
impl Env for OsEnv {
fn home_dir(&self) -> Option<PathBuf> {
crate::home_dir_inner()
}
fn current_dir(&self) -> io::Result<PathBuf> {
std::env::current_dir()
}
fn var_os(&self, key: &str) -> Option<OsString> {
std::env::var_os(key)
}
}

pub const OS_ENV: OsEnv = OsEnv {};

/// Returns the path of the current user's home directory from [`Env::home_dir`].
pub fn home_dir_with_env(env: &dyn Env) -> Option<PathBuf> {
env.home_dir()
}

/// Variant of cargo_home where the environment source is parameterized. This is
/// specifically to support in-process testing scenarios as environment
/// variables and user home metadata are normally process global state. See the
/// [`Env`] trait.
pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
let cwd = env.current_dir()?;
cargo_home_with_cwd_env(env, &cwd)
}

/// Variant of cargo_home_with_cwd where the environment source is
/// parameterized. This is specifically to support in-process testing scenarios
/// as environment variables and user home metadata are normally process global
/// state. See the OsEnv trait.
pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
match env.var_os("CARGO_HOME").filter(|h| !h.is_empty()) {
Some(home) => {
let home = PathBuf::from(home);
if home.is_absolute() {
Ok(home)
} else {
Ok(cwd.join(&home))
}
}
_ => home_dir_with_env(env)
.map(|p| p.join(".cargo"))
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")),
}
}

/// Variant of cargo_home_with_cwd where the environment source is
/// parameterized. This is specifically to support in-process testing scenarios
/// as environment variables and user home metadata are normally process global
/// state. See the OsEnv trait.
pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
let cwd = env.current_dir()?;
rustup_home_with_cwd_env(env, &cwd)
}

/// Variant of cargo_home_with_cwd where the environment source is
/// parameterized. This is specifically to support in-process testing scenarios
/// as environment variables and user home metadata are normally process global
/// state. See the OsEnv trait.
pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
match env.var_os("RUSTUP_HOME").filter(|h| !h.is_empty()) {
Some(home) => {
let home = PathBuf::from(home);
if home.is_absolute() {
Ok(home)
} else {
Ok(cwd.join(&home))
}
}
_ => home_dir_with_env(env)
.map(|d| d.join(".rustup"))
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find rustup home dir")),
}
}
149 changes: 149 additions & 0 deletions crates/home/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//! Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
//!
//! 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`.
//!
//! See also the [`dirs`](https://docs.rs/dirs) crate.
//!
//! _Note that as of 2019/08/06 it appears that cargo uses this crate. And
//! rustup has used this crate since 2019/08/21._
//!
//! 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 libraries definition - they use the
//! definition here.
//!
//! 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 also this [discussion].
//!
//! [discussion]: https://github.com/rust-lang/rust/pull/46799#issuecomment-361156935

#![doc(html_root_url = "https://docs.rs/home/0.5.4")]
#![deny(rust_2018_idioms)]

pub mod env;

#[cfg(windows)]
mod windows;

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

/// Returns the path of the current user's home directory if known.
///
/// # Unix
///
/// Returns the value of the `HOME` environment variable if it is set
/// and not equal to the empty string. Otherwise, it tries to determine the
/// home directory by invoking the `getpwuid_r` function on the UID of the
/// current user.
///
/// # Windows
///
/// Returns the value of the `USERPROFILE` environment variable if it
/// is set and not equal to the empty string. If both do not exist,
/// [`SHGetFolderPathW`][msdn] is used to return the appropriate path.
///
/// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpathw
///
/// # Examples
///
/// ```
/// match home::home_dir() {
/// Some(path) => println!("{}", path.display()),
/// None => println!("Impossible to get your home dir!"),
/// }
/// ```
pub fn home_dir() -> Option<PathBuf> {
env::home_dir_with_env(&env::OS_ENV)
}

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

#[cfg(any(unix, target_os = "redox"))]
fn home_dir_inner() -> Option<PathBuf> {
#[allow(deprecated)]
std::env::home_dir()
}

/// Returns the storage directory used by Cargo, often knowns as
/// `.cargo` or `CARGO_HOME`.
///
/// It returns one of the following values, in this order of
/// preference:
///
/// - The value of the `CARGO_HOME` environment variable, if it is
/// an absolute path.
/// - The value of the current working directory joined with the value
/// of the `CARGO_HOME` environment variable, if `CARGO_HOME` is a
/// relative directory.
/// - The `.cargo` directory in the user's home directory, as reported
/// by the `home_dir` function.
///
/// # Errors
///
/// This function fails if it fails to retrieve the current directory,
/// or if the home directory cannot be determined.
///
/// # Examples
///
/// ```
/// match home::cargo_home() {
/// Ok(path) => println!("{}", path.display()),
/// Err(err) => eprintln!("Cannot get your cargo home dir: {:?}", err),
/// }
/// ```
pub fn cargo_home() -> io::Result<PathBuf> {
env::cargo_home_with_env(&env::OS_ENV)
}

/// Returns the storage directory used by Cargo within `cwd`.
/// For more details, see [`cargo_home`](fn.cargo_home.html).
pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
env::cargo_home_with_cwd_env(&env::OS_ENV, cwd)
}

/// Returns the storage directory used by rustup, often knowns as
/// `.rustup` or `RUSTUP_HOME`.
///
/// It returns one of the following values, in this order of
/// preference:
///
/// - The value of the `RUSTUP_HOME` environment variable, if it is
/// an absolute path.
/// - The value of the current working directory joined with the value
/// of the `RUSTUP_HOME` environment variable, if `RUSTUP_HOME` is a
/// relative directory.
/// - The `.rustup` directory in the user's home directory, as reported
/// by the `home_dir` function.
///
/// # Errors
///
/// This function fails if it fails to retrieve the current directory,
/// or if the home directory cannot be determined.
///
/// # Examples
///
/// ```
/// match home::rustup_home() {
/// Ok(path) => println!("{}", path.display()),
/// Err(err) => eprintln!("Cannot get your rustup home dir: {:?}", err),
/// }
/// ```
pub fn rustup_home() -> io::Result<PathBuf> {
env::rustup_home_with_env(&env::OS_ENV)
}

/// Returns the storage directory used by rustup within `cwd`.
/// For more details, see [`rustup_home`](fn.rustup_home.html).
pub fn rustup_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
env::rustup_home_with_cwd_env(&env::OS_ENV, cwd)
}
Loading