Visitar URL original
[BUG] Coverlet.Collector produces empty report when Mediator.SourceGenerator is referenced · Issue #1718 · coverlet-coverage/coverlet · GitHub
Skip to content

[BUG] Coverlet.Collector produces empty report when Mediator.SourceGenerator is referenced #1718

Closed

Description

@alex-zyl

Describe the bug
Please share a clear and concise description of the problem.

To Reproduce

  1. Create WebAPI and UnitTest projects from template.
  2. Add Mediator packages to WebAPI project
<ItemGroup>
  <PackageReference Include="Mediator.Abstractions" Version="3.0.0-preview.27" />
  <PackageReference Include="Mediator.SourceGenerator" Version="3.0.0-preview.27" >
  <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
  </PackageReference>
</ItemGroup>
  1. Run tests
dotnet test .\TestProject.sln --configuration Debug --collect:"XPlat Code Coverage" --diag:log.txt

Expected behavior
Coverage report includes types from analyzed project.

Actual behavior
Coverage report is empty.

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="0" branch-rate="0" version="1.9" timestamp="1734109700" lines-covered="0" lines-valid="0" branches-covered="0" branches-valid="0">
  <sources />
  <packages />
</coverage>

Configuration (please complete the following information):
Please provide more information on your .NET configuration:
* Which coverlet package and version was used?
6.0.0
* Which version of .NET is the code running on?
net8.0
* What OS and version, and what distro if applicable?
Win11
* What is the architecture (x64, x86, ARM, ARM64)?
x64
* Do you know whether it is specific to that configuration?
No

Additional context
log.datacollector contains the following exception

TpTrace Information: 0 : 35584, 1, 2024/12/13, 18:07:37.009, 629740483105, datacollector.dll, AssemblyResolver.OnResolve: Resolved assembly: Microsoft.DotNet.PlatformAbstractions, from path: C:\Users\admin\.nuget\packages\coverlet.collector\6.0.0\build\netstandard1.0\Microsoft.DotNet.PlatformAbstractions.dll
TpTrace Verbose: 0 : 35584, 1, 2024/12/13, 18:07:37.012, 629740517319, datacollector.dll, [coverlet]AspNetCoreSharedFrameworkResolver search paths:
TpTrace Verbose: 0 : 35584, 1, 2024/12/13, 18:07:37.012, 629740517703, datacollector.dll, [coverlet]C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All\9.0.0
TpTrace Verbose: 0 : 35584, 1, 2024/12/13, 18:07:37.012, 629740517868, datacollector.dll, [coverlet]C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\9.0.0
TpTrace Verbose: 0 : 35584, 1, 2024/12/13, 18:07:37.013, 629740524572, datacollector.dll, [coverlet]Loading D:\Projects\Sandbox\Testcoverage\Testsnet8\bin\Debug\net8.0\testsnet8.deps.json
TpTrace Warning: 0 : 35584, 1, 2024/12/13, 18:07:37.017, 629740570341, datacollector.dll, [coverlet]Unable to instrument module: D:\Projects\Sandbox\Testcoverage\Testsnet8\bin\Debug\net8.0\ProjNet8.dll
Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Try to add <PreserveCompilationContext>true</PreserveCompilationContext> to test projects </PropertyGroup> or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line
 ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   --- End of inner exception stack trace ---
   at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.TryWithCustomResolverOnDotNetCore(AssemblyNameReference name) in /_/src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 215
   at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.Resolve(AssemblyNameReference name) in /_/src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 127
   at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
   at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type)
   at Mono.Cecil.TypeReference.Resolve()
   at Mono.Cecil.Mixin.CheckedResolve(TypeReference self)
   at Mono.Cecil.MetadataBuilder.GetConstantType(TypeReference constant_type, Object constant)
   at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type)
   at Mono.Cecil.MetadataBuilder.AddField(FieldDefinition field)
   at Mono.Cecil.MetadataBuilder.AddFields(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddTypes()
   at Mono.Cecil.MetadataBuilder.BuildTypes()
   at Mono.Cecil.MetadataBuilder.BuildModule()
   at Mono.Cecil.MetadataBuilder.BuildMetadata()
   at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
   at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleDefinition.Write(Stream stream, WriterParameters parameters)
   at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 333
   at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 149
   at Coverlet.Core.Coverage.PrepareModules() in /_/src/coverlet.core/Coverage.cs:line 135

Including <PreserveCompilationContext>true</PreserveCompilationContext> and CopyLocalLockFileAssemblies do not make any effect.

Workaround

  1. Add <PreserveCompilationContext>true</PreserveCompilationContext>
  2. Copy dependencies via AfterBuild task
  <Target Name="CopyMediatorSourceGenDeps" AfterTargets="Build">
    <ItemGroup>
      <ResolvedFiles Include="$(OutputPath)/refs/Microsoft.Extensions.DependencyInjection.Abstractions.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(ResolvedFiles)" DestinationFolder="$(OutputPath)" />
  </Target>

Activity

  1. Bertk commented on Jan 10, 2025

    @Bertk
    Collaborator

    Thanks a lot for reporting this issue. The current coverlet.collector version 6.0.3 targets netstandard2.0 only. This issue will be solved in the next major version.

  2. added
    enhancementGeneral enhancement request
    and removed
    untriagedTo be investigated
    on Jan 10, 2025
  3. alex-zyl commented on Jan 10, 2025

    @alex-zyl
    Author

    Thanks for heads up @Bertk
    Can't NetstandardAwareAssemblyResolver just be updated to try to lookup an assembly in refs folder if not found in main directory?

  4. Bertk commented on Jan 11, 2025

    @Bertk
    Collaborator

    Please you please try to use a self contained test executable. This might resolve the dependency problem.

      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net8.0-windows</TargetFramework>
        <PreserveCompilationContext>true</PreserveCompilationContext>
        <SelfContained>true</SelfContained>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
      </PropertyGroup>
    
  5. killergege commented on Apr 9, 2026

    @killergege

    We have a very similar issue

    In our solution, we replaced MediatR with Mediator.SourceGenerator.
    Since this change, the project that references the Mediator.SourceGenerator is missing from the coverage for the project testing it.
    Other side projects are there, but the main one is just missing.
    There is no warning or error displayed during build or testing though. I tried to have detailed logs, but found nothing useful.

    We found that 2 elements impact the issue:

    • Mediator.SourceGenerator
    • The .NET version : the issue is there with .NET 10.0.201 but not with .NET 10.0.104

    We tried several combinaisons:

    • MediatR + 10.0.104 => Coverage output is OK
    • MediatR + 10.0.201 => Coverage output is OK
    • Mediator + 10.0.104 => Coverage output is OK
    • Mediator + 10.0.201 => Coverage output is KO

    The suggested solution above to copy the dependency Microsoft.Extensions.DependencyInjection.Abstractions manually to the output fixes the issue, but I'm not sure why:

        <ItemGroup>
            <Content Include="$(PkgMicrosoft_Extensions_DependencyInjection_Abstractions)\lib\net10.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll">
                <CopyToOutputDirectory>Always</CopyToOutputDirectory>
            </Content>
        </ItemGroup>
    

    And updating the reference:

    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" GeneratePathProperty="true" />
    

    We are using Coverlet 8.0.1 (but we also have the issue with 6.0.4).

  6. Bertk commented on Apr 9, 2026

    @Bertk
    Collaborator

    Thank you for sharing this information. When you have a moment, could you please try the coverlet.collector preview from the Coverlet nightly build (version 8.1.0-preview-0016-gdcc9d19f60) and let me know how it works for you?

    If the issue persists, it would be very helpful if you could share the diagnostic log files so we can take a closer look.

    Looking ahead, with .NET 10 it may also be worth considering a migration to the Microsoft Testing Platform along with the coverlet.MTP NuGet package.

  7. yusyd commented on Apr 9, 2026

    @yusyd
    Contributor

    @Bertk I have the same issue, and I tested the nightly collector coverlet.collector 8.1.0-preview-0016-gdcc9d19f60. It still reproduces for me on Windows x64 with .NET SDK 10.0.201.
    I also reproduced the same on Ubuntu in GitHub Actions CI: tests pass, Cobertura is generated, but the assembly is missing from the coverage output.

    I created a standalone repro here:
    https://github.com/yusyd/coverlet-mediator-repro
    With this repro, tests pass and Cobertura is generated, but Repro.Application is missing from the coverage output

    The datacollector log shows:

    [coverlet]Unable to instrument module: ...\Repro.Application.dll
    Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
    ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
    

    I also tried the workarounds, and none of them fixed it:

    • <PreserveCompilationContext>true</PreserveCompilationContext>
    • /p:CopyLocalLockFileAssemblies=true
  8. AStarchikov commented on Apr 9, 2026

    @AStarchikov

    I have the same issue
    Was able to reproduce it with @yusyd repo sample

  9. 51 remaining items

  10. kayarre commented on Jun 4, 2026

    @kayarre

    This appears to be an issue in 10.0.1

  11. Bertk commented on Jun 5, 2026

    @Bertk
    Collaborator

    This appears to be an issue in 10.0.1

    Please use the latest coverlet.MTP nuget preview package 10.0.2-preview.2.gdfb154275d

    coverlet.MTP is actively maintained and aligned with the Microsoft Testing Platform (MTP), which is the default test platform starting with .NET 10.

    see https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test

    Important

    Running MTP projects under VSTest mode is considered legacy in favor of the newer experience in .NET 10 SDK. The support of running under this mode will be removed in MTP version 2 if run with .NET 10 SDK. The support remains available for .NET 9 SDK and earlier for backward compatibility. For more information, see Migrate to MTP mode of dotnet test.

    Note

    test example MediatorApp.Tests create test results.

    Image
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

    SolvedThe issue is solved and can be closeddriver-collectorsIssue related to collectors driverenhancementGeneral enhancement request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions