CsWin32: Workaround for WPF PackageReference and source generators bug
When developing a WPF project, dotnet/wpf#810 and dotnet/wpf#3963 will cause the WPF “inner build” to omit important elements to source generators, leading to build breaks anywhere you use the generated code as reported by a *_wpftmp.csproj
project file.
For example you may see a compiler error as if the generated types do not exist:
D:\temp\WpfApp1\WpfApp1\MainWindow.xaml.cs(15,25): error CS0234: The type or namespace name ‘Sdk’ does not exist in the namespace ‘Microsoft.Windows’ (are you missing an assembly reference?) [D:\temp\WpfApp1\WpfApp1\WpfApp1_2gbqycls_wpftmp.csproj]
To fix this you must use the .NET 5.0.102 SDK and set the following property in your project:
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
To make sure you are using the SDK with the fix, you may need to add a global.json
file with this content to the root of your repo:
{
"sdk": {
"version": "5.0.102",
"allowPrerelease": false,
"rollForward": "major"
}
}
Here is a fully working minimal sample: wpfsample.zip
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 19 (10 by maintainers)
@AArnott , figured out the issue. I needed to add
<UseWPF>true</UseWPF>
for the project to be formally recognized as a SDK-style WPF project. After that the new flag works@mddmborg Check out #601, which sounds like what you’re hitting.
True. The .NET 6 SDK includes the new behavior as on by default whereas .NET 5 SDK had it off by default.
It seems to be fixed in dotnet 6.
Ok, thanks @AArnott, I think we figured it out.
In the sample, the using path is Microsoft.Windows.SDK, but for some reason, in our project, it must be Microsoft.Windows.Sdk. Using that camel case seems to have fixed our issue. Such a small thing. 😃
@AArnott - Shoot, I’m sorry. I’m not sure what repo I thought I was in - I just followed google from a search for
IncludePackageReferencesDuringMarkupCompilation
- a property that looked suspicious when debugging a build issue. I’ll post to another repo - thanks!@nick-beer, your question sounds unrelated to this source generator and more appropriate for the WPF audience. Can you try posting your question over on the dotnet/wpf repo?