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

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

Open
alex-zyl opened this issue Dec 13, 2024 · 0 comments
Labels
untriaged To be investigated

Comments

@alex-zyl
Copy link

alex-zyl commented Dec 13, 2024

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>
@github-actions github-actions bot added the untriaged To be investigated label Dec 13, 2024
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

1 participant