diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index fe16ad52fbc..815fe528e51 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -79,6 +79,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { if let CompileFilter::Default { .. } = opts.filter { opts.filter = CompileFilter::Only { all_targets: true, + warn_unmatched: true, lib: LibRule::Default, bins: FilterRule::All, examples: FilterRule::All, diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 922eaddf3eb..be31ff8766f 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -1,7 +1,7 @@ use crate::command_prelude::*; use cargo::core::{GitReference, SourceId}; -use cargo::ops; +use cargo::ops::{self, CompileFilter}; use cargo::util::IntoUrl; pub fn cli() -> App { @@ -137,6 +137,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { compile_opts.build_config.requested_profile = args.get_profile_name(config, "release", ProfileChecking::Custom)?; + if !compile_opts.filter.is_specific() { + compile_opts.filter = CompileFilter::new_bare_install(); + } + if args.is_present("list") { ops::install_list(root, config)?; } else { diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index d37f304644c..62349c8d38b 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -249,6 +249,8 @@ pub enum CompileFilter { }, Only { all_targets: bool, + /// Flag whether to warn when there are unmatched target filters, defaulting to true. + warn_unmatched: bool, lib: LibRule, bins: FilterRule, examples: FilterRule, @@ -714,6 +716,7 @@ impl CompileFilter { { CompileFilter::Only { all_targets: false, + warn_unmatched: true, lib: rule_lib, bins: rule_bins, examples: rule_exms, @@ -730,6 +733,7 @@ impl CompileFilter { pub fn new_all_targets() -> CompileFilter { CompileFilter::Only { all_targets: true, + warn_unmatched: true, lib: LibRule::Default, bins: FilterRule::All, examples: FilterRule::All, @@ -738,6 +742,19 @@ impl CompileFilter { } } + /// Construct a CompileFilter with default options for use by `cargo install` + pub fn new_bare_install() -> CompileFilter { + CompileFilter::Only { + all_targets: false, + warn_unmatched: false, + lib: LibRule::False, + bins: FilterRule::All, + examples: FilterRule::none(), + benches: FilterRule::none(), + tests: FilterRule::none(), + } + } + pub fn need_dev_deps(&self, mode: CompileMode) -> bool { match mode { CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true, @@ -796,6 +813,21 @@ impl CompileFilter { } } + pub fn is_bare_install(&self) -> bool { + match *self { + CompileFilter::Default { .. } => false, + CompileFilter::Only { + bins: FilterRule::All, + ref examples, + .. + } => match examples { + FilterRule::All => false, + FilterRule::Just(ref targets) => targets.is_empty(), + }, + _ => !self.is_specific(), + } + } + pub fn is_all_targets(&self) -> bool { matches!( *self, @@ -1003,6 +1035,7 @@ fn generate_targets( ref examples, ref tests, ref benches, + .. } => { if *lib != LibRule::False { let mut libs = Vec::new(); @@ -1158,14 +1191,15 @@ fn unmatched_target_filters( ) -> CargoResult<()> { if let CompileFilter::Only { all_targets, - lib: _, + warn_unmatched, ref bins, ref examples, ref tests, ref benches, + .. } = *filter { - if units.is_empty() { + if units.is_empty() && warn_unmatched { let mut filters = String::new(); let mut miss_count = 0; diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index ad62de96d52..48d51d092ae 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -204,7 +204,7 @@ impl<'cfg, 'a> InstallablePackage<'cfg, 'a> { // For bare `cargo install` (no `--bin` or `--example`), check if there is // *something* to install. Explicit `--bin` or `--example` flags will be // checked at the start of `compile_ws`. - if !opts.filter.is_specific() && !pkg.targets().iter().any(|t| t.is_bin()) { + if opts.filter.is_bare_install() && !pkg.targets().iter().any(|t| t.is_bin()) { bail!( "there is nothing to install in `{}`, because it has no binaries\n\ `cargo install` is only for installing programs, and can't be used with libraries.\n\