mirror of
https://github.com/rust-lang/cargo
synced 2024-09-18 07:12:12 +00:00
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:
parent
b1e1d388ea
commit
d9534bf498
|
@ -36,7 +36,7 @@ home = "0.3"
|
||||||
ignore = "0.4"
|
ignore = "0.4"
|
||||||
lazy_static = "1.0.0"
|
lazy_static = "1.0.0"
|
||||||
jobserver = "0.1.11"
|
jobserver = "0.1.11"
|
||||||
lazycell = "1.0"
|
lazycell = "1.2.0"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
libgit2-sys = "0.7.5"
|
libgit2-sys = "0.7.5"
|
||||||
|
|
|
@ -74,12 +74,9 @@ continuous integration systems.",
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
|
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)?;
|
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");
|
compile_opts.build_config.release = !args.is_present("debug");
|
||||||
|
|
||||||
let krates = args.values_of("crate")
|
let krates = args.values_of("crate")
|
||||||
|
|
|
@ -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 {
|
pub fn cwd(&self) -> &Path {
|
||||||
&self.cwd
|
&self.cwd
|
||||||
}
|
}
|
||||||
|
@ -611,9 +618,16 @@ impl Config {
|
||||||
|
|
||||||
/// Loads configuration from the filesystem
|
/// Loads configuration from the filesystem
|
||||||
pub fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
|
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 contents = String::new();
|
||||||
let mut file = File::open(&path)?;
|
let mut file = File::open(&path)?;
|
||||||
file.read_to_string(&mut contents)
|
file.read_to_string(&mut contents)
|
||||||
|
@ -1535,7 +1549,7 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
|
||||||
::home::cargo_home_with_cwd(cwd).ok()
|
::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
|
where
|
||||||
F: FnMut(&Path) -> CargoResult<()>,
|
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
|
// 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
|
// in our history to be sure we pick up that standard location for
|
||||||
// information.
|
// 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");
|
let config = home.join("config");
|
||||||
if !stash.contains(&config) && fs::metadata(&config).is_ok() {
|
if !stash.contains(&config) && fs::metadata(&config).is_ok() {
|
||||||
walk(&config)?;
|
walk(&config)?;
|
||||||
|
|
|
@ -1256,7 +1256,7 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn install_ignores_cargo_config() {
|
fn install_ignores_local_cargo_config() {
|
||||||
pkg("bar", "0.0.1");
|
pkg("bar", "0.0.1");
|
||||||
|
|
||||||
let p = project()
|
let p = project()
|
||||||
|
@ -1273,3 +1273,22 @@ fn install_ignores_cargo_config() {
|
||||||
p.cargo("install bar").run();
|
p.cargo("install bar").run();
|
||||||
assert_has_installed_exe(cargo_home(), "bar");
|
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();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue