Skip to content

Commit

Permalink
Make home dependency Windows-only dependency
Browse files Browse the repository at this point in the history
Use the home crate only on Windows which std::env::home_dir is not
correct.
  • Loading branch information
taiki-e committed Jan 25, 2024
1 parent b822f19 commit 3802942
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Make `home` dependency Windows-only dependency.

## [0.6.3] - 2024-01-24

- Fix "The file was not recognized as a valid object file" error with `--doc`/`--doctests` flag on WSL. ([#343](https://github.com/taiki-e/cargo-llvm-cov/pull/343))
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ cargo-config2 = "0.1.5"
duct = "0.13.1"
fs-err = "2.5"
glob = "0.3"
home = "0.5"
is_executable = "1"
lcov2cobertura = "1.0.1"
lexopt = "0.3"
Expand All @@ -41,6 +40,9 @@ shell-escape = "0.1.5"
termcolor = "1.1.2"
walkdir = "2.2.3"

[target.'cfg(windows)'.dependencies]
home = "0.5"

[dev-dependencies]
easy-ext = "1"
rustversion = "1"
Expand Down
51 changes: 47 additions & 4 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

pub(crate) use std::env::*;
use std::{env, ffi::OsString};
use std::{
ffi::OsString,
path::{Path, PathBuf},
};

use anyhow::Result;

pub(crate) fn var(key: &str) -> Result<Option<String>> {
match env::var(key) {
match std::env::var(key) {
Ok(v) if v.is_empty() => Ok(None),
Ok(v) => Ok(Some(v)),
Err(env::VarError::NotPresent) => Ok(None),
Err(VarError::NotPresent) => Ok(None),
Err(e) => Err(e.into()),
}
}

pub(crate) fn var_os(key: &str) -> Option<OsString> {
env::var_os(key).filter(|v| !v.is_empty())
std::env::var_os(key).filter(|v| !v.is_empty())
}

// Use the home crate only on Windows which std::env::home_dir is not correct.
// https://github.com/rust-lang/cargo/blob/b2e1d3b6235c07221dd0fcac54a7b0c754ef8b11/crates/home/src/lib.rs#L65-L72
#[cfg(windows)]
pub(crate) use home::home_dir;
#[cfg(not(windows))]
pub(crate) fn home_dir() -> Option<PathBuf> {
#[allow(deprecated)]
std::env::home_dir()
}

// Follow the cargo's behavior.
// https://github.com/rust-lang/cargo/blob/b2e1d3b6235c07221dd0fcac54a7b0c754ef8b11/crates/home/src/lib.rs#L77-L86
// https://github.com/rust-lang/cargo/blob/b2e1d3b6235c07221dd0fcac54a7b0c754ef8b11/crates/home/src/lib.rs#L114-L123
// https://github.com/rust-lang/cargo/blob/b2e1d3b6235c07221dd0fcac54a7b0c754ef8b11/crates/home/src/env.rs#L63-L77
// https://github.com/rust-lang/cargo/blob/b2e1d3b6235c07221dd0fcac54a7b0c754ef8b11/crates/home/src/env.rs#L92-L106
pub(crate) fn cargo_home_with_cwd(cwd: &Path) -> Option<PathBuf> {
match var_os("CARGO_HOME").map(PathBuf::from) {
Some(home) => {
if home.is_absolute() {
Some(home)
} else {
Some(cwd.join(home))
}
}
_ => Some(home_dir()?.join(".cargo")),
}
}
pub(crate) fn rustup_home_with_cwd(cwd: &Path) -> Option<PathBuf> {
match var_os("RUSTUP_HOME").map(PathBuf::from) {
Some(home) => {
if home.is_absolute() {
Some(home)
} else {
Some(cwd.join(home))
}
}
_ => Some(home_dir()?.join(".rustup")),
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,16 +1152,16 @@ fn ignore_filename_regex(cx: &Context) -> Option<String> {
}
out.push_abs_path(&cx.ws.target_dir);
if cx.args.remap_path_prefix {
if let Some(path) = home::home_dir() {
if let Some(path) = env::home_dir() {
out.push_abs_path(path);
}
}
if let Ok(path) = home::cargo_home() {
if let Some(path) = env::cargo_home_with_cwd(&cx.current_dir) {
let path = regex::escape(&path.as_os_str().to_string_lossy());
let path = format!("^{path}{SEPARATOR}(registry|git){SEPARATOR}");
out.push(path);
}
if let Ok(path) = home::rustup_home() {
if let Some(path) = env::rustup_home_with_cwd(&cx.current_dir) {
out.push_abs_path(path.join("toolchains"));
}
for path in resolve_excluded_paths(cx) {
Expand Down

0 comments on commit 3802942

Please sign in to comment.