ReportGenerator icon indicating copy to clipboard operation
ReportGenerator copied to clipboard

Different results when rerunning `CoverageReportParser` against same input files

Open afscrome opened this issue 6 months ago • 1 comments

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

afscrome avatar Aug 22 '24 20:08 afscrome