wpf icon indicating copy to clipboard operation
wpf copied to clipboard

CS0757 & CS0102 Logger source generator error

Open ali50m opened this issue 9 months ago • 6 comments

Description

I have a WPF project, and recently I introduced an ASP FrameworkReference to it, but it resulted in a logger source generator error.

So, I simplified the project as much as possible to reproduce this issue. The code is at WpfLogger.

Reproduction Steps

git clone [email protected]:ali50m/WpfLogger.git
cd WpfLogger
dotnet build

Expected behavior

Build successfully.

Actual behavior

Build fail.

  WpfLogger_gyim0c0w_wpftmp failed with 2 error(s) (2.2s)
    C:\Users\liu\source\repos\test\WpfLogger\src\WpfLogger\obj\Debug\net9.0-windows\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(13,36): error CS0757: A partial method may not have multiple implementing declarations
    C:\Users\liu\source\repos\test\WpfLogger\src\WpfLogger\obj\Debug\net9.0-windows\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(9,128): error CS0102: The type 'LoggerExtensions' already contains a definition for '__SayHiCallback'

Build failed with 2 error(s) in 3.8s

Regression?

No response

Known Workarounds

Pick any of following can make the build pass.

  1. remove Asp frameworkreference from the project link
  2. downgrade the nuget of "Serilog.Extensions.Logging" to 3.1.0 link
  3. remove the namespace of this assemble in xaml file link

Impact

It will stop me using logger source generator in my WPF project after introducing ASP FrameworkReference.

Configuration

No response

Other information

dotnet: 9.0.2
windows: 10.0.26100 visual studio: Community Version 17.13.2

ali50m avatar Mar 11 '25 12:03 ali50m

Reference: https://github.com/dotnet/wpf/issues/10093 and https://github.com/dotnet/wpf/issues/8128 and https://github.com/dotnet/wpf/issues/4311


@ali50m Build pass by remove Microsoft.AspNetCore.App FrameworkReference in wpftmp project:

  <Target Name="Fxxxxx" AfterTargets="RemoveDuplicateAnalyzers"
          Condition="$(MSBuildProjectName.Contains('_wpftmp'))">
    <ItemGroup>
      <FrameworkReference Remove="Microsoft.AspNetCore.App" />
    </ItemGroup>
  </Target>

Adding the <IncludePackageReferencesDuringMarkupCompilation>false</IncludePackageReferencesDuringMarkupCompilation> will cause the CS8795 error


Adding the

  <Target Name="Fxxxxx" AfterTargets="RemoveDuplicateAnalyzers">
    <ItemGroup>
      <Analyzer Remove="C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\9.0.1\analyzers/dotnet/roslyn4.4/cs/Microsoft.Extensions.Logging.Generators.dll" />
    </ItemGroup>
  </Target>

will cause the CS8795 error


The logger generator generate twice by WPF and Microsoft.AspNetCore.App ...

The WPF's file is generated in MarkupCompilePass1:

https://github.com/dotnet/wpf/blob/e3ce5f83070d04582f652b5e010c4beb969034ca/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFX.targets#L211C10-L251

,and it be used in

https://github.com/dotnet/wpf/blob/e3ce5f83070d04582f652b5e010c4beb969034ca/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFX.targets#L446

lindexi avatar Mar 11 '25 13:03 lindexi

Also related: https://github.com/dotnet/wpf/issues/10175 https://github.com/dotnet/wpf/issues/7624

Source Generator incompatibility in WPF projects is a growing problem as SG's become more prevalent.

franchyd avatar Mar 11 '25 13:03 franchyd

@franchyd The main reason is WPF have to create the tmp project to help build the xaml. And I think it can fix by using SourceGenerator to build the xaml or convert the xaml to csharp code or VB code, just like the MAUI or UNO or Avalonia.

lindexi avatar Mar 12 '25 00:03 lindexi

@franchyd The main reason is WPF have to create the tmp project to help build the xaml. And I think it can fix by using SourceGenerator to build the xaml or convert the xaml to csharp code or VB code, just like the MAUI or UNO or Avalonia.

Yeah but thats not even on the WPF roadmap. Whereas many SG's are broken now due to the tmp project not de-duplicating SGs https://github.com/dotnet/wpf/issues/7624#issuecomment-2083303755

franchyd avatar Mar 13 '25 14:03 franchyd

I'd like to add the Property as the Identifi of the wpf_tmp project. So that the analyzer can be skip to do more other thing.


~~Emmm, it seems that the _TemporaryTargetAssemblyProjectName is enough.~~

https://github.com/dotnet/wpf/blob/c5b6632f5f80f07869e1ae4ec3fcd976f18c2083/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFX.targets#L375

No. The wpftmp project can not get the right _TemporaryTargetAssemblyProjectName property.


Ok, the $(MSBuildProjectName.Contains('_wpftmp')) is the good one. https://github.com/dotnet/wpf/blob/e3ce5f83070d04582f652b5e010c4beb969034ca/eng/WpfArcadeSdk/tools/ApiCompat.targets#L19

lindexi avatar Mar 17 '25 01:03 lindexi

Looks like the same as https://github.com/dotnet/wpf/issues/9589#issuecomment-3259564750 - I added a workaround and reccomended fix there.

ericstj avatar Sep 05 '25 20:09 ericstj