mozilla / grcov

Rust tool to collect and aggregate code coverage data for multiple source files
Mozilla Public License 2.0
1.19k stars 149 forks source link

readme: example uses `-Zprofile` which has been removed from Rust nightly #1240

Open cakebaker opened 2 weeks ago

cakebaker commented 2 weeks ago

Example: How to generate .gcda files for a Rust project in the readme uses -Zprofile which has been removed recently from Rust nightly (see https://github.com/rust-lang/rust/pull/131829).

marco-c commented 2 weeks ago

Thanks, we should update the README to remove the mention to "-Zprofile".

josecelano commented 2 weeks ago

Could anyone tell me what the solution is?

It seems it does not work if I remove the -Z profile flag:

Original:

CARGO_INCREMENTAL: "0"
RUSTFLAGS: "-Z profile -C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort"
RUSTDOCFLAGS: "-Z profile -C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort"

I've tried without the -Z profile flag:

CARGO_INCREMENTAL: "0"
RUSTFLAGS: "-C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort"
RUSTDOCFLAGS: "-C codegen-units=1 -C opt-level=0 -C link-dead-code -C overflow-checks=off -Z panic_abort_tests -C panic=abort"
marco-c commented 2 weeks ago

@josecelano the solution is using source-based coverage, as described at https://github.com/mozilla/grcov?tab=readme-ov-file#example-how-to-generate-source-based-coverage-for-a-rust-project.

josecelano commented 2 weeks ago

@josecelano the solution is using source-based coverage, as described at https://github.com/mozilla/grcov?tab=readme-ov-file#example-how-to-generate-source-based-coverage-for-a-rust-project.

Thank you @marco-c; I'm trying to upload the generated report to codecov.io. I'm trying to figure out how to update my GitHub workflows. I'm just posting this here in case other people have the same problem. I haven't found the solution yet.

I was using the https://github.com/marketplace/actions/grcov-for-rust action to generate the coverage report and upload it to codecov with a workflow similar to this:

name: Coverage

on:
  push:
    branches:
      - develop

env:
  CARGO_TERM_COLOR: always

jobs:
  report:
    name: Report
    environment: coverage
    runs-on: ubuntu-latest
    env:
      CARGO_INCREMENTAL: "0"
      RUSTFLAGS: "-Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
      RUSTDOCFLAGS: "-Cpanic=abort"

    steps:
      - id: setup
        name: Setup Toolchain
        uses: dtolnay/rust-toolchain@nightly
        with:
          toolchain: nightly
          components: llvm-tools-preview

      - id: tools
        name: Install Tools
        uses: taiki-e/install-action@v2
        with:
          tool: grcov

      - id: coverage
        name: Generate Coverage Report
        uses: alekitto/grcov@v0.2

      - id: upload
        name: Upload Coverage Report
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ${{ steps.coverage.outputs.report }}
          verbose: true
          fail_ci_if_error: true

The "alekitto/grcov" hasn't been updated for a while and I don't know what type of artefacts generating. Snice the action seems to be unmantained I'm trying to generate the report info manually with a workflow like this:

name: Coverage V2

on:
  push:
    branches:
      - develop

env:
  CARGO_TERM_COLOR: always

jobs:
  report:
    name: Report
    environment: coverage
    runs-on: ubuntu-latest
    env:
      CARGO_INCREMENTAL: "0"
      RUSTFLAGS: "-Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
      RUSTDOCFLAGS: "-Cpanic=abort"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - id: setup
        name: Setup Toolchain
        uses: dtolnay/rust-toolchain@nightly
        with:
          toolchain: nightly
          components: llvm-tools-preview

      - id: cache
        name: Enable Workflow Cache
        uses: Swatinem/rust-cache@v2

      - id: tools
        name: Install Tools
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov

      - id: coverage
        name: Generate Coverage Report
        run: |
          mkdir .coverage
          cargo llvm-cov --lcov --output-path=./.coverage/lcov.info

      - id: upload
        name: Upload Coverage Report
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./.coverage/lcov.info
          verbose: true
          fail_ci_if_error: true

But I don't know why I needed grcov in the first version. lcov format seems to be supported by codecov.