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

--codecov: change how regions are calculated #255

Merged
merged 2 commits into from
Apr 18, 2023

Conversation

andrewgazelka
Copy link
Contributor

@andrewgazelka andrewgazelka commented Apr 14, 2023

The definition of a region is now very conservative. A region is now defined by

/// The location of a region
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub(crate) struct RegionLocation {
    start_line: u64,
    end_line: u64,
    start_column: u64,
    end_column: u64,
}

If that location is hit even once, it is marked as covered.

Quickly looking at the results, it appears to work properly. However, this should probably be tested more before it is merged. https://app.codecov.io/gh/getcollective-ai/collective/tree/codecov-better-region?displayType=list

TODO

*covered = *covered || region.execution_count() > 0; // TODO: maybe this should be compared to func_count?

@andrewgazelka andrewgazelka changed the title change how regions are calculated codecov: change how regions are calculated Apr 14, 2023
@andrewgazelka andrewgazelka changed the title codecov: change how regions are calculated --codecov: change how regions are calculated Apr 14, 2023
@geeklint
Copy link

Out of curiosity, do you know why I might run into the issue if I'm not testing generic code? My report shows over 97% locally vs less than 28% on codecov (seemingly because of untested instantations) but I'm not sure why functions would get instantiated multiple times if they're not generic?

@andrewgazelka
Copy link
Contributor Author

andrewgazelka commented Apr 15, 2023

Out of curiosity, do you know why I might run into the issue if I'm not testing generic code? My report shows over 97% locally vs less than 28% on codecov (seemingly because of untested instantations) but I'm not sure why functions would get instantiated multiple times if they're not generic?

It's difficult for me to pinpoint the exact cause without a deeper understanding of the raw JSON format, which is later processed into the codecov format. However, I can still offer some insights that might be helpful.

Even though your code might not involve generics, the changes in this PR could still impact it. For instance, under the new code, a region is considered hit as long as it's executed once, whereas the old code required a region to be executed at least a specific number of times (defined by func_count) to be considered valid.

I'm not entirely sure if func_count represents the number of regions between lines or simply how many times a function was instantiated, but it's worth looking into. If you could provide more context or perhaps share the JSON format and relevant code, I'd be happy to help you dive deeper into this issue and explore potential solutions.

@andrewgazelka
Copy link
Contributor Author

In addition, does this PR fix the coverage?

@geeklint
Copy link

The repo is here, I hadn't had a chance to test this PR myself yet.

https://github.com/geeklint/typeid-set

@andrewgazelka
Copy link
Contributor Author

andrewgazelka commented Apr 15, 2023

The repo is here, I hadn't had a chance to test this PR myself yet.

geeklint/typeid-set

Hey there @geeklint,

Whenever you have some time, I'd be grateful if you could give it a go and share your experience with me. It's as simple as installing this custom cargo-llvm-cov in your GitHub actions, just like I did in this example:

https://github.com/getcollective-ai/collective/blob/main/.github/workflows/rust-test.yml#L61-L62

Feel free to reach out if it doesn't solve your problem, and I'll be more than happy to help! Fingers crossed it does the trick, though. 🙂

@geeklint
Copy link

Alright, I had a chance to check it out. It seems the data is at least consistent now, with the only difference being how the respective services count lines.

Screenshot from 2023-04-15 20-13-56

The region I am missing covers two "real" lines of code, but the braces on the surrounding lines get excluded as "partially" covered. I can't imagine there's a consistent way to adjust a region to only the meaningful bits though, so I would suggest this is working as can be expected.

I wonder if the documentation for cargo-llvm-cov should be adjusted, since reading the line "By using --codecov flag instead of --lcov flag, you can use region coverage on Codecov:" in the README gave me the impression codecov.io would let me report the actual region coverage, whereas it seems it's more like it uses the collected region coverage data to determine a final line coverage, unless I'm missing something?

@andrewgazelka
Copy link
Contributor Author

andrewgazelka commented Apr 16, 2023

@geeklint Unfortunately, CodeCov does not support region coverage and only offers branch coverage, while Rust LLVM coverage only works with region coverage. Branch and region coverage are quite similar, so it would be interesting to explore creating a mapping between the two in the future. As you mentioned, accurately highlighting the relevant part of a line would be more helpful than displaying the entire line in red, yellow, or green. @taiki-e, do you think creating such a mapping would be valuable?

Regarding the term "line coverage," using it may lead to confusion, as it typically does not imply partial coverage support. However, if you have examples that suggest otherwise, please feel free to share them.

@andrewgazelka
Copy link
Contributor Author

andrewgazelka commented Apr 16, 2023

ok, so I looked into this a little bit and a branch basically is the same as a region except for the FalseExecutionCount field

https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0/llvm/tools/llvm-cov/CoverageExporterJson.cpp#L92-L98

maybe we could just set ExecutionCount = FalseExecutionCount?

@andrewgazelka
Copy link
Contributor Author

andrewgazelka commented Apr 16, 2023

@andrewgazelka
Copy link
Contributor Author

I looked into this more, and about 30 minutes to try to get fake branches to work, but couldn't. I think we should just merge this for now.

@andrewgazelka
Copy link
Contributor Author

@taiki-e, what are your thoughts on merging this? Please let me know if you require further testing or changes.

@taiki-e
Copy link
Owner

taiki-e commented Apr 18, 2023

@geeklint

whereas it seems it's more like it uses the collected region coverage data to determine a final line coverage

AFAIK, this is how codecov handles lines that are partially covered in branch coverage, etc. (see also #20 (comment) and later 3 comments). Since LLVM's region coverage is a subset of branch coverage (see this LLVM patch for more), the --codecov flag maps region coverage like branch coverage.

To be honest, I don't think the way codecov shows branch coverage is great, but some people indeed prefer it (#86), and I don't expect codecov to change it in the near future.

If you want to get decent region coverage reports with CI, consider generating HTML reports and deploying them to github pages (e.g., by using actions/deploy-pages), github artifacts (e.g., by using actions/upload-artifact), netlify, etc. -- This approch is what my colleagues (and I) use.


@andrewgazelka

As you mentioned, accurately highlighting the relevant part of a line would be more helpful than displaying the entire line in red, yellow, or green. @taiki-e, do you think creating such a mapping would be valuable?

I believe HTML reports and text reports already do that.

Copy link
Owner

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @andrewgazelka!

src/json.rs Outdated Show resolved Hide resolved
@taiki-e taiki-e merged commit 08c6777 into taiki-e:main Apr 18, 2023
@taiki-e
Copy link
Owner

taiki-e commented Apr 18, 2023

Published in 0.5.16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants