How do you setup rust code coverage?

So I’m following Flox’s Rust Cookbook, and I was surprised to learn that it doesn’t support code coverage. Or, at least, not in the way I expected.

Apparently, for -C instrument-coverage to work, I’ll need to have built rustc with profiler = true in the compiler’s TOML file.

Flox provides Rust packages, but I’m not sure how, if possible, to get a rustc (and thus cargo) that can do code coverage.

I gave up and just went back to rustup.

In case it’s still any help:

Rust nightly includes support for code coverage, so you can follow the cookbook’s instructions for using a nightly compiler: Rust - Flox Docs

You’d probably want to create your own flake with a nightly compiler rather than using github:zmitchell/rust-toolchains#nightly, but using a nightly seems to work where using plain cargo fails:

flox [coverage] /tmp/coverage main > flox install cargo
✅ 'cargo' installed to environment 'coverage'
flox [coverage] /tmp/coverage main > RUSTFLAGS="-C instrument-coverage" cargo build
   Compiling coverage v0.1.0 (/private/tmp/coverage)
error[E0463]: can't find crate for `profiler_builtins`
  |
  = note: the compiler may have been built without the profiler runtime

For more information about this error, try `rustc --explain E0463`.
error: could not compile `coverage` (bin "coverage") due to 1 previous error

flox [coverage] /tmp/coverage main > flox remove cargo
🗑️  'cargo' uninstalled from environment 'coverage'

flox [coverage] /tmp/coverage main > flox install github:zmitchell/rust-toolchains#nightly
✅ 'nightly' installed to environment 'coverage'

flox [coverage] /tmp/coverage main > RUSTFLAGS="-C instrument-coverage" cargo build
   Compiling coverage v0.1.0 (/private/tmp/coverage)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.01s

flox [coverage] /tmp/coverage main > ./target/debug/coverage
Hello, world!

flox [coverage] /tmp/coverage main > ls | grep profraw
default_12879082849180275691_0_74579.profraw