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

Add selective coverage instrumentation for crates #387

Open
0xdeafbeef opened this issue Sep 30, 2024 · 5 comments
Open

Add selective coverage instrumentation for crates #387

0xdeafbeef opened this issue Sep 30, 2024 · 5 comments
Labels
C-question Category: A question

Comments

@0xdeafbeef
Copy link

This tool already has --exclude-from-report, does it make sense to add black/white lists for toggling instrumentation on and off?
Like --enable-coverage-for crate1 --enable-coverage-for crate2 or --disable coverage-for tokio

@taiki-e
Copy link
Owner

taiki-e commented Sep 30, 2024

I guess you can use --dep-coverage (#353).

    --dep-coverage <NAME>
            Show coverage of the specified dependency instead of the crates in the current workspace. (unstable)

@taiki-e taiki-e added the C-question Category: A question label Sep 30, 2024
@0xdeafbeef
Copy link
Author

I guess you can use --dep-coverage (#353).

    --dep-coverage <NAME>
            Show coverage of the specified dependency instead of the crates in the current workspace. (unstable)

Seems not. I want to disable coverage instrumentation for some heavy math libs and tokio, otherwise tests fail because some components time out. It can be done here, if I read code correctly.

fn set_env(cx: &Context, env: &mut dyn EnvTarget, IsNextest(is_nextest): IsNextest) -> Result<()> {
I can send a pr if it makes sense

@taiki-e
Copy link
Owner

taiki-e commented Sep 30, 2024

My understanding is that excluding the application of -C instrument-coverage in a particular crate, or applying it only to a particular crate, is not yet supported (rust-lang/rfcs#3310), but if there is another way that works, I would like to accept it.

@0xdeafbeef
Copy link
Author

I've solved it with this RUSTC_WRAPPER

#!/usr/bin/env python3
import os
import sys


blacklist = ["25519", "tokio"]


def main():
    # The first argument is the path to rustc, followed by its arguments
    args = sys.argv[1:]

    # coverage is necessary only for our project
    crate_name = get_crate_name(args)
    if any(crate in crate_name for crate in blacklist):
        try:
            instrument_coverage_index = args.index("instrument-coverage")
            del args[instrument_coverage_index]
            del args[instrument_coverage_index - 1]
        except ValueError:
            pass

    # Execute rustc with the potentially modified arguments
    os.execvp(args[0], args)


def get_crate_name(args):
    for i, arg in enumerate(args):
        if arg == "--crate-name":
            return args[i + 1]
    return ""

if __name__ == "__main__":
    main()

cargo-llvm-cov can act as a wrapper, but I'm not sure if it's a good idea. Maybe this script can be put to README

@taiki-e
Copy link
Owner

taiki-e commented Oct 3, 2024

Ah, RUSTC_WRAPPER is a nice way to go.

Making the cargo-llvm-cov binary act as a RUSTC_WRAPPER only when a specific environment variable is set, making cargo llvm-cov to set RUSTC_WRAPPER=current_exe, and passing information about dependencies from the cargo llvm-cov through the environment variable, etc., would probably be possible to avoid applying instrument-coverage to all dependencies (i.e., emulate rust-lang/rfcs#3310).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-question Category: A question
Projects
None yet
Development

No branches or pull requests

2 participants