Skip to content

Logging source generator fails to compile if class has [DynamicallyAccessedMembers] #97498

Closed

Description

@martincostello

Description

If the logging source generator is used in a .NET 8 application that is intended to be native AoT compiled and is used on a class with a generic type that is annotated with a [DynamicallyAccessedMembers] attribute, the project fails to compile.

Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(4,31): error CS0246: The type or namespace name 'DynamicallyAccessedMembersAttribute' could not be found (are you missing a using directive or an assembly reference?)
Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(4,31): error CS0246: The type or namespace name 'DynamicallyAccessedMembers' could not be found (are you missing a using directive or an assembly reference?)
Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(4,58): error CS0103: The name 'DynamicallyAccessedMemberTypes' does not exist in the current context

Without the attribute, dotnet publish produces a warning:

Trim analysis warning IL2075: Describer`1.Describe(!0): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Reproduction Steps

Clone this repository repository and run dotnet build from the root directory.

Expected behavior

  • The project compiles.
  • dotnet publish succeeds with no warnings.

Actual behavior

The project fails to compile with CS0246 and CS0103 errors.

Regression?

Unknown - I've only tried to do this code pattern once, and this was the result.

Known Workarounds

Suppress the generated IL2075 in code in a simple program tested to otherwise work.

This is not likely to be appropriate for a larger application that cannot be otherwise verified to be safe.

Configuration

❯ dotnet --info
.NET SDK:
 Version:           8.0.101
 Commit:            6eceda187b
 Workload version:  8.0.100-manifests.66f5ce51

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.101\

.NET workloads installed:
 Workload version: 8.0.100-manifests.66f5ce51
There are no installed workloads to display.

Host:
  Version:      8.0.1
  Architecture: x64
  Commit:       bf5e279d92

.NET SDKs installed:
  6.0.321 [C:\Program Files\dotnet\sdk]
  6.0.418 [C:\Program Files\dotnet\sdk]
  7.0.312 [C:\Program Files\dotnet\sdk]
  7.0.405 [C:\Program Files\dotnet\sdk]
  8.0.101 [C:\Program Files\dotnet\sdk]
  8.0.200-preview.23624.5 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.1 [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:\Coding\martincostello\logging-source-generator-dynamically-accessed-members\global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jan 25, 2024
  2. martincostello commented on Jan 25, 2024

    @martincostello
    MemberAuthor

    I also attempted to workaround this by adding the following, but that produces a different compilation error:

    <ItemGroup>
      <Using Include="System.Diagnostics.CodeAnalysis" />
    </ItemGroup>
    Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(4,30): error CS0579: Duplicate 'DynamicallyAccessedMembers' attribute [C:\Coding\martincostello\logging-source-generatordynamically-accessed-members\Repro.csproj]
  3. added this to the 9.0.0 milestone on Jan 25, 2024
  4. ghost removed
    untriaged
    on Jan 25, 2024
  5. added
    source-generator
    on Jan 25, 2024
  6. martincostello commented on Jan 28, 2024

    @martincostello
    MemberAuthor

    Having a quick look now to see if this is something simple enough to try and fix myself, and found that while native AoT was the motivating scenario for me finding this issue, it appears to apply to any generic class with a type argument annotated with an attribute:

    using Microsoft.Extensions.Logging;
    
    public partial class Greeter<[Foo] T>(ILogger<Greeter<T>> logger)
    {
        public void Greet() => Log.Greet(logger);
    
        private static partial class Log
        {
            [LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "Hello.")]
            public static partial void Greet(ILogger logger);
        }
    }
    
    [AttributeUsage(AttributeTargets.GenericParameter)]
    public sealed class FooAttribute : Attribute
    {
    }
    ❯ dotnet build
    MSBuild version 17.8.3+195e7f5a3 for .NET
    Restore complete (0.7s)
      Repro failed with errors (3.3s)
        Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(4,28): error CS0579: Duplicate 'Foo' attribute
    
    Build failed with errors in 4.3s

    Generated code:

    // <auto-generated/>
    #nullable enable
    
        partial class Greeter<[Foo] T>
        {
            partial class Log
            {
                [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "8.0.9.3103")]
                private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __GreetCallback =
                    global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(1, nameof(Greet)), "Hello.", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 
    
                [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "8.0.9.3103")]
                public static partial void Greet(global::Microsoft.Extensions.Logging.ILogger logger)
                {
                    if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
                    {
                        __GreetCallback(logger, null);
                    }
                }
            }
        }
  7. ghost added
    in-pr
    on Jan 28, 2024
  8. added a commit that references this issue on Jan 30, 2024
    bf5c474
  9. added a commit that references this issue on Jan 30, 2024
    7c852c4
  10. ghost removed
    in-pr
    on Jan 30, 2024
  11. added a commit that references this issue on Jan 30, 2024
    afc3b6a
  12. added a commit that references this issue on Jan 31, 2024
    b54886b
  13. locked and limited conversation to collaborators on Mar 1, 2024
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

    area-Extensions-Loggingsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions