Skip to content

Unable to disable Microsoft.Extensions.Logging.SourceGenerator with DisableMicrosoftExtensionsLoggingSourceGenerator #4772

@RussKie

Description

@RussKie

Description

dotnet/extensions have own generators for LoggerMessageAttribute] and require the runtime's one to be disabled.

I'm having issues building dotnet/extensions-samples:

D:\Development\dotnet-r9-samples\src\Telemetry\LatencyMeasurement\LatencyContext\LatencyDataExporter\Log.cs(12,65): error SYSLIB1015: Argument 'level' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1015.md) [D:\Development\dotnet-r9-samples\src\Telemetry\LatencyMeasurement\LatencyContext\LatencyContext.csproj::TargetFramework=net9.0]
D:\Development\dotnet-r9-samples\src\Telemetry\LatencyMeasurement\LatencyContext\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(58,36): error CS0757: A partial method may not have multiple implementing declarations [D:\Development\dotnet-r9-samples\src\Telemetry\LatencyMeasurement\LatencyContext\LatencyContext.csproj::TargetFramework=net9.0]

The repo-global Directory.Build.targets sets the flag:

<Project>
  <Import Project="Sdk.targets" Sdk="Microsoft.DotNet.Arcade.Sdk" />

  <PropertyGroup>
    <DisableMicrosoftExtensionsLoggingSourceGenerator>true</DisableMicrosoftExtensionsLoggingSourceGenerator>
  </PropertyGroup>

</Project>

However, the runtime's generator is still being executed:
image

Looking at binlog it looks like _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers target is being executed before another target that re-adds the generator back:
image

I think that AfterTargets must include ResolveReferences target instead - testing it locally everything "just works" (c):
https://github.com/dotnet/runtime/blob/8cb3c6187f9e1338f22f9e37c4dfe615123eaa59/eng/MultiTargetRoslynComponent.targets.template#L21-L24

  <Target Name="_{TargetPrefix}RemoveAnalyzers" 
          Condition="'$({DisableSourceGeneratorPropertyName})' == 'true'"
-         AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets"
+         AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets;ResolveReferences"
          DependsOnTargets="_{TargetPrefix}GatherAnalyzers">

Reproduction Steps

Expected behavior

The generator is removed.

Actual behavior

The generator is being re-added.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions