wpf: Source Generator support for WPF project blocked?

Version Used: dotnet 5 preview 8, visual studio 16.8 preview 2

Steps to Reproduce: Create fresh WPF project from VS modify project like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows</TargetFramework>
        <UseWPF>true</UseWPF>
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

    <ItemGroup>
	    <ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>
</Project>

Expected Behavior: The source generator referenced works for me in a console dotnet core app. I would expect this to work in a WPF project too.

Actual Behavior: In a freshly created WPF (dotnet core) project from the VS-template, however the generator does not seem to run.

Is this intended behavior?

As the generator is not used at all, I have not included it here. It is a slight variation of the HelloWorld sample.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 12
  • Comments: 20 (8 by maintainers)

Most upvoted comments

Just documenting that it doesn’t work isn’t enough, this needs to be implemented.

Intent is to fix the behavior, not document it.

Thanks for pointing this out, @Zalutskii. Existing source generator and PackageReference issues have not been linked to the source generator change: #3846. Apologies.

This was made ‘opt-in’ to reduce the possibility of breaking compilation of existing WPF applications, but will eventually be turned on by default in .NET 6.0.

I found a hint on how to solve the problem here https://github.com/dotnet/wpf/issues/3974

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
...
    <IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
...
  </PropertyGroup>

In my case it works.

Please consider documenting the lack of WPF support somewhere, because it “just doesn’t work” but without any error. It took me quite a while to figure out what was the issue here and even in hindsight I couldn’t find any warning.

I tried the property that @Zalutskii was mentioning above IncludePackageReferencesDuringMarkupCompilation, but it does not seem to work? Or maybe that PR didn’t make it into 5.0.2 after all.

One think I would like to point out, that while most people (and that attribute) are talking about PackageReference, please don’t forget about ProjectReference. At least as far as I know, referencing a source generator within the same solution should be done by ProjectReference.

here I give an example of how to add the Source Generator as a Project Reference. If you want to add as NuGet it will look like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
...
    <IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
...
  </PropertyGroup>
  <ItemGroup>
...
    <PackageReference Include="RxSourceGenerator" Version="1.1.2" />
...
  </ItemGroup>
</Project>

Microsoft Visual Studio Community 2019 16.8.4 If you use resharper, it must be the latest version, or disabled.

@Marv51 from another issue

@miloush needs-more-info was merely our way of marking the issue as “we’ve seen it, but we don’t have time in this meeting to read the whole thread, understand it, and decide what to do about it”. In this case, the more-info needed is the informed opinion of a team member (me). We need a better GitHub label for this (we’re actually working on this), so as not to confuse people