question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`CompilerGeneratedAttribute` excludes new class

See original GitHub issue

Hello!

We have a strange issue in our project.

We added a new class that basically looks like this:

using MediatR;
using System.Transactions;
using ORG.PROJECT.OTHERPACKAGE;

namespace ORG.PROJECT.PACKAGE.Behaviours;

public class TransactionBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>, ITransactionCommand
{
    public async Task<TResponse> Handle(
        TRequest request,
        RequestHandlerDelegate<TResponse> next,
        CancellationToken cancellationToken)
    {
        using (var scope = new TransactionScope(
            TransactionScopeOption.Required,
            new TransactionOptions { IsolationLevel = request.TransactionIsolationLevel },
            TransactionScopeAsyncFlowOption.Enabled
            ))
        {
            var result = await next();
            scope.Complete();
            return result;
        }
    }
}

This new class (&method) does not show up in our code coverage report: image I also already ruled out any display or converting issues (report generator), it is definitely already missing in the opencover file, we get from our build.

However, all other classes from that package are found. The strange thing is, that our other Behaviours classes are really similar. Like same generics, same method definition and so on. The only difference would be our ITransactionCommand interface, but also already tried removing that.

After some digging I found out, that this class gets excluded with the CompilerGeneratedAttribute

Our test execution looks like this:

- task: DotNetCoreCLI@2
  displayName: Test application
  inputs:
    command: test
    projects: '**/*Tests/*.csproj'
    arguments: >-
      --configuration $(buildConfiguration)
      --collect:"XPlat Code Coverage"
      -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
      -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByFile=**/Migrations/*.cs
      -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByAttribute=Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute

As soon as I remove the CompilerGeneratedAttribute from the ExcludedByAttribute list, the new class gets detected.

Our environment:

  • .NET 7.0.2
  • Microsoft.NET.Test.Sdk 17.4.1
  • xunit 2.4.2
  • xunit.runner.visualstudio 2.4.5
  • coverlet.collector 3.2.0
  • Ubuntu 22.04

This looks like a bug to me and to be honest, I have zero clue, why it is behaving in that way. Some differences to our other behaviours:

  • Dependency to System.Transactions
  • using statement for IDisposable
  • Only this one Handle method, instead of multiple methods (that are called by the Handle method)

Apparently this was already a problem in the past: https://github.com/coverlet-coverage/coverlet/issues/794

If needed, I can try to create a minimal reproducible repo.

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
MarcoRossignolicommented, Feb 6, 2023

We cannot do a lot for it as @daveMueller explained, I also suggest to remove the attribute and handle the “noise” with other filtering.

1reaction
daveMuellercommented, Feb 5, 2023

Yes I would advise to remove it. Only thing that I have in mind are auto-implemented properties that after removing could be reported as uncovered (instead of beeing skipped). But therefore there is the SkipAutoProps option (https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md#skip-auto-implemented-properties).

@petli @MarcoRossignoli @tonerdo Any other thoughts on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get with Reflection fields that are not generated by the ...
So I modified the method to exclude the fields with CompilerGenerated attribute and his name match with some property.
Read more >
[Question] [Possible Issue] exclude-by-attributes does not ...
When using --exclude-by-attribute 'Obsolete,GeneratedCode,CompilerGenerated' It will not exclude files like Resource Designer files (The ...
Read more >
CompilerGeneratedAttribute Class (System.Runtime. ...
Distinguishes a compiler-generated element from a user-generated element. This class ... Initializes a new instance of the CompilerGeneratedAttribute class.
Read more >
Exclude compiler generated code from coverage?
Hi, I love the support for the ExcludeFromCodeCoverage attribute. Is there a way to indicate other attributes that should exclude class and ...
Read more >
Customizing Code Coverage Analysis - Visual Studio
Learn how to use the ExcludeFromCodeCoverageAttribute attribute to exclude test code from coverage results. You can include assemblies ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found