Skip to content

Commit

Permalink
make cargo install ignore .cargo/config
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Sep 1, 2018
1 parent aecaef3 commit e620865
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 9 additions & 2 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

0 comments on commit e620865

Please sign in to comment.