From e620865633c3842297fe791cc4fedbda01ca348d Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 8 Aug 2018 10:09:54 -0500 Subject: [PATCH] make `cargo install` ignore .cargo/config closes #5850 --- src/bin/cargo/commands/install.rs | 2 +- src/cargo/core/compiler/build_config.rs | 11 +++++++-- src/cargo/core/profiles.rs | 1 + src/cargo/ops/cargo_compile.rs | 7 ++++-- tests/testsuite/install.rs | 30 +++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 94f9f8c5791..2042b957f16 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -74,7 +74,7 @@ continuous integration systems.", } pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { - let mut compile_opts = args.compile_options(config, CompileMode::Build)?; + let mut compile_opts = args.compile_options(config, CompileMode::Install)?; compile_opts.build_config.release = !args.is_present("debug"); let krates = args.values_of("crate") diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 77ed087eee3..39c29feac82 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -60,8 +60,13 @@ impl BuildConfig { bail!("target was empty") } } - let cfg_target = config.get_string("build.target")?.map(|s| s.val); - let target = requested_target.clone().or(cfg_target); + let target = if mode == CompileMode::Install { + // ignore `.cargo/config` when compiling for `cargo install` + requested_target + } else { + let cfg_target = config.get_string("build.target")?.map(|s| s.val); + requested_target.clone().or(cfg_target) + }; if jobs == Some(0) { bail!("jobs must be at least 1") @@ -131,6 +136,8 @@ pub enum CompileMode { Doc { deps: bool }, /// A target that will be tested with `rustdoc`. Doctest, + // Like `Build` but we are compiling something that will be installed + Install, /// A marker for Units that represent the execution of a `build.rs` /// script. RunCustomBuild, diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 2a822022af4..1056e4dd11e 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -84,6 +84,7 @@ impl Profiles { CompileMode::Build | CompileMode::Check { .. } | CompileMode::Doctest + | CompileMode::Install | CompileMode::RunCustomBuild => { // Note: RunCustomBuild doesn't normally use this code path. // `build_unit_profiles` normally ensures that it selects the diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index fe5623ce62a..73417a3eaca 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -386,7 +386,10 @@ impl CompileFilter { pub fn need_dev_deps(&self, mode: CompileMode) -> bool { match mode { CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true, - CompileMode::Build | CompileMode::Doc { .. } | CompileMode::Check { .. } => match *self + CompileMode::Build + | CompileMode::Doc { .. } + | CompileMode::Check { .. } + | CompileMode::Install => match *self { CompileFilter::Default { .. } => false, CompileFilter::Only { @@ -707,7 +710,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target> .iter() .filter(|t| t.tested() || t.is_example()) .collect(), - CompileMode::Build | CompileMode::Check { .. } => targets + CompileMode::Build | CompileMode::Check { .. } | CompileMode::Install => targets .iter() .filter(|t| t.is_bin() || t.is_lib()) .collect(), diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 0c5cb3f076f..192ff89a314 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -1237,3 +1237,33 @@ warning: be sure to add `[..]` to your PATH to be able to run the installed bina ", ).run(); } + +fn install_ignores_cargo_config() { + pkg("bar", "0.0.1"); + + let p = project("foo") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + authors = [] + "#, + ) + .file( + ".cargo/config", + r#" + [build] + target = "non-existing-target" + "#, + ) + .file("src/main.rs", "fn main() {}") + .build(); + + assert_that( + cargo_process("install").arg("bar").cwd(p.root()), + execs().with_status(0), + ); + assert_that(cargo_home(), has_installed_exe("bar")); +}