Search code examples
c#.netwpfcsharp-source-generator

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


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?


Solution

  • 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.