Skip to content

Generated code (GeneratedRegex, LoggerMessage) does not work inside new extension blocks #121593

@Kinematics

Description

@Kinematics

Description

Use of code generation attributes (I have specifically encountered this with GeneratedRegex and LoggerMessage, but there are presumably others) fails when applied to an extension method that is inside a new extension block. Such attributes work fine on the original style of extension methods.

I submitted this bug on the Roslyn side, and was told that this was an issue that required an update on the analyzer/generator side. Since GeneratedRegex and LoggerMessage are inside this repo, I'm re-submitting it here.

Reproduction Steps

Sample Code:

public static partial class LoggerMessages
{
    // Do not work:

    extension(ILogger logger)
    {
        [GeneratedRegex(@"PatternToFind", RegexOptions.IgnoreCase, "en-US")]
        public partial Regex PatternRegex1 { get; }

        [LoggerMessage(
            EventId = 100,
            Level = LogLevel.Debug,
            Message = "An event happened. Current count: {count}")]
        public partial void DoAThing1(int count);
    }

    // Do work:

    [GeneratedRegex(@"PatternToFind", RegexOptions.IgnoreCase, "en-US")]
    public static partial Regex PatternRegex2 { get; }

    [LoggerMessage(
            EventId = 100,
            Level = LogLevel.Debug,
            Message = "An event happened. Current count: {count}")]
    public static partial void DoAThing2(this ILogger logger, int count);
}

Expected behavior

Generated code should function with the newly available extension block syntax.

Actual behavior

Code generators fail to compile when placed inside the new extension block syntax.

Regression?

No response

Known Workarounds

Do not use the new extension block syntax when you need to apply code generation.

Configuration

.NET 10
Windows 11
x64
Visual Studio 18.0.0, Compiler version: '5.0.0-2.25529.7 (68a9207c)'.
I do not know if it is specific to this configuration.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Metasource-generatorIndicates an issue with a source generator featureuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions