Skip to content

Commit e22b452

Browse files
committed
Make path handling more robust wrt to missing home directory
Only check the home directory if `CARGO_HOME` is unset.
1 parent 3fc86ee commit e22b452

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/cargo/util/config.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,25 @@ fn determine_paths(cwd: &Path) -> Option<Paths> {
488488
fs::metadata(&path).ok().map(|_| path)
489489
}
490490

491+
// Strategy to determine where to put files:
492+
//
493+
// 1) Use the environment variable CARGO_HOME if it exists.
494+
// 2) Use the XDG specification if it exists.
495+
// 3) Use the legacy location (~/.cargo) if it exists.
496+
// 4) Fall back to the XDG specification if all of the above things fail.
497+
498+
// 1)
499+
if let Some(v) = env::var_os("CARGO_HOME").map(|p| cwd.join(p)) {
500+
return Some(Paths {
501+
bin: v.clone(),
502+
cache: v.clone(),
503+
config: v,
504+
additional_configs: vec![],
505+
});
506+
}
507+
491508
let user_home = if let Some(p) = env::home_dir() { p } else { return None; };
492509

493-
let home_var = env::var_os("CARGO_HOME").map(|home| cwd.join(home));
494510
let xdg = xdg::BaseDirectories::with_prefix("cargo");
495511
let legacy = user_home.join(".cargo");
496512

@@ -502,26 +518,13 @@ fn determine_paths(cwd: &Path) -> Option<Paths> {
502518
let mut bin: Option<PathBuf>;
503519
let mut cache: Option<PathBuf>;
504520
let mut config: Option<PathBuf>;
505-
let additional_configs: Option<Vec<PathBuf>>;
506-
507-
// Strategy to determine where to put files:
508-
//
509-
// 1) Use the environment variable CARGO_HOME if it exists.
510-
// 2) Use the XDG specification if it exists.
511-
// 3) Use the legacy location (~/.cargo) if it exists.
512-
// 4) Fall back to the XDG specification if all of the above things fail.
513-
514-
// 1)
515-
bin = home_var.clone();
516-
cache = home_var.clone();
517-
config = home_var.clone();
518-
additional_configs = home_var.map(|_| vec![]);
521+
let additional_configs: Vec<PathBuf>;
519522

520523
// 2)
521-
bin = bin.or_else(|| path_exists(bin_xdgish.clone()));
522-
cache = cache.or_else(|| path_exists(cache_xdg.clone()));
523-
config = config.or_else(|| path_exists(config_xdg.clone()));
524-
let additional_configs = additional_configs.unwrap_or(additional_configs_xdg);
524+
bin = path_exists(bin_xdgish.clone());
525+
cache = path_exists(cache_xdg.clone());
526+
config = path_exists(config_xdg.clone());
527+
additional_configs = additional_configs_xdg;
525528

526529
// 3)
527530
if let Some(l) = path_exists(legacy) {

0 commit comments

Comments
 (0)