Skip to content

Commit 77e02e7

Browse files
committed
Auto merge of #6026 - alexcrichton:install-config, r=ehuss
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
2 parents de314a8 + d9534bf commit 77e02e7

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ home = "0.3"
3636
ignore = "0.4"
3737
lazy_static = "1.0.0"
3838
jobserver = "0.1.11"
39-
lazycell = "1.0"
39+
lazycell = "1.2.0"
4040
libc = "0.2"
4141
log = "0.4"
4242
libgit2-sys = "0.7.5"

src/bin/cargo/commands/install.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ continuous integration systems.",
7474
}
7575

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

79-
// for `cargo-install` we want to use what the user specified via `--target` and ignore what's
80-
// in `.cargo/config` and what the environment says
81-
compile_opts.build_config.requested_target = args.target();
82-
8380
compile_opts.build_config.release = !args.is_present("debug");
8481

8582
let krates = args.values_of("crate")

src/cargo/util/config.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ impl Config {
281281
}
282282
}
283283

284+
pub fn reload_rooted_at_cargo_home(&mut self) -> CargoResult<()> {
285+
let home = self.home_path.clone().into_path_unlocked();
286+
let values = self.load_values_from(&home)?;
287+
self.values.replace(values);
288+
Ok(())
289+
}
290+
284291
pub fn cwd(&self) -> &Path {
285292
&self.cwd
286293
}
@@ -611,9 +618,16 @@ impl Config {
611618

612619
/// Loads configuration from the filesystem
613620
pub fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
621+
self.load_values_from(&self.cwd)
622+
}
623+
624+
fn load_values_from(&self, path: &Path)
625+
-> CargoResult<HashMap<String, ConfigValue>>
626+
{
614627
let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));
628+
let home = self.home_path.clone().into_path_unlocked();
615629

616-
walk_tree(&self.cwd, |path| {
630+
walk_tree(path, &home, |path| {
617631
let mut contents = String::new();
618632
let mut file = File::open(&path)?;
619633
file.read_to_string(&mut contents)
@@ -1535,7 +1549,7 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
15351549
::home::cargo_home_with_cwd(cwd).ok()
15361550
}
15371551

1538-
fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
1552+
fn walk_tree<F>(pwd: &Path, home: &Path, mut walk: F) -> CargoResult<()>
15391553
where
15401554
F: FnMut(&Path) -> CargoResult<()>,
15411555
{
@@ -1552,12 +1566,6 @@ where
15521566
// Once we're done, also be sure to walk the home directory even if it's not
15531567
// in our history to be sure we pick up that standard location for
15541568
// information.
1555-
let home = homedir(pwd).ok_or_else(|| {
1556-
format_err!(
1557-
"Cargo couldn't find your home directory. \
1558-
This probably means that $HOME was not set."
1559-
)
1560-
})?;
15611569
let config = home.join("config");
15621570
if !stash.contains(&config) && fs::metadata(&config).is_ok() {
15631571
walk(&config)?;

tests/testsuite/install.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1256,20 +1256,39 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina
12561256
}
12571257

12581258
#[test]
1259-
fn install_ignores_cargo_config() {
1259+
fn install_ignores_local_cargo_config() {
12601260
pkg("bar", "0.0.1");
12611261

12621262
let p = project()
12631263
.file(
12641264
".cargo/config",
12651265
r#"
1266-
[build]
1267-
target = "non-existing-target"
1268-
"#,
1266+
[build]
1267+
target = "non-existing-target"
1268+
"#,
12691269
)
12701270
.file("src/main.rs", "fn main() {}")
12711271
.build();
12721272

12731273
p.cargo("install bar").run();
12741274
assert_has_installed_exe(cargo_home(), "bar");
12751275
}
1276+
1277+
#[test]
1278+
fn install_global_cargo_config() {
1279+
pkg("bar", "0.0.1");
1280+
1281+
let config = cargo_home().join("config");
1282+
let mut toml = fs::read_to_string(&config).unwrap_or(String::new());
1283+
1284+
toml.push_str(r#"
1285+
[build]
1286+
target = 'nonexistent'
1287+
"#);
1288+
fs::write(&config, toml).unwrap();
1289+
1290+
cargo_process("install bar")
1291+
.with_status(101)
1292+
.with_stderr_contains("[..]--target nonexistent[..]")
1293+
.run();
1294+
}

0 commit comments

Comments
 (0)