Only load ~/.cargo/config for cargo install

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
This commit is contained in:
Alex Crichton 2018-09-14 10:42:27 -07:00
parent b1e1d388ea
commit d9534bf498
4 changed files with 42 additions and 18 deletions

View file

@ -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"

View file

@ -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")

View file

@ -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
}
@ -611,9 +618,16 @@ impl Config {
/// Loads configuration from the filesystem
pub fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));
self.load_values_from(&self.cwd)
}
walk_tree(&self.cwd, |path| {
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(path, &home, |path| {
let mut contents = String::new();
let mut file = File::open(&path)?;
file.read_to_string(&mut contents)
@ -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<()>,
{
@ -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)?;

View file

@ -1256,16 +1256,16 @@ 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();
@ -1273,3 +1273,22 @@ fn install_ignores_cargo_config() {
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();
}