Fix helpers::cargo_home: home::cargo_home is buggy

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-23 19:08:29 +10:00
parent 3c09cfb196
commit 5bf2b4e45d
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,3 +1,4 @@
use std::env;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::io; use std::io;
@ -44,10 +45,34 @@ mod crate_name;
pub use crate_name::CrateName; pub use crate_name::CrateName;
pub fn cargo_home() -> Result<&'static Path, io::Error> { pub fn cargo_home() -> Result<&'static Path, io::Error> {
fn cargo_home_inner() -> Result<PathBuf, io::Error> {
if let Some(p) = env::var_os("CARGO_HOME") {
let p = PathBuf::from(p);
debug!("using CARGO_HOME ({})", p.display());
return Ok(p);
}
// Standard $HOME/.cargo/bin
if let Some(mut d) = dirs::home_dir() {
d.push(".cargo");
if d.exists() {
debug!("using $HOME/.cargo: {}", d.display());
return Ok(d);
}
}
Err(io::Error::new(
io::ErrorKind::NotFound,
"Failed to detect cargo home",
))
}
static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new(); static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
CARGO_HOME CARGO_HOME
.get_or_try_init(home::cargo_home) .get_or_try_init(cargo_home_inner)
.map(ops::Deref::deref) .map(ops::Deref::deref)
} }