Auto merge of #9549 - Bryysen:master, r=ehuss

Warn if an "all" target is specified, but we don't match anything

If a combination of --bins, --benches, --examples, --tests flags have
been specified, but we didn't match on anything after resolving the unit-list,
we emit a warning to make it clear that cargo didn't do anything and that the
code is unchecked.

This is my first PR and there are a couple things that I'm unsure about
* The integration test covers only one case (ideally it should cover every combination of the above mentioned flags the user can pass). I figured since the warning function is so simple, it'd best not to clog the testsuite with unnecessary `p.cargo().runs()` and whatnot. If I should make the test more comprehensive I can do that, it's also very easy to write unit tests so i can do that as well if needed.
* I figure we don't actually have to check the `--all-targets`, but i'm doing so for consistency. I also didn't check for the `--lib` flag at all because (I'm assuming) if the user passes `--lib` and there are no libraries, we error.
Edit: I notice (among other things) we sometimes silently skip certain units that have incompatible feature flags (see [here](ed0c8c6d66/src/cargo/ops/cargo_compile.rs (L1140))) so maybe we should be checking the `--lib` flag after all, in the event that a library was silently skipped and we no-opped 🤔

And thanks to `@ehuss` for taking the time to answer my questions and helping me through the contribution process, much appreciated

Closes #9536
This commit is contained in:
bors 2021-06-09 18:00:26 +00:00
commit 46ba901448
2 changed files with 81 additions and 0 deletions

View file

@ -1140,11 +1140,63 @@ fn generate_targets(
// else, silently skip target.
}
let mut units: Vec<_> = units.into_iter().collect();
unmatched_target_filters(&units, &filter, &mut ws.config().shell())?;
// Keep the roots in a consistent order, which helps with checking test output.
units.sort_unstable();
Ok(units)
}
/// Checks if the unit list is empty and the user has passed any combination of
/// --tests, --examples, --benches or --bins, and we didn't match on any targets.
/// We want to emit a warning to make sure the user knows that this run is a no-op,
/// and their code remains unchecked despite cargo not returning any errors
fn unmatched_target_filters(
units: &[Unit],
filter: &CompileFilter,
shell: &mut Shell,
) -> CargoResult<()> {
if let CompileFilter::Only {
all_targets,
lib: _,
ref bins,
ref examples,
ref tests,
ref benches,
} = *filter
{
if units.is_empty() {
let mut filters = String::new();
let mut miss_count = 0;
let mut append = |t: &FilterRule, s| {
if let FilterRule::All = *t {
miss_count += 1;
filters.push_str(s);
}
};
if all_targets {
filters.push_str(" `all-targets`");
} else {
append(bins, " `bins`,");
append(tests, " `tests`,");
append(examples, " `examples`,");
append(benches, " `benches`,");
filters.pop();
}
return shell.warn(format!(
"Target {}{} specified, but no targets matched. This is a no-op",
if miss_count > 1 { "filters" } else { "filter" },
filters,
));
}
}
Ok(())
}
/// Warns if a target's required-features references a feature that doesn't exist.
///
/// This is a warning because historically this was not validated, and it

View file

@ -2,6 +2,35 @@
use cargo_test_support::project;
#[cargo_test]
fn warn_unmatched_target_filters() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[lib]
test = false
bench = false
"#,
)
.file("src/lib.rs", r#"fn main() {}"#)
.build();
p.cargo("check --tests --bins --examples --benches")
.with_stderr(
"\
[WARNING] Target filters `bins`, `tests`, `examples`, `benches` specified, \
but no targets matched. This is a no-op
[FINISHED][..]
",
)
.run();
}
#[cargo_test]
fn reserved_windows_target_name() {
let p = project()