Skip to content

TypeInitializationException when targeting .NET Framework - Could not load type 'System.Collections.Concurrent.ConcurrentBag`1' #2009

Description

@viktorgobbi

Describe the bug
Test execution of an NUnit project fails with 'Zero Tests ran' when running dotnet test --coverlet with the latest nightly packages (10.0.2-preview.30.geeb431030d) for .net Framework projects (net481) and an error is logged: TypeInitializationException with Could not load type 'System.Collections.Concurrent.ConcurrentBag1' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.` .

To Reproduce:

  1. Create a new .net framework project targeting net481 with the latest Coverlet.MTP and other required packages. My test project file:
    • CoverletIssueRepro.csproj:
      <Project Sdk="Microsoft.NET.Sdk">
          <PropertyGroup>
              <OutputType>Exe</OutputType>
              <TargetFramework>net481</TargetFramework>
              <RootNamespace>CoverletIssueRepro</RootNamespace>
          </PropertyGroup>
          <PropertyGroup>
              <OutputType>Exe</OutputType>
              <EnableNUnitRunner>true</EnableNUnitRunner>
              <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
          </PropertyGroup>
          <ItemGroup>
              <PackageReference Include="coverlet.MTP" Version="10.0.2-preview.30.geeb431030d" />
              <PackageReference Include="NUnit" Version="4.6.1" />
              <PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
              <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
          </ItemGroup>
      </Project>
  2. Create a file with one dummy unit test:
    • Repro.cs:
      using NUnit.Framework;
      
      namespace CoverletIssueRepro
      {
         [TestFixture]
         public class Repro
         {
             [Test]
             public void DemoTest()
             {
                 Assert.Pass();
             }
         }
      }
  3. Enable MTP for dotnet CLI in global.json (not sure if this is still required with NET 10 SDK)
    • global.json:
      {
        "test": {
          "runner": "Microsoft.Testing.Platform"
        }
      }
  4. Run dotnet test with coverlet dotnet test --coverlet

Expected behavior
dotnet test --coverlet should show "1 succeeded test" and a coverage file should be present in the TestResrults directory

Actual behavior
dotnet test --coverlet shows "Zero test ran" and "error: 1" as well as a logged error output with a TypeInitializationException with Could not load type 'System.Collections.Concurrent.ConcurrentBag1' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.`

Full command output:

-> % dotnet test --coverlet
Running tests from CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64)
CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64) Zero tests ran (3s 220ms)
Exit code: 7
  Standard output: [16:33:52.906] SynchronizedRunTestsAsync: ENTRY
  [16:33:52.907] SynchronizedRunTestsAsync: Creating executor
  Error output:
  Unhandled Exception: System.TypeInitializationException: The type initializer for 'NUnit.VisualStudio.TestAdapter.VsTestFilter' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCaseProperties' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Coverlet.Core.Instrumentation.Tracker.Microsoft.TestPlatform.CoreUtilities_288d6f6f-5c15-4bc8-8840-5fe054fdf339' threw an exception. ---> System.TypeLoadException: Could not load type 'System.Collections.Concurrent.ConcurrentBag`1' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
     at Coverlet.Core.Instrumentation.Tracker.Microsoft.TestPlatform.CoreUtilities_288d6f6f-5c15-4bc8-8840-5fe054fdf339.RegisterUnloadEvents()
     at Coverlet.Core.Instrumentation.Tracker.Microsoft.TestPlatform.CoreUtilities_288d6f6f-5c15-4bc8-8840-5fe054fdf339..cctor()
     --- End of inner exception stack trace ---
     at Coverlet.Core.Instrumentation.Tracker.Microsoft.TestPlatform.CoreUtilities_288d6f6f-5c15-4bc8-8840-5fe054fdf339.RecordHit(Int32 )
     at Microsoft.VisualStudio.TestPlatform.ObjectModel.ValidateArg.NotNullOrEmpty(String arg, String parameterName) in /_/src/Microsoft.TestPlatform.CoreUtilities/ValidateArg.cs:line 60
     at Microsoft.VisualStudio.TestPlatform.ObjectModel.TestProperty.Register(String id, String label, Type valueType, TestPropertyAttributes attributes, Type owner) in /_/src/Microsoft.TestPlatform.ObjectModel/TestProperty/TestProperty.cs:line 299
     at NUnit.VisualStudio.TestAdapter.CategoryList..cctor() in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\CategoryList.cs:line 35
     --- End of inner exception stack trace ---
     at NUnit.VisualStudio.TestAdapter.VsTestFilter..cctor() in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\VsTestFilter.cs:line 61
     --- End of inner exception stack trace ---
     at NUnit.VisualStudio.TestAdapter.VsTestFilter..ctor(IRunContext runContext)
     at NUnit.VisualStudio.TestAdapter.VsTestFilterFactory.CreateVsTestFilter(IAdapterSettings settings, IRunContext context) in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\VsTestFilter.cs:line 182
     at NUnit.VisualStudio.TestAdapter.NUnit3TestExecutor.InitializeForExecution(IRunContext runContext, IFrameworkHandle frameworkHandle) in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\NUnit3TestExecutor.cs:line 551
     at NUnit.VisualStudio.TestAdapter.NUnit3TestExecutor.RunTests(IEnumerable`1 sources, IRunContext runContext, IFrameworkHandle frameworkHandle) in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\NUnit3TestExecutor.cs:line 184
     at NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter.NUnitBridgedTestFramework.<SynchronizedRunTestsAsync>d__20.MoveNext() in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\TestingPlatformAdapter\NUnitBridgedTestFramework.cs:line 105
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter.NUnitBridgedTestFramework.<SynchronizedRunTestsAsync>d__20.MoveNext() in D:\a\nunit3-vs-adapter\nunit3-vs-adapter\src\NUnitTestAdapter\TestingPlatformAdapter\NUnitBridgedTestFramework.cs:line 113
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Extensions.VSTestBridge.SynchronizedSingleSessionVSTestBridgedTestFramework.<>c__DisplayClass22_0.<<ExecuteRequestAsync>b__0>d.MoveNext() in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/SynchronizedSingleSessionVSTestAndTestAnywhereAdapter.cs:line 166
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Extensions.VSTestBridge.SynchronizedSingleSessionVSTestBridgedTestFramework.<ExecuteRequestWithRequestCountGuardAsync>d__24.MoveNext() in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/SynchronizedSingleSessionVSTestAndTestAnywhereAdapter.cs:line 188
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Extensions.VSTestBridge.VSTestBridgedTestFrameworkBase.<ExecuteRequestAsync>d__24.MoveNext() in /_/src/Platform/Microsoft.Testing.Extensions.VSTestBridge/VSTestBridgedTestFrameworkBase.cs:line 102
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Requests.TestHostTestFrameworkInvoker.<ExecuteRequestAsync>d__16.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Requests/TestHostTestFrameworkInvoker.cs:line 112
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Requests.TestHostTestFrameworkInvoker.<ExecuteAsync>d__15.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Requests/TestHostTestFrameworkInvoker.cs:line 87
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Hosts.CommonHost.<ExecuteRequestAsync>d__15.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 212
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Hosts.ConsoleTestHost.<InternalRunAsync>d__10.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ConsoleTestHost.cs:line 92
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.Testing.Platform.Hosts.ConsoleTestHost.<InternalRunAsync>d__10.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ConsoleTestHost.cs:line 117
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Hosts.CommonHost.<RunTestAppAsync>d__13.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 175
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Hosts.CommonHost.<RunAsync>d__8.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 71
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.Testing.Platform.Hosts.CommonHost.<RunAsync>d__8.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 79
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.Testing.Platform.Hosts.CommonHost.<RunAsync>d__8.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs:line 103
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Hosts.TestHostControlledHost.<RunAsync>d__4.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostControlledHost.cs:line 25
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.Testing.Platform.Builder.TestApplication.<RunAsync>d__10.MoveNext() in /_/src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs:line 231
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at CoverletIssueRepro.MicrosoftTestingPlatformEntryPoint.<Main>d__0.MoveNext() in C:\Users\USERNAME\RiderProjects\CoverletIssueRepro\CoverletIssueRepro\obj\Debug\net481\MicrosoftTestingPlatformEntryPoint.cs:line 18
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at CoverletIssueRepro.MicrosoftTestingPlatformEntryPoint.<Main>(String[] args)

Test run summary: Zero tests ran
  error: 1

  total: 0
  failed: 0
  succeeded: 0
  skipped: 0
  duration: 5s 831ms
Test run completed with non-success exit code: 7 (see: https://aka.ms/testingplatform/exitcodes)

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

  • Which coverlet package (collector, console, msbuild, ...) and version was used?
    • Reproducible with:
      • 10.0.2-preview.30.geeb431030d
      • 10.0.2-preview.28.g4032f53415
      • 10.0.2-preview.21.gb734a326e7
      • 10.0.2-preview.17.g856637d9d2
      • 10.0.2-preview.16.g68e4db09d3
    • Not reproducible with 10.0.2-preview.15.g09fd57f9a9
  • Which version of .NET is the code running on?
    • I'd assume net481 but I'm not sure what this exactly refers to
  • What OS and version, and what distro if applicable?
    • Windows 11 25H2 (ver output Microsoft Windows [Version 10.0.26200.8893])
  • What is the architecture (x64, x86, ARM, ARM64)?
    • x64
  • Do you know whether it is specific to that configuration?
  • dotnet --info output:
    •   dotnet.exe --info
        .NET SDK:
         Version:           10.0.301
         Commit:            96856fd726
         Workload version:  10.0.300-manifests.8c7d7c03
         MSBuild version:   18.6.4+96856fd72
      
        Runtime Environment:
         OS Name:     Windows
         OS Version:  10.0.26200
         OS Platform: Windows
         RID:         win-x64
         Base Path:   C:\Program Files\dotnet\sdk\10.0.301\
        
        .NET workloads installed:
        There are no installed workloads to display.
        Configured to use workload sets when installing new manifests.
        No workload sets are installed. Run "dotnet workload restore" to install a workload set.
        
        Host:
          Version:      10.0.9
          Architecture: x64
          Commit:       901ca94124
        
        .NET SDKs installed:
          8.0.422 [C:\Program Files\dotnet\sdk]
          10.0.301 [C:\Program Files\dotnet\sdk]
        
        .NET runtimes installed:
          Microsoft.AspNetCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
          Microsoft.AspNetCore.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
          Microsoft.NETCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
          Microsoft.NETCore.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
          Microsoft.WindowsDesktop.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
          Microsoft.WindowsDesktop.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
        
        Other architectures found:
          x86   [C:\Program Files (x86)\dotnet]
            registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
        
        Environment variables:
          Not set
        
        global.json file:
          C:\Users\USERNAME\RiderProjects\CoverletIssueRepro\global.json
        
        Learn more:
          https://aka.ms/dotnet/info
        
        Download .NET:
          https://aka.ms/dotnet/download
      
    
    

Additional context
I'm in the process of switching our testrunner over to MTP and originally ran into the issue described in #1984 which affects the latest released version as of writing (10.0.1) but I saw that there was a fix available so I wanted to try the nightly build where I encountered this issue. Due to the specific build it is failing at and the the affected types I assume it is related to #1987 with 62021a9 which recently introduced the type that fails to load.
Without Coverlet enabled the tests run fine:

-> dotnet test
Running tests from CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64)
CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64) passed (947ms)

Test run summary: Passed!
  total: 1
  failed: 0
  succeeded: 1
  skipped: 0
  duration: 1s 278ms

I also noticed that the issue disappeared when I ran dotnet test --coverlet --coverlet-exclude "[Microsoft.*]*" but as soon as I add a class library project (again net481) and use that in the unit tests, I get the same error again:

dotnet test --coverlet  --coverlet-exclude "[Microsoft.*]*"
Running tests from CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64)
failed DemoTest (118ms)
  from CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64)
  System.TypeInitializationException : The type initializer for 'Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467' threw an exception.
    TypeName: Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467
    ----> System.TypeLoadException : Could not load type 'System.Collections.Concurrent.ConcurrentBag`1' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    TypeName: System.Collections.Concurrent.ConcurrentBag`1
    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467.RecordHit(Int32 )
    at LibraryUnderTest.ClassUnderTest.ReturnSomething() in LibraryUnderTest\ClassUnderTest.cs:5
    at CoverletIssueRepro.Repro.DemoTest() in CoverletIssueRepro\Repro.cs:12
    --TypeLoadException
    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467.RegisterUnloadEvents()
    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467..cctor()
    1)    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467.RecordHit(Int32 )
    at LibraryUnderTest.ClassUnderTest.ReturnSomething() in LibraryUnderTest\ClassUnderTest.cs:5
    at CoverletIssueRepro.Repro.DemoTest() in CoverletIssueRepro\Repro.cs:12
    --TypeLoadException
    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467.RegisterUnloadEvents()
    at Coverlet.Core.Instrumentation.Tracker.ClassLibrary1_b7058d04-60e1-4767-9e88-b09d2adec467..cctor()

CoverletIssueRepro\bin\Debug\net481\CoverletIssueRepro.exe (net48|x64) failed with 1 error(s) (1s 418ms)
Exit code: 2
  Standard output: [17:51:18.046] SynchronizedRunTestsAsync: ENTRY
  [17:51:18.046] SynchronizedRunTestsAsync: Creating executor

Test run summary: Failed!
  total: 1
  failed: 1
  succeeded: 0
  skipped: 0
  duration: 2s 506ms
Test run completed with non-success exit code: 2 (see: https://aka.ms/testingplatform/exitcodes)

Let me know if there's anything else I can provide. Happy to help however I can.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    driver-MTPIssue related to Microsoft Testing Platform driverwith reproIssue with repro

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions