extensions icon indicating copy to clipboard operation
extensions copied to clipboard

Fix logging source-gens duplication on WPF

Open xakep139 opened this issue 1 year ago • 13 comments

This is a naive fix for the #4969

We can also consider fixing RemoveDuplicateAnalyzers Target since it basically adds back the analyzer: https://github.com/dotnet/ResXResourceManager/blob/c7f0d75c55917c5de01f807394c37dd2af11251b/src/Directory.Build.targets#L28

Microsoft Reviewers: Open in CodeFlow

xakep139 avatar Mar 12 '24 16:03 xakep139

Reading the comment https://github.com/dotnet/extensions/issues/4969#issuecomment-1991870300 it suggests the issue is not specific to extension telemetry library and it suggest the fix should be somewhere else maybe in SDK targets? I am not sure.

tarekgh avatar Mar 12 '24 16:03 tarekgh

@tarekgh is there someone else you could suggest to rope in for a review of this?

RussKie avatar Mar 12 '24 21:03 RussKie

@joperezr can advise about that. I have no idea why the analyzer get injected twice in the WPF scenario (if I am understanding the issue correctly).

tarekgh avatar Mar 12 '24 22:03 tarekgh

Reading the comment #4969 (comment) it suggests the issue is not specific to extension telemetry library and it suggest the fix should be somewhere else maybe in SDK targets? I am not sure.

In fact, it is specific for Microsoft.Extensions.Telemetry.Abstractions package. I left that comment to highlight that the root cause isn't related to Microsoft.Extensions.Resilience package - as the author wrote there. The latter references former, that's why it fails to compile even if you reference only resilience bits.

xakep139 avatar Mar 13 '24 09:03 xakep139

@joperezr can advise about that. I have no idea why the analyzer get injected twice in the WPF scenario (if I am understanding the issue correctly).

Correct, the issue doesn't happen on console projects, nor does it happen for WinForms.

xakep139 avatar Mar 13 '24 10:03 xakep139

If it's WPF specific, perhaps we should get WPF folks roped in? E.g., ping @dotnet/dotnet-wpf alias.

RussKie avatar Mar 13 '24 11:03 RussKie

@dotnet/dotnet-wpf folks, can you please suggest a good way of disabling an analyzer/source-generator (or confirm that the approach in this PR is acceptable) for WPF apps?

xakep139 avatar Mar 13 '24 14:03 xakep139

Is there anything interim for us to implement on our end in order to progress while you figure this out? Thanks in advance.

ZzZombo avatar Mar 15 '24 04:03 ZzZombo

Is there anything interim for us to implement on our end in order to progress while you figure this out? Thanks in advance.

Copy the new target into your project, and it should remove the analyzers from the intermediate project.

RussKie avatar Mar 18 '24 03:03 RussKie

Setting to draft for now until we agree this is the best way to proceed.

RussKie avatar Mar 19 '24 00:03 RussKie

I can confirm the change works! Thanks!

ZzZombo avatar Mar 20 '24 02:03 ZzZombo

I propose the following change instead:

--- a/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.targets
+++ b/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.targets
@@ -2,10 +2,10 @@
   <!-- This package should replace the Microsoft.Extensions.Logging.Abstractions source generator. -->
   <Target Name="_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers" 
           Condition="'$(DisableMicrosoftExtensionsLoggingSourceGenerator)' == 'true'"
-          AfterTargets="ResolveReferences">
+          BeforeTargets="CoreCompile">
     <ItemGroup>
-      <_Microsoft_Extensions_Logging_AbstractionsAnalyzer Include="@(Analyzer)" Condition="'%(Analyzer.AssemblyName)' == 'Microsoft.Extensions.Logging.Generators' Or
-                                                                                           '%(Analyzer.NuGetPackageId)' == 'Microsoft.Extensions.Logging.Abstractions'" />
+      <_Microsoft_Extensions_Logging_AbstractionsAnalyzer Include="@(Analyzer)" Condition="'%(Analyzer.Filename)' == 'Microsoft.Extensions.Logging.Generators' Or
+                                                                                           '%(Analyzer.Filename)' == 'Microsoft.Extensions.Logging.Abstractions'" />
     </ItemGroup>
 
     <!-- Remove Microsoft.Extensions.Logging.Abstractions Analyzer -->

RussKie avatar May 17 '24 02:05 RussKie

@RussKie, thanks! I applied your change, please review

xakep139 avatar May 17 '24 11:05 xakep139

Thanks @xakep139!

joperezr avatar May 31 '24 22:05 joperezr