coverlet-coverage / coverlet

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

[BUG] Type initializer errors after updating from 6.0.0 to 6.0.1 #1629

Closed joaorosado closed 3 months ago

joaorosado commented 4 months ago

Describe the bug After upgrading from 6.0.0 to 6.0.1 the tests fail with errors initializing Coverlet

To Reproduce Updated from 6.0.0 to 6.0.1. Project being tested targets netstandard2.0. Only external reference is Newtonsoft.Json 13.0.3 Test project targets 4.7.2 and has the following references:

    <PackageReference Include="coverlet.msbuild" Version="6.0.1" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
    <PackageReference Include="GitVersion.MsBuild" Version="5.12.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="NSubstitute" Version="5.1.0" />
    <PackageReference Include="Nunit" Version="3.14.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
    <PackageReference Include="WireMock.Net" Version="1.5.40" />

Expected behavior Tests to run without crashing

Actual behavior

   System.TypeInitializationException : The type initializer for 'Coverlet.Core.Instrumentation.Tracker.OutSystems.Auditing_cbebb53f-99cc-461e-8124-db81357fd8a1' threw an exception.
  ----> System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  Stack Trace:
     at Coverlet.Core.Instrumentation.Tracker.OutSystems.Auditing_cbebb53f-99cc-461e-8124-db81357fd8a1.RecordHit(Int32 )
   at Auditing.Tests.AuditConfigTests.AuditConfigRequestTimeoutTest() in D:\a\1\s\tests\UnitTests\Auditing.Tests\AuditConfigTests.cs:line 12
--FileNotFoundException
   at Coverlet.Core.Instrumentation.Tracker.OutSystems.Auditing_cbebb53f-99cc-461e-8124-db81357fd8a1..cctor()

Configuration (please complete the following information): Please provide more information on your .NET configuration:

Additional context Current command being used to execute the tests:

dotnet test D:\a\1\s\tests\UnitTests\Auditing.Tests --configuration Release --no-restore --no-build --logger trx --framework net472 /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:ExcludeByFile=*/GitVersionInformation. /p:CoverletOutput=D:\a\TestResults\coverage.net472.1.xml

Currently I'm not able to switch execution mode to the --collect version due to the test CI templates being shared by all our or organizational repositories. So cannot test with the --collect:"XPlat Code Coverage" option

valadas commented 4 months ago

I got the same issue, interestingly the build worked fine on ubuntu github runner but failed on local builds/tests (on windows)

santisq commented 4 months ago

I believe I have the same issue too:

image

https://github.com/santisq/PSCompression/actions/runs/8039618762/job/21956826632?pr=22#step:5:49

daveMueller commented 4 months ago

Thanks a lot for reporting this. I can reproduce it with simply dotnet test /p:CollectCoverage=true. Repro is attached here. Issue1629.zip

sungam3r commented 4 months ago

The same here https://github.com/destructurama/attributed/pull/109

sungam3r commented 4 months ago

https://github.com/ArieGato/serilog-sinks-rabbitmq/pull/163

daveMueller commented 4 months ago

I start working on this and see if I can figure something out.

daveMueller commented 4 months ago

So the problem only occurs for none-NetCore projects like in the bug description net472. As coverlet.core is now multitarget in version 6.0.1, msbuild is using the wrong assemblies.

image

I think the issue is here https://github.com/coverlet-coverage/coverlet/blob/master/src/coverlet.msbuild.tasks/coverlet.msbuild.props#L22-L23. We can't use the MSBuildRuntimeType for this condition as e.g. on my local this still is Core.

image

I see two options here. We could move back to build coverlet.core only targeting netstandard2.0. I already tried that out and it is working. Or try to work with some other condition.

cc: @MarcoRossignoli @Bertk

MarcoRossignoli commented 4 months ago

I see two options here. We could move back to build coverlet.core only targeting netstandard2.0. I already tried that out and it is working. Or try to work with some other condition.

I would go for now netstandard2.0 and we will work if needed to multi-tfm but with completely different packaging...like be fair and do not cheat nuget.

daveMueller commented 4 months ago

I think I would have found a condition that works but keeping it netstandard2.0 for now is fine for me. I'll create a PR.

tmp64 commented 3 months ago

I'm getting a similar error with net7.0-windows. It attempts to load System.Collections.Immutable, Version=8.0.0.0. There are no errors with coverlet.collector 6.0.0

Test log ``` > dotnet test --collect "XPlat Code Coverage" Microsoft (R) Test Execution Command Line Tool Version 17.9.0 (x64) Copyright (c) Microsoft Corporation. All rights reserved. Starting test execution, please wait... A total of 1 test files matched the specified pattern. [xUnit.net 00:00:00.41] REDACTED [FAIL] [xUnit.net 00:00:00.42] REDACTED [FAIL] Failed REDACTED [190 ms] Error Message: REDACTED The type initializer for 'REDACTED' threw an exception. ---- System.TypeInitializationException : The type initializer for 'REDACTED' threw an exception. -------- System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Could not load file or assembly 'System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621) Could not load file or assembly 'System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621) Stack Trace: at REDACTED.<>c__DisplayClass14_0.g__REDACTED|1(<>c__DisplayClass14_1&) in REDACTED at REDACTED ----- Inner Stack Trace ----- at REDACTED ----- Inner Stack Trace ----- at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.Assembly.GetTypes() at REDACTED Failed REDACTED [< 1 ms] ```
Runtimes ``` Microsoft.AspNetCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] ```
SDKs ``` 7.0.304 [C:\Program Files\dotnet\sdk] 7.0.406 [C:\Program Files\dotnet\sdk] 8.0.201 [C:\Program Files\dotnet\sdk] ```
daveMueller commented 3 months ago

Hi guys, we finally merged everything we broke with the last release. We would really appreciate if someone could give it a try. Here is how to consume the nightly: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

sungam3r commented 3 months ago

Fixed in https://github.com/destructurama/attributed/pull/109

daveMueller commented 3 months ago

We now have a new official release 6.0.2 that can be consumed from nuget.org.

sungam3r commented 3 months ago

Thank you.