danielpalme / ReportGenerator

ReportGenerator converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.
https://reportgenerator.io
Apache License 2.0
2.63k stars 283 forks source link

Different results when rerunning `CoverageReportParser` against same input files #690

Closed afscrome closed 1 month ago

afscrome commented 2 months ago

If I re-run CoverageReportParser.ParseFiles against the same input coverage files, I'm getting different total line counts.

string[] coverageFiles = { /*...*/ };

var parallelism = 1;
CoverageReportParser parser = new CoverageReportParser(parallelism, parallelism, new string[] { }, new DefaultFilter(new string[] { }),
    new DefaultFilter(new string[] { }),
    new DefaultFilter(new string[] { }));

ReadOnlyCollection<string> collection = new ReadOnlyCollection<string>(coverageFiles);
var results = parser.ParseFiles(collection);

var lines = results.Assemblies.Select(x => x.CoveredLines).Sum();
Console.WriteLine($"LINES: {lines}");
// The above is printing different results from run to run

I believe the problem is coming from some kind of concurrency race condition - after removing the parallelism from the following line, I start getting consistent results. https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/CoberturaParser.cs#L135

Digging further, I came across the following, and confirmed that my report files are ending up with multiple classes with the same name. When the earlier parallelism is enabled, these duplicates get added in different orders, meaning the FirstOrDefault can return different results if re-run against the same inputs. https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/Analysis/Assembly.cs#L180

At this point I'm getting a bit lost in the weeds, but I have two theories for the root cause.

The first is that the Equals method for ClassNameParserResult compares both Name and DisplayName, meaning we get all unique PAIRS of Name and DisplayName - giving us opportunities for later duplicates https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/CoberturaParser.cs#L122-L128

The other possible cause is a mismatch between the following bits of logic in filtering for elements for a given class: https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/CoberturaParser.cs#L122-L128 https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/CoberturaParser.cs#L149-L157 https://github.com/danielpalme/ReportGenerator/blob/4444f14f0b574d4deda7921e3ab6892c823474c7/src/ReportGenerator.Core/Parser/CoberturaParser.cs#L190-L198

danielpalme commented 2 months ago

Can you share the coverage files you are using?

You can send them privately via email: reportgenerator@palmmedia.de or share them here.

AlexanderBartoshZ commented 1 month ago

Same for me. Regenerating reports with history on the same cobertura file generates reports with differences. @danielpalme Do you need any additional info to fix this ?

danielpalme commented 1 month ago

@AlexanderBartoshZ A sample cobertura file and history files would be great. And some instructions which commands you are executing.

AlexanderBartoshZ commented 1 month ago

@danielpalme Thank you for quick reaction! You should have the coverage report in your inbox. Repro: Run this command 2 times: reportgenerator -reporttypes:html -reports:output.cobertura.o.xml -targetdir:htmlh -historydir:h

Open the html report and check the difference - you expect none, but you get some:
image Let me know if you need any other info

The coverage is collected using dotnet-coverage collect -f cobertura from muplitple processes. Some of the "changed" coverage lines are about the code that only runs within one process

danielpalme commented 1 month ago

Thank you! Will have a look as soon as possible!

danielpalme commented 1 month ago

@AlexanderBartoshZ I was able to fix the problem with the unexpected changes in history comparison.

One remaining problem is, that some classes appear twice in the report (e.g. Aspire.Dashboard.Otlp.Storage.CircularBuffer<T>).

I guess you are using Microsoft CodeCoverage, therefore nested classes appear differently in the Cobertura file (compared to tools like coverlet). Details can be found here: #663.

afscrome commented 1 month ago

Sorry for missing this @danielpalme . Not sure I'm allowed to share my files I'm afraid.

That said, I do believe I have a variant of the the same duplicate classes issue you highlighted above, although my issue is happening specifically withe Merge. I've tested locally and #697 fixes my inconsistency.

AlexanderBartoshZ commented 1 month ago

Thank you @danielpalme. As soon as there is a binary I can verify it with - Will gladly do that! Yes, as stated in the initial comment, dotnet-coverage (aka https://github.com/microsoft/codecoverage) is used

With regards to #663 : What would be the right argument to change the . to / :

  1. You cannot distinguish between nested classes and non-nested ones (?)

    If there are enough arguments IMO the pressure for a change of https://github.com/microsoft/codecoverage can be built up. IF those do not work correctly with VS it could be a heavy argument

danielpalme commented 1 month ago

@AlexanderBartoshZ

If there are enough arguments IMO the pressure for a change of https://github.com/microsoft/codecoverage can be built up. IF those do not work correctly with VS it could be a heavy argument

Microsoft does not want to change form . to '/'. See: https://github.com/microsoft/codecoverage/issues/124#issuecomment-2175667139

AlexanderBartoshZ commented 1 month ago

Yes. Have read that ... But IMO, If there is a good argument they will have to