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

Code coverage not working with MVC/Razor applications targeting ASP.NET 6 preview 3+ #1140

Closed
martincostello opened this issue Apr 10, 2021 · 5 comments
Labels
untriaged To be investigated

Comments

@martincostello
Copy link
Contributor

martincostello commented Apr 10, 2021

In .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 dotnet/sdk#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.

@martincostello
Copy link
Contributor Author

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:

<PropertyGroup>
  <_UseRazorSourceGenerator>false</_UseRazorSourceGenerator>
</PropertyGroup>

@martincostello
Copy link
Contributor Author

In preview 6 the workaround changes to:

<PropertyGroup>
  <UseRazorSourceGenerator>false</UseRazorSourceGenerator>
</PropertyGroup>

@martincostello
Copy link
Contributor Author

I also tried adding an exclude of [*]AspNetCoreGeneratedDocument* to ignore the types generated by the Razor Source Generator, but that didn't work either.

@martincostello
Copy link
Contributor Author

Also tried using EmitCompilerGeneratedFiles=true, but that doesn't work as the path looked for by coverlet doesn't match the one used by the compiler:

Expected:

C:\Coding\martincostello\api\src\API\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\_Views_Docs_Index_cshtml.cs

Actual:

C:\Coding\martincostello\api\src\API\obj\Release\net6.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator

The \obj\Release\net6.0\generated\ isn't accounted for.

@martincostello
Copy link
Contributor Author

This was fixed by dotnet/sdk#18943 which will be in .NET 6 RC1, but I think this has highlighted that there's some room for improvement in how coverlet handles what it perceives to be missing source files as this is the second time I'm aware of that projects using source generators have had to be changed to get coverlet to produce any coverage output for them, this issue and #1084.

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

No branches or pull requests

2 participants