Skip to content

Commit 9a131b5

Browse files
committed
install fails earlier when no binaries can be found
this automatically filters out the lib targets in `cargo install` when no specific targets are provided by the user, so they won't get accidentally built. now when you try to install a crate with no satisfied bin targets, cargo will skip the meat of the compile phase and give the same error message it currently gives when it finds no bin targets at all. a new flag called `warn_unmatched` was also added to `CompileFilter::Only` that can suppress the warning about unmatched target filters. fixes #8970
1 parent 77a0379 commit 9a131b5

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/bin/cargo/commands/fix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
7979
if let CompileFilter::Default { .. } = opts.filter {
8080
opts.filter = CompileFilter::Only {
8181
all_targets: true,
82+
warn_unmatched: true,
8283
lib: LibRule::Default,
8384
bins: FilterRule::All,
8485
examples: FilterRule::All,

src/bin/cargo/commands/install.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::command_prelude::*;
22

33
use cargo::core::{GitReference, SourceId};
4-
use cargo::ops;
4+
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
55
use cargo::util::IntoUrl;
66

77
pub fn cli() -> App {
@@ -137,6 +137,18 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
137137
compile_opts.build_config.requested_profile =
138138
args.get_profile_name(config, "release", ProfileChecking::Custom)?;
139139

140+
if !compile_opts.filter.is_specific() {
141+
compile_opts.filter = CompileFilter::Only {
142+
all_targets: false,
143+
warn_unmatched: false,
144+
lib: LibRule::False,
145+
bins: FilterRule::All,
146+
examples: FilterRule::none(),
147+
benches: FilterRule::none(),
148+
tests: FilterRule::none(),
149+
}
150+
}
151+
140152
if args.is_present("list") {
141153
ops::install_list(root, config)?;
142154
} else {

src/cargo/ops/cargo_compile.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ pub enum CompileFilter {
249249
},
250250
Only {
251251
all_targets: bool,
252+
warn_unmatched: bool,
252253
lib: LibRule,
253254
bins: FilterRule,
254255
examples: FilterRule,
@@ -714,6 +715,7 @@ impl CompileFilter {
714715
{
715716
CompileFilter::Only {
716717
all_targets: false,
718+
warn_unmatched: true,
717719
lib: rule_lib,
718720
bins: rule_bins,
719721
examples: rule_exms,
@@ -730,6 +732,7 @@ impl CompileFilter {
730732
pub fn new_all_targets() -> CompileFilter {
731733
CompileFilter::Only {
732734
all_targets: true,
735+
warn_unmatched: true,
733736
lib: LibRule::Default,
734737
bins: FilterRule::All,
735738
examples: FilterRule::All,
@@ -1003,6 +1006,7 @@ fn generate_targets(
10031006
ref examples,
10041007
ref tests,
10051008
ref benches,
1009+
..
10061010
} => {
10071011
if *lib != LibRule::False {
10081012
let mut libs = Vec::new();
@@ -1158,14 +1162,15 @@ fn unmatched_target_filters(
11581162
) -> CargoResult<()> {
11591163
if let CompileFilter::Only {
11601164
all_targets,
1161-
lib: _,
1165+
warn_unmatched,
11621166
ref bins,
11631167
ref examples,
11641168
ref tests,
11651169
ref benches,
1170+
..
11661171
} = *filter
11671172
{
1168-
if units.is_empty() {
1173+
if units.is_empty() && warn_unmatched {
11691174
let mut filters = String::new();
11701175
let mut miss_count = 0;
11711176

tests/testsuite/install.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ fn no_binaries() {
586586
.with_status(101)
587587
.with_stderr(
588588
"\
589-
[ERROR] there is nothing to install in `foo v0.0.1 ([..])`, because it has no binaries[..]
590589
[..]
591-
[..]",
590+
[..]
591+
[ERROR] no binaries are available for install using the selected features",
592592
)
593593
.run();
594594
}

0 commit comments

Comments
 (0)