Skip to content

Commit aef99e0

Browse files
committed
Introduce LibRule::Default to avoid failure mode
The other targets have a no-failure FilterRule::All, but if lib is true and the crate doesn't have a library, then generate_targets fails hard. Let's give library a flexible option.
1 parent 12307fc commit aef99e0

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::command_prelude::*;
22

3-
use cargo::ops::{self, CompileFilter, FilterRule};
3+
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
44

55
pub fn cli() -> App {
66
subcommand("fix")
@@ -127,7 +127,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
127127
if let CompileFilter::Default { .. } = opts.filter {
128128
opts.filter = CompileFilter::Only {
129129
all_targets: true,
130-
lib: true,
130+
lib: LibRule::Default,
131131
bins: FilterRule::All,
132132
examples: FilterRule::All,
133133
benches: FilterRule::All,

src/bin/cargo/commands/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo::ops::{self, CompileFilter, FilterRule};
1+
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
22

33
use crate::command_prelude::*;
44

@@ -111,7 +111,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
111111
}
112112
compile_opts.build_config.mode = CompileMode::Doctest;
113113
compile_opts.filter = ops::CompileFilter::new(
114-
true,
114+
LibRule::True,
115115
FilterRule::none(),
116116
FilterRule::none(),
117117
FilterRule::none(),

src/cargo/ops/cargo_compile.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ impl Packages {
172172
}
173173
}
174174

175+
#[derive(Debug, PartialEq, Eq)]
176+
pub enum LibRule {
177+
/// Include the library, fail if not present
178+
True,
179+
/// Include the library if present
180+
Default,
181+
/// Exclude the library
182+
False,
183+
}
184+
175185
#[derive(Debug)]
176186
pub enum FilterRule {
177187
All,
@@ -186,7 +196,7 @@ pub enum CompileFilter {
186196
},
187197
Only {
188198
all_targets: bool,
189-
lib: bool,
199+
lib: LibRule,
190200
bins: FilterRule,
191201
examples: FilterRule,
192202
tests: FilterRule,
@@ -389,6 +399,7 @@ impl CompileFilter {
389399
all_bens: bool,
390400
all_targets: bool,
391401
) -> CompileFilter {
402+
let rule_lib = if lib_only { LibRule::True } else { LibRule::False };
392403
let rule_bins = FilterRule::new(bins, all_bins);
393404
let rule_tsts = FilterRule::new(tsts, all_tsts);
394405
let rule_exms = FilterRule::new(exms, all_exms);
@@ -397,34 +408,34 @@ impl CompileFilter {
397408
if all_targets {
398409
CompileFilter::Only {
399410
all_targets: true,
400-
lib: true,
411+
lib: LibRule::Default,
401412
bins: FilterRule::All,
402413
examples: FilterRule::All,
403414
benches: FilterRule::All,
404415
tests: FilterRule::All,
405416
}
406417
} else {
407-
CompileFilter::new(lib_only, rule_bins, rule_tsts, rule_exms, rule_bens)
418+
CompileFilter::new(rule_lib, rule_bins, rule_tsts, rule_exms, rule_bens)
408419
}
409420
}
410421

411422
/// Construct a CompileFilter from underlying primitives.
412423
pub fn new(
413-
lib_only: bool,
424+
rule_lib: LibRule,
414425
rule_bins: FilterRule,
415426
rule_tsts: FilterRule,
416427
rule_exms: FilterRule,
417428
rule_bens: FilterRule,
418429
) -> CompileFilter {
419-
if lib_only
430+
if rule_lib == LibRule::True
420431
|| rule_bins.is_specific()
421432
|| rule_tsts.is_specific()
422433
|| rule_exms.is_specific()
423434
|| rule_bens.is_specific()
424435
{
425436
CompileFilter::Only {
426437
all_targets: false,
427-
lib: lib_only,
438+
lib: rule_lib,
428439
bins: rule_bins,
429440
examples: rule_exms,
430441
benches: rule_bens,
@@ -460,7 +471,7 @@ impl CompileFilter {
460471
match *self {
461472
CompileFilter::Default { .. } => true,
462473
CompileFilter::Only {
463-
lib,
474+
ref lib,
464475
ref bins,
465476
ref examples,
466477
ref tests,
@@ -472,7 +483,11 @@ impl CompileFilter {
472483
TargetKind::Test => tests,
473484
TargetKind::Bench => benches,
474485
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => examples,
475-
TargetKind::Lib(..) => return lib,
486+
TargetKind::Lib(..) => return match *lib {
487+
LibRule::True => true,
488+
LibRule::Default => true,
489+
LibRule::False => false,
490+
},
476491
TargetKind::CustomBuild => return false,
477492
};
478493
rule.matches(target)
@@ -626,13 +641,13 @@ fn generate_targets<'a>(
626641
}
627642
CompileFilter::Only {
628643
all_targets,
629-
lib,
644+
ref lib,
630645
ref bins,
631646
ref examples,
632647
ref tests,
633648
ref benches,
634649
} => {
635-
if lib {
650+
if *lib != LibRule::False {
636651
let mut libs = Vec::new();
637652
for proposal in filter_targets(packages, Target::is_lib, false, build_config.mode) {
638653
let Proposal { target, pkg, .. } = proposal;
@@ -646,7 +661,7 @@ fn generate_targets<'a>(
646661
libs.push(proposal)
647662
}
648663
}
649-
if !all_targets && libs.is_empty() {
664+
if !all_targets && libs.is_empty() && *lib == LibRule::True {
650665
let names = packages.iter().map(|pkg| pkg.name()).collect::<Vec<_>>();
651666
if names.len() == 1 {
652667
failure::bail!("no library targets found in package `{}`", names[0]);

src/cargo/ops/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use self::cargo_clean::{clean, CleanOptions};
22
pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOptions};
3-
pub use self::cargo_compile::{CompileFilter, FilterRule, Packages};
3+
pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages};
44
pub use self::cargo_doc::{doc, DocOptions};
55
pub use self::cargo_fetch::{fetch, FetchOptions};
66
pub use self::cargo_generate_lockfile::generate_lockfile;

0 commit comments

Comments
 (0)