Skip to content

Commit

Permalink
Only load ~/.cargo/config for cargo install
Browse files Browse the repository at this point in the history
This commit tweaks how configuration is loaded for `cargo install`, ensuring
that we only load configuration from `$HOME` instead of the current working
directory. This should make installations a little more consistent in that they
probably shouldn't cover project-local configuration but should respect global
configuration!

Closes #6025
  • Loading branch information
alexcrichton committed Sep 21, 2018
1 parent b1e1d38 commit d9534bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ home = "0.3"
ignore = "0.4"
lazy_static = "1.0.0"
jobserver = "0.1.11"
lazycell = "1.0"
lazycell = "1.2.0"
libc = "0.2"
log = "0.4"
libgit2-sys = "0.7.5"
Expand Down
5 changes: 1 addition & 4 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ continuous integration systems.",
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
config.reload_rooted_at_cargo_home()?;
let mut compile_opts = args.compile_options(config, CompileMode::Build)?;

// for `cargo-install` we want to use what the user specified via `--target` and ignore what's
// in `.cargo/config` and what the environment says
compile_opts.build_config.requested_target = args.target();

compile_opts.build_config.release = !args.is_present("debug");

let krates = args.values_of("crate")
Expand Down
24 changes: 16 additions & 8 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ impl Config {
}
}

pub fn reload_rooted_at_cargo_home(&mut self) -> CargoResult<()> {
let home = self.home_path.clone().into_path_unlocked();
let values = self.load_values_from(&home)?;
self.values.replace(values);
Ok(())
}

pub fn cwd(&self) -> &Path {
&self.cwd
}
Expand Down Expand Up @@ -611,9 +618,16 @@ impl Config {

/// Loads configuration from the filesystem
pub fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
self.load_values_from(&self.cwd)
}

fn load_values_from(&self, path: &Path)
-> CargoResult<HashMap<String, ConfigValue>>
{
let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));
let home = self.home_path.clone().into_path_unlocked();

walk_tree(&self.cwd, |path| {
walk_tree(path, &home, |path| {
let mut contents = String::new();
let mut file = File::open(&path)?;
file.read_to_string(&mut contents)
Expand Down Expand Up @@ -1535,7 +1549,7 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
::home::cargo_home_with_cwd(cwd).ok()
}

fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
fn walk_tree<F>(pwd: &Path, home: &Path, mut walk: F) -> CargoResult<()>
where
F: FnMut(&Path) -> CargoResult<()>,
{
Expand All @@ -1552,12 +1566,6 @@ where
// Once we're done, also be sure to walk the home directory even if it's not
// in our history to be sure we pick up that standard location for
// information.
let home = homedir(pwd).ok_or_else(|| {
format_err!(
"Cargo couldn't find your home directory. \
This probably means that $HOME was not set."
)
})?;
let config = home.join("config");
if !stash.contains(&config) && fs::metadata(&config).is_ok() {
walk(&config)?;
Expand Down
27 changes: 23 additions & 4 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,20 +1256,39 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
}

#[test]
fn install_ignores_cargo_config() {
fn install_ignores_local_cargo_config() {
pkg("bar", "0.0.1");

let p = project()
.file(
".cargo/config",
r#"
[build]
target = "non-existing-target"
"#,
[build]
target = "non-existing-target"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("install bar").run();
assert_has_installed_exe(cargo_home(), "bar");
}

#[test]
fn install_global_cargo_config() {
pkg("bar", "0.0.1");

let config = cargo_home().join("config");
let mut toml = fs::read_to_string(&config).unwrap_or(String::new());

toml.push_str(r#"
[build]
target = 'nonexistent'
"#);
fs::write(&config, toml).unwrap();

cargo_process("install bar")
.with_status(101)
.with_stderr_contains("[..]--target nonexistent[..]")
.run();
}

0 comments on commit d9534bf

Please sign in to comment.