pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
742 stars 111 forks source link

Dotcover code coverage fails with AspectInjector 2.6.1 but not with AspectInjector 2.6.0 #170

Closed El-Gor-do closed 2 years ago

El-Gor-do commented 2 years ago

Create a solution with 2 projects - MyLibrary and MyLibrary.Tests - and create the following files:

MyLibrary\MyLibrary.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AspectInjector" Version="2.6.1" />
  </ItemGroup>
</Project>

MyLibrary\InjectBeforeAttribute.cs

using System;
using System.Reflection;

using AspectInjector.Broker;

namespace MyLibrary
{
    [Aspect(Scope.PerInstance)]
    [AttributeUsage(AttributeTargets.Method)]
    [Injection(typeof(InjectBeforeAttribute))]
    public class InjectBeforeAttribute : Attribute
    {
        [Advice(Kind.Before, Targets = Target.Public | Target.Method)]
        public void OnEntry(
            [Argument(Source.Instance)] object instance,
            [Argument(Source.Metadata)] MethodBase method,
            [Argument(Source.Arguments)] object[] args)
        {
            Console.WriteLine($"Injected before method {method.Name}");
        }
    }
}

MyLibrary\Helper.cs

namespace MyLibrary
{
    public class Helper
    {
        [InjectBefore]
        public string Run()
        {
            return "Hello world";
        }
    }
}

MyLibrary.Tests\MyLibrary.Tests.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="JetBrains.dotCover.CommandLineTools" Version="2021.3.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\MyLibrary\MyLibrary.csproj" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="JetBrains.dotCover.DotNetCliTool" Version="2021.3.2" />
  </ItemGroup>
</Project>

MyLibrary.Tests\HelperTests.cs

using Xunit;

namespace MyLibrary.Tests
{
    public class HelperTests
    {
        [Fact]
        public void RunTest()
        {
            Helper helper = new Helper();
            string s = helper.Run();
        }
    }
}

Open a command prompt in the MyLibrary.Tests directory and run Dotcover using this command line:

dotnet.exe dotcover test --dotCoverOutput="C:\temp\MyLibrary.Tests.dcvr" --dcLogFile="C:\temp\MyLibrary.Tests.dcvr.log" --dcFilters=+:module=MyLibrary

Dotcover fails with the following output:

[JetBrains dotCover] Coverage session finished with errors: Inconsistent PDB detected, too large offset in statement table
[location] = C:\BuildAgent\work\c87178d597890b6b\Profiler\Kernel\Windows\Native\Solution\core\src\IL\Var\ILMethodVar.cpp(53)
[function] = unsigned short __cdecl ILMethodVar::BeginInject(struct write_stream_iface *,unsigned int)
[token] = 06000001, MethodDef
[method name] = Run
[module name] = D:\dev\AspectInjectorDotcoverClash\MyLibrary.Tests\bin\Debug\net6.0\MyLibrary.dll
[type name] = MyLibrary.Helper.

If I change MyLibrary.csproj to use AspectInjector 2.6.0 then the Dotcover command line runs successfully:

[JetBrains dotCover] Coverage session finished [29/12/2021 14:27:50]
[JetBrains dotCover] Coverage results post-processing started [29/12/2021 14:27:50]
[JetBrains dotCover] Merging snapshots [29/12/2021 14:27:50]
[JetBrains dotCover] Snapshots merging finished [29/12/2021 14:27:50]
[JetBrains dotCover] Coverage results post-processing finished [29/12/2021 14:27:50]
pamidur commented 2 years ago

Hi @El-Gor-do , thanks for reporting, I 'll take a look shortly