Search code examples
c#.net-coresourcegenerators

Source Generator not functioning with XAML files in .NET 6


Recently migrating some .NET 472 projects to .NET 6. One of these projects is a WPF project, but for some reason, after migration, my Source Generator does not function correctly. I receive the message:

error CS0101: The namespace already contains a definition for type

Indicating that the generator is trying to run twice, perhaps? If I turn the variable IncludePackageReferencesDuringMarkupCompilation to false, it shows an error indicating the variable was never declared (makes sense).

After looking thru google, someone indicated that this only happens after adding XAML files to the project, which I confirmed independently against a test project: The project builds normally until I added a XAML window to the project.

I'm guessing this is something to do with how WPF projects are compiled in a 2-stage mode, but I'd appreciate any suggestion on how to get Source Generators working with XAML in .NET 6.

Thank you.


Solution

  • After some googling I found a couple of github issues with the same problem:

    https://github.com/microsoft/CsWin32/issues/601

    This seems to be a regression bug with the current release of .NET SDK 6.0.302, and the fix is either a slight change to the proj file or downgrade to 301.