taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
948 stars 57 forks source link

Effective CI Caching #293

Closed TroyKomodo closed 1 year ago

TroyKomodo commented 1 year ago

Hi, I was curious how I can effectively use github cache action to cache building the artifacts for unit testing.

Normally, I would just cache the target directory and it would work to restore and the builds would be much much faster. However if I do that with cargo-llvm-cov as the build script invoker the cache is always invalidated and a fresh build is started. This does not appear to happen locally, (with the --no-clean) flag enabled. Adding the --no-clean flag inside a ci runner caching only the /target directory does not seem to solve this issue and the next time the CI is run the cache is not used and its rebuilt from scratch.

Any ideas on why this might be the case?

cargo llvm-cov nextest --lcov --output-path lcov.info --ignore-filename-regex "(main\.rs|tests|.*\.nocov\.rs)" --workspace -r --no-fail-fast -E "not test(_v6)" --status-level all --profile ci --no-clean

We also remove all profraw data before the test, but that does not seem to do anything on the cache status. Same with the --no-clean flag

My full build takes roughly 20min.

taiki-e commented 1 year ago

Please provide the information about your CI config.

Also, information on whether the same problem occurs without nextest would probably be helpful.

TroyKomodo commented 1 year ago

Ok, i have no idea. I cannot reproduce it on a smaller repo https://github.com/ScuffleTV/cache-issue/actions/runs/5589681016

I'll try again on the larger monorepo and see if the issue still happens. Not sure sorry. I'll close the issue if i still cannot reproduce this.

aasseman commented 1 year ago

I'm having the same issue. Here's my config: https://github.com/semiotic-ai/timeline-aggregation-protocol/blob/fd76a839003b02cb3d002962092f69db806e7095/.github/workflows/tests.yml#L40-L67

taiki-e commented 1 year ago

You both seem to be using actions/cache. Try Swatinem/rust-cache. I can confirm that the cache is working in at least one of the projects with hundreds of dependencies using Swatinem/rust-cache.

taiki-e commented 1 year ago

My guess is that the artifacts generated by the build with cargo-llvm-cov are not cached correctly because the cache key is not specified properly, and old caches built before you started using cargo-llvm-cov or built with old rustc are still in use. Simply removing the old cache may work, but using the appropriate tool such as Swatinem/rust-cache may be more desirable.

In any case, I can confirm that the cache is working in a project with the proper CI config, so I believe this is a problem with your CI config, not with cargo-llvm-cov. I'm going to close this issue, but feel free to comment if you have additional questions.

aasseman commented 1 year ago

I now use taiki-e/install-action@cargo-llvm-cov to install cargo-llvm-cov and set the actions/cache paths to:

~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/

And it seems to have fixed it.