Skip to content

Commit 42346b9

Browse files
committed
cargo check,build,rustc --all-targets
`cargo check` does not check all targets by default, and to check all, you need to call it `cargo check --tests --examples --bins --benches`. `cargo check --all-targets` is a shortcut. `--all-targets` is also added to `build` and to `rustc` command. For consitency `--all-targets` added to other commands like `test` although "all targets" is default behavior.
1 parent e7f2374 commit 42346b9

File tree

11 files changed

+67
-12
lines changed

11 files changed

+67
-12
lines changed

src/bin/bench.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Options {
2626
flag_tests: bool,
2727
flag_bench: Vec<String>,
2828
flag_benches: bool,
29+
flag_all_targets: bool,
2930
flag_frozen: bool,
3031
flag_locked: bool,
3132
arg_args: Vec<String>,
@@ -50,6 +51,7 @@ Options:
5051
--tests Benchmark all tests
5152
--bench NAME Benchmark only the specified bench target
5253
--benches Benchmark all benches
54+
--all-targets Benchmark all targets (default)
5355
--no-run Compile, but don't run benchmarks
5456
-p SPEC, --package SPEC ... Package to run benchmarks for
5557
--all Benchmark all packages in the workspace
@@ -115,7 +117,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
115117
&options.flag_bin, options.flag_bins,
116118
&options.flag_test, options.flag_tests,
117119
&options.flag_example, options.flag_examples,
118-
&options.flag_bench, options.flag_benches,),
120+
&options.flag_bench, options.flag_benches,
121+
options.flag_all_targets),
119122
message_format: options.flag_message_format,
120123
target_rustdoc_args: None,
121124
target_rustc_args: None,

src/bin/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Options {
2828
flag_tests: bool,
2929
flag_bench: Vec<String>,
3030
flag_benches: bool,
31+
flag_all_targets: bool,
3132
flag_locked: bool,
3233
flag_frozen: bool,
3334
flag_all: bool,
@@ -55,6 +56,7 @@ Options:
5556
--tests Build all tests
5657
--bench NAME Build only the specified bench target
5758
--benches Build all benches
59+
--all-targets Build all targets (lib and bin targets by default)
5860
--release Build artifacts in release mode, with optimizations
5961
--features FEATURES Space-separated list of features to also build
6062
--all-features Build all available features
@@ -111,7 +113,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
111113
&options.flag_bin, options.flag_bins,
112114
&options.flag_test, options.flag_tests,
113115
&options.flag_example, options.flag_examples,
114-
&options.flag_bench, options.flag_benches,),
116+
&options.flag_bench, options.flag_benches,
117+
options.flag_all_targets),
115118
message_format: options.flag_message_format,
116119
target_rustdoc_args: None,
117120
target_rustc_args: None,

src/bin/check.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Options:
2626
--tests Check all tests
2727
--bench NAME Check only the specified bench target
2828
--benches Check all benches
29+
--all-targets Check all targets (lib and bin targets by default)
2930
--release Check artifacts in release mode, with optimizations
3031
--features FEATURES Space-separated list of features to also check
3132
--all-features Check all available features
@@ -72,6 +73,7 @@ pub struct Options {
7273
flag_tests: bool,
7374
flag_bench: Vec<String>,
7475
flag_benches: bool,
76+
flag_all_targets: bool,
7577
flag_locked: bool,
7678
flag_frozen: bool,
7779
flag_all: bool,
@@ -109,7 +111,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
109111
&options.flag_bin, options.flag_bins,
110112
&options.flag_test, options.flag_tests,
111113
&options.flag_example, options.flag_examples,
112-
&options.flag_bench, options.flag_benches,),
114+
&options.flag_bench, options.flag_benches,
115+
options.flag_all_targets),
113116
message_format: options.flag_message_format,
114117
target_rustdoc_args: None,
115118
target_rustc_args: None,

src/bin/doc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
9898
&options.flag_bin, options.flag_bins,
9999
&empty, false,
100100
&empty, false,
101-
&empty, false),
101+
&empty, false,
102+
false),
102103
message_format: options.flag_message_format,
103104
release: options.flag_release,
104105
mode: ops::CompileMode::Doc {

src/bin/install.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
119119
&options.flag_bin, options.flag_bins,
120120
&[], false,
121121
&options.flag_example, options.flag_examples,
122-
&[], false),
122+
&[], false,
123+
false),
123124
message_format: ops::MessageFormat::Human,
124125
target_rustc_args: None,
125126
target_rustdoc_args: None,

src/bin/run.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
9898
&bins, false,
9999
&[], false,
100100
&examples, false,
101-
&[], false)
101+
&[], false,
102+
false)
102103
},
103104
message_format: options.flag_message_format,
104105
target_rustdoc_args: None,

src/bin/rustc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct Options {
2929
flag_tests: bool,
3030
flag_bench: Vec<String>,
3131
flag_benches: bool,
32+
flag_all_targets: bool,
3233
flag_profile: Option<String>,
3334
flag_frozen: bool,
3435
flag_locked: bool,
@@ -53,6 +54,7 @@ Options:
5354
--tests Build all tests
5455
--bench NAME Build only the specified bench target
5556
--benches Build all benches
57+
--all-targets Build all targets (lib and bin targets by default)
5658
--release Build artifacts in release mode, with optimizations
5759
--profile PROFILE Profile to build the selected target for
5860
--features FEATURES Features to compile for the package
@@ -120,7 +122,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
120122
&options.flag_bin, options.flag_bins,
121123
&options.flag_test, options.flag_tests,
122124
&options.flag_example, options.flag_examples,
123-
&options.flag_bench, options.flag_benches,),
125+
&options.flag_bench, options.flag_benches,
126+
options.flag_all_targets),
124127
message_format: options.flag_message_format,
125128
target_rustdoc_args: None,
126129
target_rustc_args: options.arg_opts.as_ref().map(|a| &a[..]),

src/bin/rustdoc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Options {
2828
flag_tests: bool,
2929
flag_bench: Vec<String>,
3030
flag_benches: bool,
31+
flag_all_targets: bool,
3132
flag_frozen: bool,
3233
flag_locked: bool,
3334
}
@@ -52,6 +53,7 @@ Options:
5253
--tests Build all tests
5354
--bench NAME Build only the specified bench target
5455
--benches Build all benches
56+
--all-targets Build all targets (default)
5557
--release Build artifacts in release mode, with optimizations
5658
--features FEATURES Space-separated list of features to also build
5759
--all-features Build all available features
@@ -105,7 +107,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
105107
&options.flag_bin, options.flag_bins,
106108
&options.flag_test, options.flag_tests,
107109
&options.flag_example, options.flag_examples,
108-
&options.flag_bench, options.flag_benches,),
110+
&options.flag_bench, options.flag_benches,
111+
options.flag_all_targets),
109112
message_format: options.flag_message_format,
110113
mode: ops::CompileMode::Doc { deps: false },
111114
target_rustdoc_args: Some(&options.arg_opts),

src/bin/test.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct Options {
2424
flag_tests: bool,
2525
flag_bench: Vec<String>,
2626
flag_benches: bool,
27+
flag_all_targets: bool,
2728
flag_verbose: u32,
2829
flag_quiet: Option<bool>,
2930
flag_color: Option<String>,
@@ -54,6 +55,7 @@ Options:
5455
--tests Test all tests
5556
--bench NAME ... Test only the specified bench target
5657
--benches Test all benches
58+
--all-targets Test all targets (default)
5759
--no-run Compile, but don't run tests
5860
-p SPEC, --package SPEC ... Package to run tests for
5961
--all Test all packages in the workspace
@@ -122,14 +124,16 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
122124
if options.flag_doc {
123125
mode = ops::CompileMode::Doctest;
124126
filter = ops::CompileFilter::new(true, &empty, false, &empty, false,
125-
&empty, false, &empty, false);
127+
&empty, false, &empty, false,
128+
false);
126129
} else {
127130
mode = ops::CompileMode::Test;
128131
filter = ops::CompileFilter::new(options.flag_lib,
129132
&options.flag_bin, options.flag_bins,
130133
&options.flag_test, options.flag_tests,
131134
&options.flag_example, options.flag_examples,
132-
&options.flag_bench, options.flag_benches);
135+
&options.flag_bench, options.flag_benches,
136+
options.flag_all_targets);
133137
}
134138

135139
let spec = Packages::from_flags(options.flag_all,

src/cargo/ops/cargo_compile.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,20 @@ impl<'a> CompileFilter<'a> {
367367
bins: &'a [String], all_bins: bool,
368368
tsts: &'a [String], all_tsts: bool,
369369
exms: &'a [String], all_exms: bool,
370-
bens: &'a [String], all_bens: bool) -> CompileFilter<'a> {
370+
bens: &'a [String], all_bens: bool,
371+
all_targets: bool) -> CompileFilter<'a> {
371372
let rule_bins = FilterRule::new(bins, all_bins);
372373
let rule_tsts = FilterRule::new(tsts, all_tsts);
373374
let rule_exms = FilterRule::new(exms, all_exms);
374375
let rule_bens = FilterRule::new(bens, all_bens);
375376

376-
if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
377+
if all_targets {
378+
CompileFilter::Only {
379+
lib: true, bins: FilterRule::All,
380+
examples: FilterRule::All, benches: FilterRule::All,
381+
tests: FilterRule::All,
382+
}
383+
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
377384
|| rule_exms.is_specific() || rule_bens.is_specific() {
378385
CompileFilter::Only {
379386
lib: lib_only, bins: rule_bins,

tests/check.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,29 @@ fn check_all() {
393393
.with_stderr_contains("[..] --crate-name b b[/]src[/]main.rs [..]")
394394
);
395395
}
396+
397+
#[test]
398+
fn check_all_targets() {
399+
let foo = project("foo")
400+
.file("Cargo.toml", r#"
401+
[package]
402+
name = "foo"
403+
version = "0.0.1"
404+
authors = []
405+
"#)
406+
.file("src/main.rs", "fn main() {}")
407+
.file("src/lib.rs", "pub fn smth() {}")
408+
.file("examples/example1.rs", "fn main() {}")
409+
.file("tests/test2.rs", "#[test] fn t() {}")
410+
.file("benches/bench3.rs", "")
411+
;
412+
413+
assert_that(foo.cargo_process("check").arg("--all-targets").arg("-v"),
414+
execs().with_status(0)
415+
.with_stderr_contains("[..] --crate-name foo src[/]lib.rs [..]")
416+
.with_stderr_contains("[..] --crate-name foo src[/]main.rs [..]")
417+
.with_stderr_contains("[..] --crate-name example1 examples[/]example1.rs [..]")
418+
.with_stderr_contains("[..] --crate-name test2 tests[/]test2.rs [..]")
419+
.with_stderr_contains("[..] --crate-name bench3 benches[/]bench3.rs [..]")
420+
);
421+
}

0 commit comments

Comments
 (0)