Home > Software engineering >  Why is <UseWPF>true</UseWPF> causing my c# source generator to generate duplicates?
Why is <UseWPF>true</UseWPF> causing my c# source generator to generate duplicates?

Time:08-06

I have a C# source generator that generates partial classes. It used to work 100% a couple of months ago for WPF AND non-WPF projects. But now that I am revisiting this, it no longer compiles if I have <UseWPF>true</UseWPF> set in the csproj file. As soon as I have WPF enabled I get build errors suggesting my generated symbols are already defined (aka duplicate generation). The errors disappear if I disable WPF. This all used to work before and nothing has changed in my code since then.

How would I even go about investigating this? Might this be a bug in the dotnet SDK or may something have changed that causes me to have to update my generator?

CodePudding user response:

This is a known issue present in .NET SDK 6.0.7 (more details here).

Here's a workaround that worked for me:

  1. Download and install the previous version of .NET SDK (6.0.6).
  2. Add global.json file next to your solution:
{
    "sdk": {
        "version": "6.0.301",
        "rollForward": "disable"
    }
}
  1. Rebuild.

CodePudding user response:

Have you upgraded .NET to a newer version since the last successful project run?

CodePudding user response:

So I did find that this was indeed a bug in the SDK introduced by this other bug fix https://github.com/dotnet/wpf/pull/6680. I am using the following workaround in csproj file for now until the fix in https://github.com/dotnet/wpf/pull/6798 makes it into a release.

<Target Name="WorkaroundRemoveAnalayzers" BeforeTargets="GenerateTemporaryTargetAssembly">
  <ItemGroup>
    <_TempWorkaroundAnalyzer Include="@(Analyzer)" />
    <Analyzer Remove="@(Analyzer)" />
  </ItemGroup>
</Target>
<Target Name="WorkaroundAddAnalayzers" AfterTargets="GenerateTemporaryTargetAssembly">
  <ItemGroup>
    <Analyzer Include="@(_TempWorkaroundAnalyzer)" />
  </ItemGroup>
</Target>

Downgrading the SDK to 6.0.301 is also apparently an option if one manages to uninstall 6.0.302 which was too cumbersome for me right now since the VS installer didn't let me.

  • Related