coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.94k stars 385 forks source link

Incorrect coverage - only one module detected #1322

Closed klemmchr closed 2 years ago

klemmchr commented 2 years ago

When using the MSBuild collector I get an incorrect coverage result. Coverlet only detects one module while the test project references multiple projects that are being tested.

Here you can find a sample run that shows this issue. The project references can be found here.

daveMueller commented 2 years ago

Hi @klemmchr and thanks for reporting. I think this is a dup of #1262. The log shows

[coverlet] Unable to instrument module: D:\Repos\MvvmBlazor\src\MvvmBlazor.Tests\bin\Debug\net6.0\MvvmBlazor.Core.dll, pdb without local source files, [D:\Repos\MvvmBlazor\src\MvvmBlazor.Core\MvvmBlazor.CodeGenerators\MvvmBlazor.CodeGenerators.Components.MvvmComponentGenerator\MvvmComponentBase.Generated.cs]

Coverlet skip by default all dlls that don't have a corresponding local source files. For source generators coverlet can't find the source file and thus skips the dll. However, by convention coverlet skips source files with the suffix .g.cs.

So in your MvvmComponentGenerator just change the two lines

context.AddSource(componentClass.Identifier + ".Generated.cs", componentSourceText);
...
context.AddSource(componentClass.Identifier + "T.Generated.cs", genericComponentSourceText);

to

context.AddSource(componentClass.Identifier + ".g.cs", componentSourceText);
...
context.AddSource(componentClass.Identifier + "T.g.cs", genericComponentSourceText);

and it should work.

grafik

klemmchr commented 2 years ago

Thanks for the quick response, I did not expect that this is an issue related to source generators. I changed the file name and while the test itself now runs properly I get a source link error. This probably isn't related to this issue here but maybe you could give me a hint on what the resolution could be. Surely I could also create a new issue if this is desired.

These are the changes I made and this is the failing test run. The stack trace is

The given key '' was not present in the dictionary. [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key) [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]
   at Coverlet.Core.Coverage.GetSourceLinkUrl(Dictionary`2 sourceLinkDocuments, String document) in /_/src/coverlet.core/Coverage.cs:line 516 [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]
   at Coverlet.Core.Coverage.CalculateCoverage() in /_/src/coverlet.core/Coverage.cs:line 379 [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]
   at Coverlet.Core.Coverage.GetCoverageResult() in /_/src/coverlet.core/Coverage.cs:line 161 [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]
   at Coverlet.MSbuild.Tasks.CoverageResultTask.Execute() in /_/src/coverlet.msbuild.tasks/CoverageResultTask.cs:line 83 [/home/runner/work/MvvmBlazor/MvvmBlazor/src/MvvmBlazor.Tests/MvvmBlazor.Tests.csproj]

I'm building the project with -p:ContinuousIntegrationBuild=true and also set the Deterministic property in the project itself when running in the CI. I validated that those flags are set properly on the latest working build of the feature branch I'm working on. image

The repository contains three projects of which two are not really supported by Source Link or don't need them. MvvmBlazor.SourceGenerators has no need for them and MvvmBlazor itself is just a glue package that has a dependency on the source generators and MvvmBlazor.Core. Maybe this is the issue but I'm not sure about that.

daveMueller commented 2 years ago

OK this only happens with the UseSourceLink switch set to true. Maybe you can run your tests without this parameter and meanwhile I try to find some time to debug this. Could really be a new bug.