Skip to content

Fix cargo kcov crashing/generating incorrect reports for proc-macros #3207

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/integration_tests/build/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def test_coverage(test_fc_session_root_path, test_session_tmp_path):
exclude_region = "'mod tests {'"
target = "{}-unknown-linux-musl".format(platform.machine())

# Temporarily modify PATH so that "rustc" resolved to our wrapper script
# This is required as cargo kcov parses cargo's verbose output, and looking for lines starting
# with "Running `rustc":
# https://github.com/kennytm/cargo-kcov/blob/master/src/target_finder.rs#L44
# However, if we use the RUSTC_WRAPPER environment variable to make cargo run out script, the
# output will contain the lines "Running /firecracker/tests/rustc", and thus cargo kcov will
# fail to resolve the paths of the binaries compiled.
os.environ["PATH"] = "/firecracker/tests" + os.pathsep + os.environ["PATH"]
cmd = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try using CARGO_TARGET_x86_64_unknown_linux_musl_RUSTFLAGS=-Clink-dead-code as an alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those flags only affect the code that is actually compiled to run on the specified target, and thus also excludes proc macros and build scripts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it should be CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS=-Clink-dead-code (uppercased), but if it's not going to apply by design then probably not worth trying again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try both! (if you use the lowercase option then cargo will tell you to use the uppercase one instead)

'CARGO_WRAPPER="kcov" RUSTFLAGS="{}" CARGO_TARGET_DIR={} '
"cargo kcov --all "
Expand Down
9 changes: 9 additions & 0 deletions tests/rustc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# We have to fully qualify the path to rustc here, as we modified $PATH in a way that this script is founds before
# the actual rust executable.
# Then we invoke normal rustc, but force the 'link-dead-code' codegen option. This option is required for gathering
# coverage reports on proc-macros during cross-compilation, as the 'RUSTFLAGS' environment variable is ignored by cargo
# for build-scripts, plugins and proc-macros in this case.
# See also https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags
/usr/local/rust/bin/rustc $@ -Clink-dead-code