Skip to content

Source Generator support for WPF project blocked? #3404

Closed

Description

@Marv51

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.

Activity

  1. jmarolf commented on Aug 27, 2020

    @jmarolf
    Contributor

    I believe the issue here is that the intermediate project that the WPF build creates is not passing the source generators to the compiler.

    @jaredpar and @chsienki do you agree?

  2. chsienki commented on Aug 27, 2020

    @chsienki
    Member

    The issue is that the WPF intermediate build clears the analyzers collection as a way of disabling analyzers from running. This obviously wipes out the generators too, so we need to update the WPF targets to use the new 'SkipAnalyzers' option instead. See: dotnet/roslyn#46669

    I think there is an issue for this WPF + generators problem somewhere already, but can't find it right now.

  3. Marv51 commented on Aug 28, 2020

    @Marv51
    Author

    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.

  4. transferred this issue fromdotnet/roslynon Aug 28, 2020
  5. jaredpar commented on Aug 28, 2020

    @jaredpar
    Member

    Transferring this issue to the wpf repository as it appears they are not properly passing $(Analyzers) to the compiler when building their intermediate temp project. That in turn can impact build success as generators are included in that group.

  6. weltkante commented on Aug 30, 2020

    @weltkante

    Please consider documenting the lack of WPF support somewhere, because it "just doesn't work" but without any error.

    This would be a blocking issue for adoption since it makes either source generators or WPF unusable. Just documenting that it doesn't work isn't enough, this needs to be implemented.

    For example the new COM interop story (CsWinRT) is already looking at using source generators to dynamically generate code, so the amount of things WPF would be incompatible with is only rapidly going to grow from here as generators are adopted by other nuget packages.

  7. jaredpar commented on Aug 31, 2020

    @jaredpar
    Member

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

    Intent is to fix the behavior, not document it.

  8. added
    needs more information
    on Aug 31, 2020
  9. self-assigned this
    on Aug 31, 2020
  10. Marv51 commented on Sep 1, 2020

    @Marv51
    Author

    What info is needed @ryalanms?

    (Also, when I was asking for documentation I meant to add a known-issue that it is not working in the previews, while it is being worked on.)

  11. miloush commented on Sep 1, 2020

    @miloush
    Contributor

    @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

  12. clairernovotny commented on Sep 3, 2020

    @clairernovotny

    I believe this is a dup of #810 which I think is the same underlying issue. If not a dup, would be impacted by any source generators that ship as a NuGet package.

  13. weltkante commented on Sep 3, 2020

    @weltkante

    Not a direct dup, note that OP is using <ProjectReference> not <PackageReference>, but they are probably closely related in their underlying issue, and yes you'll probably get the same problem for generators distributed via nuget.

  14. Marv51 commented on Sep 14, 2020

    @Marv51
    Author

    I believe that this should get at least really looked at for 5.0?

    Source Generator are a major feature of c# 9/dotnet 5.0, for them not to work in wpf seems like a problem to me.

    @jaredpar, @jmarolf

  15. jmarolf commented on Sep 14, 2020

    @jmarolf
    Contributor

    CC @dotnet/dotnet-wpf as they will need to decide when this fits into their schedule

  16. added
    Bug
    and removed
    needs more information
    on Nov 6, 2020
  17. added this to the .NET 5.0 Servicing milestone on Nov 6, 2020
  18. Zalutskii commented on Jan 12, 2021

    @Zalutskii

    I found a hint on how to solve the problem here #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.

  19. ryalanms commented on Jan 12, 2021

    @ryalanms
    Member

    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.

  20. Marv51 commented on Jan 22, 2021

    @Marv51
    Author

    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.

    Update: I did it wrong. Attribute works perfectly.

  21. Zalutskii commented on Jan 22, 2021

    @Zalutskii

    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>

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

  22. Nukepayload2 commented on Jan 25, 2021

    @Nukepayload2

    I'm trying to port VB "My WPF extension" of .NET Framework to .NET 5.
    My VB source generator doesn't work because of this bug.
    The <PropertyGroup>.<IncludePackageReferencesDuringMarkupCompilation> element seems has no effect in VB projects.
    The error list of Visual Studio is empty, but there're build errors in the output window. I'm using Visual Studio 16.9 preview 3.
    image
    image
    image
    image

    Here's my project: https://github.com/Nukepayload2/MyWpfExtensionGenerator

  23. Marv51 commented on Jan 25, 2021

    @Marv51
    Author

    Yes this definitely works like @Zalutskii describes. The new attribute fixes the issue. Turning this on by default is tracked by #3974. So to me (the author of this issue) this is fixed.

    Feel free to reopen if issues occur.

  24. clairernovotny commented on Jan 25, 2021

    @clairernovotny

    It's also tracked here for the .NET SDK for 5.0.200: dotnet/sdk#15395

  25. usikpavel commented on Feb 12, 2021

    @usikpavel

    I'm using VisualStudio 16.8.5, not preview.

    In my case the @Zalutskii solution works for simple class generation only, but not for more complicated generator. When I'm trying to apply [AutoNotify] attribute from samples to my ViewModel, then all errors come back again.

    UPD. I found a solution here. In addition to <IncludePackageReferencesDuringMarkupCompilation> project option, you must use the .NET 5.0.102 SDK. 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"
      }
    }
    
  26. ghost locked as resolved and limited conversation to collaborators on Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions