Skip to content

Allow setting cargo home from .cargo/home #9154

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 32 additions & 9 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use std::str::FromStr;
use std::sync::Once;
use std::time::Instant;

use anyhow::{anyhow, bail, format_err};
use anyhow::{anyhow, bail, format_err, Context};
use curl::easy::Easy;
use lazycell::LazyCell;
use serde::Deserialize;
Expand Down Expand Up @@ -260,12 +260,7 @@ impl Config {
let shell = Shell::new();
let cwd =
env::current_dir().chain_err(|| "couldn't get the current directory of the process")?;
let homedir = homedir(&cwd).ok_or_else(|| {
anyhow!(
"Cargo couldn't find your home directory. \
This probably means that $HOME was not set."
)
})?;
let homedir = homedir(&cwd)?;
Ok(Config::new(shell, cwd, homedir))
}

Expand Down Expand Up @@ -1618,8 +1613,36 @@ impl ConfigValue {
}
}

pub fn homedir(cwd: &Path) -> Option<PathBuf> {
::home::cargo_home_with_cwd(cwd).ok()
pub fn homedir(cwd: &Path) -> CargoResult<PathBuf> {
if std::env::var_os("CARGO_HOME")
.filter(|h| !h.is_empty())
.is_none()
{
// CARGO_HOME is not set -- look for a .cargo/home file instead
let mut components = cwd.components();
loop {
let cargo_home = components.as_path().join(".cargo/home");
if cargo_home.exists() {
let home = std::fs::read_to_string(&cargo_home).with_context(|| {
format!(
"Cargo could not follow home pointer in '{}'",
cargo_home.display()
)
})?;
return Ok(PathBuf::from(home));
}
if components.next_back().is_none() {
break;
}
}
}

::home::cargo_home_with_cwd(cwd).map_err(|_| {
anyhow!(
"Cargo couldn't find your home directory. \
This probably means that $HOME was not set."
)
})
}

pub fn save_credentials(
Expand Down
79 changes: 79 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,85 @@ fn override_cargo_home() {
assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
}

#[cargo_test]
fn cargo_home_pointer() {
let root = paths::root();
let my_home = root.join("my_home");
fs::create_dir(&my_home).unwrap();
fs::write(
&my_home.join("config"),
r#"
[cargo-new]
name = "foo"
email = "bar"
git = false
"#,
)
.unwrap();

fs::create_dir(root.join(".cargo")).unwrap();
fs::write(
root.join(".cargo/home"),
my_home.as_os_str().to_str().unwrap().as_bytes(),
)
.unwrap();

cargo_process("new foo")
.env("USER", "foo")
.env_remove("CARGO_HOME")
.run();

let toml = paths::root().join("foo/Cargo.toml");
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
}

#[cargo_test]
fn cargo_home_prefer_env() {
let root = paths::root();
let my_home = root.join("my_home");
fs::create_dir(&my_home).unwrap();
fs::write(
&my_home.join("config"),
r#"
[cargo-new]
name = "foo"
email = "bar"
git = false
"#,
)
.unwrap();

let my_other_home = root.join("my_other_home");
fs::create_dir(&my_other_home).unwrap();
fs::write(
&my_other_home.join("config"),
r#"
[cargo-new]
name = "foo"
email = "baz"
git = false
"#,
)
.unwrap();

fs::create_dir(root.join(".cargo")).unwrap();
fs::write(
root.join(".cargo/home"),
my_other_home.as_os_str().to_str().unwrap().as_bytes(),
)
.unwrap();

cargo_process("new foo")
.env("USER", "foo")
.env("CARGO_HOME", &my_home)
.run();

let toml = paths::root().join("foo/Cargo.toml");
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
}

#[cargo_test]
fn cargo_subcommand_env() {
let src = format!(
Expand Down