Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check --tests always succeeds if you have test = false in Cargo.toml #9536

Closed
davidpdrsn opened this issue Jun 3, 2021 · 5 comments · Fixed by #9549
Closed

check --tests always succeeds if you have test = false in Cargo.toml #9536

davidpdrsn opened this issue Jun 3, 2021 · 5 comments · Fixed by #9549
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug E-easy Experience: Easy

Comments

@davidpdrsn
Copy link

Problem
If a crate has

[lib]
test = false

in its Cargo.toml and you run cargo check --tests the crate always compiles regardless of what errors might actually be there.

I would expect --tests to still fail to compile or at least emit a clear warning.

Steps

  1. Made a little reproduction repo: git clone https://github.com/davidpdrsn/cargo-bug
  2. cd cargo-bug
  3. cargo check --tests compiles
  4. cargo check does not compile

Notes

Output of cargo version: cargo 1.52.0 (69767412a 2021-04-21)

@ehuss
Copy link
Contributor

ehuss commented Jun 3, 2021

When passing a target selection flag, that tells cargo to check the specified targets instead of the default set of targets. With --tests, it only selects tested targets. Since you have specified the lib not to be tested, there is nothing for cargo to check. There is more information about target selection here.

I think, perhaps, cargo could print a warning if using --tests, --bins, --examples, or --benches, and no matching targets were found.

@ehuss ehuss added A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-diagnostics Area: Error and warning messages generated by Cargo itself. E-easy Experience: Easy labels Jun 3, 2021
@Bryysen
Copy link
Contributor

Bryysen commented Jun 5, 2021

If we do cargo [command] --[target] target, where 'target' doesn't exist cargo will error accordingly. Would it also make sense if we error when doing cargo [command] --[targets] if there are zero matching targets? Also I would like claim this one if that's okay, and I'll work towards a solution once a consensus is reached.

@ehuss
Copy link
Contributor

ehuss commented Jun 6, 2021

I think for now it should just be a warning. It might otherwise break legitimate use cases. An example is the rust build system, which here blindly passes a bunch of flags to different packages which may or may not contain all those targets. We probably don't want a bunch of noisy warnings in that case.

I also think this warning should only appear if no targets were found in total. That is, cargo test --lib --benches should be fine if there is a lib but no benches. The set of root targets is determined here, and I think (maybe) it just needs to check if units.is_empty().

It is totally OK to claim, and let me know if you need any help. There's a guide here for writing and running tests.

@Bryysen
Copy link
Contributor

Bryysen commented Jun 6, 2021

Yes I agree, we should just emit an error here (seems like a silly question in hindsight). I was actually wondering if we wanted to emit a warning for every single missed target, or only if no targets were found in total. I agree it's better to go with the second option like you say as this would cut down on warning noise.

I'm a little unsure on the formatting on the warning message. Here's what I have so far
screen
Let me know if the formatting should be done differently, or if there is more useful information we can supply to the user. I was thinking maybe to create a special-case notification if we have [lib] test = false and --tests was passed without matching on anything, or would that be too much effort (diving into manifests and whatnot may just overcomplicate things)?

@ehuss
Copy link
Contributor

ehuss commented Jun 6, 2021

The formatting looks good, feel free to post a PR and we can go over it in more detail. I wouldn't bother with special-casing if something like test=false is set, I think the wording you have above seems petty clear.

bors added a commit that referenced this issue Jun 9, 2021
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](https://github.com/rust-lang/cargo/blob/ed0c8c6d66e36fafbce4f78907a110838797ae39/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
@bors bors closed this as completed in da1c2f3 Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants