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

When running cargo test, cargo also re-builds all examples #6675

Closed
fschutt opened this issue Feb 16, 2019 · 1 comment · Fixed by #6683
Closed

When running cargo test, cargo also re-builds all examples #6675

fschutt opened this issue Feb 16, 2019 · 1 comment · Fixed by #6683
Assignees
Labels
C-bug Category: bug

Comments

@fschutt
Copy link
Contributor

fschutt commented Feb 16, 2019

Problem

When running cargo test, cargo re-builds all examples, even though they are never run. This makes re-running tests very slow, because if you only want to re-run one test, you have to wait for all examples to build - and especially since cargo for some reason compiles the code and the examples twice, the compilation times for re-running just a single test are extreme. Right now, rebuilding just one single test takes 1m 50s on my machine, this isn't good.

Steps

I am not sure if you can observe this with an empty example. I noticed it because of the progress bar at the bottom - every time I change a test and re-run cargo test, it shows this:

Building [===================================================>   ] 
205/215: azul, calculator(example), dragger(example), list(example)
[...]

running 1 test
test style::test_case_issue_93 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 11 filtered out

running 1 test
test css::test_case_issue_93 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 78 filtered out

     Running target/debug/deps/azul_native_style-bd6572d92ecb73f1

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

[...]

real	0m40,153s
user	1m44,308s
sys	0m17,192s

1 minute 44 seconds to re-run a single test - and I just added a space to the test, didn't even refactor anything. The repository I was working on is azul. If you want to reproduce it, please clone the repository, change a single test (so it recompiles) and watch the progress bar. On a hello world repository it's probably not noticable, but technically the steps are:

  1. cargo new --bin my_example
  2. Create a 50 empty examples and 50 doc-tests
  3. Watch the bar at the bottom fill with (example)

This happens even when I specify the test name case. I.e. I can specify "test_issue_93" to run, but it will still compile the examples, for no reason.

Possible Solution(s)
Don't compile examples when the user provides a test name case or passes --doc-tests (to only run doc tests) and provide a flag like --no-examples to stop the examples from being compiled. Also, cache the examples and make the build faster, but that's probably more advanced. I mean, why would I need to re-build all examples if all I want to do is to re-build a single test case?

Notes

Output of cargo version: cargo 1.34.0-nightly (865cb7010 2019-02-10)

Additional information

This happens on all versions of cargo from 1.30 to 1.34.

rustc 1.34.0-nightly (a9410cd1a 2019-02-15)
binary: rustc
commit-hash: a9410cd1af7c1194fbda1457c74b8ab25547e1e6
commit-date: 2019-02-15
host: x86_64-unknown-linux-gnu
release: 1.34.0-nightly
LLVM version: 8.0
@fschutt fschutt added the C-bug Category: bug label Feb 16, 2019
@dwijnand
Copy link
Member

The workaround to just run unit tests is invoking cargo test --tests.

@dwijnand dwijnand self-assigned this Feb 17, 2019
bors added a commit that referenced this issue Feb 25, 2019
…ested-examples-when-filtered, r=ehuss

Cargo test quicker by not building untested examples when filtered

Alternative to #6677
Fixes #6675
r? @ehuss
link2xt added a commit to deltachat/deltachat-core-rust that referenced this issue Jul 10, 2023
When `cargo test` is executed,
all examples are built by default
to ensure that they can be compiled.

This is a documented and expected behaviour,
even though it was previously reported as a bug:
<rust-lang/cargo#6675>

In particular, `examples/simple.rs` is built into
a 67M binary `target/debug/examples/simple`.
This is unnecessary to do so every time
you change a line in the `deltachat` crate
and want to rerun the tests.

Workaround is to run `cargo test --tests`,
but it is easy to forget and is not discoverable
unless you read the "Target Selection" section of `cargo help test`.

We have a maintained example at https://github.com/deltachat-bot/echo,
so there is no need for an example in the core repository.
link2xt added a commit to deltachat/deltachat-core-rust that referenced this issue Jul 10, 2023
When `cargo test` is executed,
all examples are built by default
to ensure that they can be compiled.

This is a documented and expected behaviour,
even though it was previously reported as a bug:
<rust-lang/cargo#6675>

In particular, `examples/simple.rs` is built into
a 67M binary `target/debug/examples/simple`.
This is unnecessary to do so every time
you change a line in the `deltachat` crate
and want to rerun the tests.

Workaround is to run `cargo test --tests`,
but it is easy to forget and is not discoverable
unless you read the "Target Selection" section of `cargo help test`.

We have a maintained example at https://github.com/deltachat-bot/echo,
so there is no need for an example in the core repository.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants