Code coverage not working with MVC/Razor applications targeting ASP.NET 6 preview 3+
See original GitHub issueIn .NET 6 preview 3, the Razor compiler was changed to use a Roslyn source generator instead of compiling a separate .Views
assembly (announcement).
Similar to #1084, this causes coverlet to be unable to instrument the application’s assembly due to seemingly missing source files emitted into the assembly/PDB for the views:
[coverlet] Unable to instrument module: C:\Coding\martincostello\api\tests\API.Tests\bin\debug\net6.0\API.dll, pdb without local source files, [C:\Coding\martincostello\api\src\API\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\_Views_Docs_Index.cshtml.cs]
At first I thought this might have been caused by https://github.com/dotnet/sdk/pull/16777, but having updated to a nightly build of the preview 4 SDK containing the fix for that issue to verify that (6.0.100-preview.4.21210.1
), code coverage is still not reported.
I’m not sure if this is an SDK issue (missing .g.cs
suffix), a coverlet issue, or both, so I’m opening this issue here and ~will open a separate issue referencing this one~ have opened dotnet/sdk#16835 in the dotnet/sdk repo.
To replicate the issue and generate an MSBuild binary log file with the coverlet logging present, run the following commands:
git clone https://github.com/martincostello/api.git martincostello-api
cd martincostello-api
git checkout 39867b25cc6faccaa85a3091c2a8dfa58dad6975
./build.ps1 -SkipTests
dotnet test tests/API.Tests -c debug -verbosity:diagnostic -bl:msbuild.binlog -noconsolelogger
You can then open the msbuild.binlog
file in MSBuild Structured Log Viewer and search for [coverlet]
to find the log message.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
As of .NET 6 preview 5, you can workaround this issue by adding the following to the
.csproj
file for any project that contains Razor views:In preview 6 the workaround changes to: