Skip to content

Visual Studio 17.2.6 Breaks CommunityToolkit.Mvvm #6792

Description

@jamiehankins

Version Used: 17.2.6

Steps to Reproduce:

  1. Create a WPF exe project targeting net6.0-windows
  2. Reference CommunityToolkit.Mvvm like so:
    <PackageReference Include="CommunityToolkit.Mvvm" Version="7.1.2" />
  3. Create a simple class inheriting from ObservableObject, like so:
using CommunityToolkit.Mvvm.ComponentModel;

namespace ObservablePropertyTest
{
    public partial class TestVM : ObservableObject
    {
        [ObservableProperty]
        private string _testString = string.Empty;
    }
}

Expected Behavior:
In 17.2.5, it built and ran fine.

Actual Behavior:

Rebuild started...
1>------ Rebuild All started: Project: ObservablePropertyTest, Configuration: Debug Any CPU ------
Restored C:\git\ObservablePropertyTest\ObservablePropertyTest.csproj (in 2 ms).
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(12,27,12,70): error CS0101: The namespace 'CommunityToolkit.Mvvm.ComponentModel.__Internals' already contains a definition for '__KnownINotifyPropertyChangedOrChangingArgs'
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(7,6,7,51): error CS0579: Duplicate 'global::System.CodeDom.Compiler.GeneratedCode' attribute
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(8,6,8,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(9,6,9,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(10,6,10,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\__KnownINotifyPropertyChangedOrChangingArgs.cs(11,6,11,29): error CS0579: Duplicate 'global::System.Obsolete' attribute
1>C:\git\ObservablePropertyTest\CommunityToolkit.Mvvm.SourceGenerators\CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator\ObservablePropertyTest.TestVM.cs(12,23,12,33): error CS0102: The type 'TestVM' already contains a definition for 'TestString'
1>Done building project "ObservablePropertyTest_yynlzhol_wpftmp.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

It appears that somehow the CommunityToolkit.Mvvm source generators are being run twice.

If you create a WPF class library, things work fine. They're only broken in a WPF app.

Here is my test project:
ObservablePropertyTest.zip

Activity

  1. RikkiGibson commented on Jul 12, 2022

    @RikkiGibson
    Member

    If you build your project from the developer command prompt and pass -bl it should produce a binary log which you can use to confirm that:

    1. multiple source generators are being passed
    2. where those generators originate from.
  2. jamiehankins commented on Jul 13, 2022

    @jamiehankins
    Author

    @RikkiGibson

    If you build your project from the developer command prompt and pass -bl it should produce a binary log which you can use to confirm that:

    1. multiple source generators are being passed
    2. where those generators originate from.

    Wow, first I've seen of this tool. There's an insane amount of info, but I can't seem to find that needle in this haystack.

    Here's the output from building my tiny test project in case you have a better idea of where in there to look.
    msbuild.zip

  3. RikkiGibson commented on Jul 13, 2022

    @RikkiGibson
    Member

    It's in the Csc task which contains the build errors, under CommandLineArguments

    image

    In this case the paths appear to be:

    • C:\Users\jamie.hankins\.nuget\packages\communitytoolkit.mvvm\7.1.2\analyzers\dotnet\cs\CommunityToolkit.Mvvm.SourceGenerators.dll
    • C:\Users\jamie.hankins\.nuget\packages\communitytoolkit.mvvm\7.1.2\analyzers\dotnet\cs\CommunityToolkit.Mvvm.SourceGenerators.dll

    In other words, the two paths are exactly the same. It's odd that msbuild would do that, It feels like the compiler should be modified to give a diagnostic and/or dedupe the duplicate generators. I'll have to look more closely later to confirm that and determine a root cause.

  4. k-u-s commented on Jul 13, 2022

    @k-u-s

    Got similar problem after updating from 6.0.6 to 6.0.7 (sdk 6.0.301 to 6.0.302) but with code ganareted via CsWin32

    Minimal repro crated by WPF apllication template from Visual studio 2022 and adding mentioned nuget:
    WpfApp.Issue62582.zip

    Moving code generation to separate class library fixes issue.

    dotnet --info

    C:\WpfApp.Issue62582>dotnet --info
    .NET SDK (reflecting any global.json):
    Version: 6.0.302
    Commit: c857713418

    Runtime Environment:
    OS Name: Windows
    OS Version: 10.0.22000
    OS Platform: Windows
    RID: win10-x64
    Base Path: C:\Program Files\dotnet\sdk\6.0.302\

    global.json file:
    Not found

    Host:
    Version: 6.0.7
    Architecture: x64
    Commit: 0ec02c8c96

    .NET SDKs installed:
    3.1.400 [C:\Program Files\dotnet\sdk]
    5.0.103 [C:\Program Files\dotnet\sdk]
    6.0.201 [C:\Program Files\dotnet\sdk]
    6.0.301 [C:\Program Files\dotnet\sdk]
    6.0.302 [C:\Program Files\dotnet\sdk]

    .NET runtimes installed:
    Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.AspNetCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

    Download .NET:
    https://aka.ms/dotnet-download

    Learn about .NET Runtimes and SDKs:
    https://aka.ms/dotnet/runtimes-sdk-info

    dotnet build

    C:\WpfApp.Issue62582>dotnet build Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET Copyright (C) Microsoft Corporation. All rights reserved.

    Determining projects to restore...
    Restored C:\WpfApp.Issue62582\WpfApp.Issue62582.csproj (in 221 ms).
    C:\WpfApp.Issue62582\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.WINDOW_STYLE.g.cs(23,15): error CS0101: The namespace 'Windows.Win32.UI.WindowsAndMe
    ssaging' already contains a definition for 'WINDOW_STYLE' [C:\WpfApp.Issue62582\WpfApp.Issue62582_uddecyia_wpftmp.csproj]

    Build FAILED.

    C:\WpfApp.Issue62582\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.WINDOW_STYLE.g.cs(23,15): error CS0101: The namespace 'Windows.Win32.UI.WindowsAndMe
    ssaging' already contains a definition for 'WINDOW_STYLE' [C:\WpfApp.Issue62582\WpfApp.Issue62582_uddecyia_wpftmp.csproj]
    0 Warning(s)
    1 Error(s)

    Time Elapsed 00:00:02.94

  5. DingpingZhang commented on Jul 13, 2022

    @DingpingZhang

    This is a generic problem that affects all source generator projects.
    It seems to occur only when referenced by the nuget package, If referenced in the following (manual reference), there will be no problem, so the test cases within the source generator project are normal.

        <ProjectReference Include="..\Xxx.Generator\Xxx.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  6. Tum4ik commented on Jul 13, 2022

    @Tum4ik

    I also have faced the same problem with Refit.

  7. tan-force commented on Jul 13, 2022

    @tan-force

    I uninstalled VS 17.2.6 and installed 17.2.5 that used to work. It doesn't anymore :-( Still the same error.

  8. glennawatson commented on Jul 13, 2022

    @glennawatson

    @tan-force seems to be related to dotnet 6.0.7 so you may have to uninstall that version

  9. tan-force commented on Jul 13, 2022

    @tan-force

    Yes! Uninstalled dotnet 6.0.7, and now it works. Thank you.

  10. Tum4ik commented on Jul 13, 2022

    @Tum4ik

    You still can use the latest version of Visual Studio. Here is a workaround that works for me:

    1. Download and install the previous version of .NET SDK (6.0.301) https://dotnet.microsoft.com/en-us/download/dotnet/6.0
    2. Add global.json file to the root of your solution (where .sln file is located)
    3. The content of the global.json:
    {
        "sdk": {
            "version": "6.0.301",
            "rollForward": "disable"
        }
    }
    1. Rebuild solution.
  11. aahz16384 commented on Jul 13, 2022

    @aahz16384

    Source generators still working in actual VS2022 preview.

  12. LadislavLang commented on Jul 13, 2022

    @LadislavLang

    I've the same problem in latest preview (17.3.0 Preview 3.0)

  13. 69 remaining items

  14. rasyidf commented on Sep 1, 2022

    @rasyidf

    Is the fix for this available in the .NET 7 preview SDK at this point?

    Still occurred in latest .NET 7 Preview

  15. Gakk commented on Sep 2, 2022

    @Gakk
    <Target Name="RemoveDuplicateAnalyzers" BeforeTargets="CoreCompile">
        <!-- Work around https://github.com/dotnet/wpf/issues/6792 -->
    
        <ItemGroup>
            <FilteredAnalyzer Include="@(Analyzer->Distinct())" />
            <Analyzer Remove="@(Analyzer)" />
            <Analyzer Include="@(FilteredAnalyzer)" />
        </ItemGroup>
    </Target>
    

    Thanks @rainersigwald for this solution. It looks like it fixed the issue with the duplicate analyzers 🙂👍

    Unfortunately it also looks like it may have an unattended side effect 🤔
    I have a WPF application, and it has a constructor with a parameter:

    public partial class App : System.Windows.Application
    {
        public App(SplashScreen splash)
        {
            // (removed for readability)
        }
    }

    MSBuild then generates a partial class with Main(), but this now uses a constructor without parameters - which in my case does not exist:

    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    // (...)
    public partial class App : System.Windows.Application
    {
        // (removed for readability)
            
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "6.0.8.0")]
        public static void Main() {
            LocusMobile.Client.App app = new LocusMobile.Client.App();
            app.InitializeComponent();
            app.Run();
        }
    }

    Resulting in:

    error CS7036: There is no argument given that corresponds to the required formal parameter 'splash' of 'App.App(SplashScreen)'

    Anyone other that has experienced this with the workaround? Or also has a constructor with a parameter that is not affected by this workaround, and can confirm that this issue must come from something else?

    I experienced this issue with the generators when switching from legacy project file to SDK style project file, so my error might be related to something else as well...

  16. ThomasGoulet73 commented on Sep 2, 2022

    @ThomasGoulet73
    Contributor

    Hey @Gakk, I doubt that your issue is related to analyzers/generators.

    This is likely the cause:
    I experienced this issue with the generators when switching from legacy project file to SDK style project file

    Without a repro I can only speculate but it looks like you use your own Main entry point instead of the generated App.Main and pass a SplashScreen to the constructor of App. In your legacy project file, App.xaml was probably included as Page instead of ApplicationDefinition which meant that App.Main was never generated. If you do not use the generated App.Main, you need to set the MSBuild property EnableDefaultApplicationDefinition to false in your project. If this does not fix your issue, please open a new Github issue without a repro project since it is probably unrelated to this one.

    Thanks.

  17. added a commit that references this issue on Sep 9, 2022
  18. ThomasGoulet73 commented on Sep 13, 2022

    @ThomasGoulet73
    Contributor

    This issue should now be fixed in .Net SDK 6.0.9 released today.

  19. ecxdev commented on Sep 15, 2022

    @ecxdev

    I had the same problem today! How to fix it?

    `Version: 6.0.401
    Commit: 0906eae6f8

    Runtime Environment:
    OS Name: Windows
    OS Version: 10.0.19044
    OS Platform: Windows
    RID: win10-x64
    Base Path: C:\Program Files\dotnet\sdk\6.0.401\

    global.json file:
    Not found

    Host:
    Version: 6.0.9
    Architecture: x64
    Commit: 163a63591c

    .NET SDKs installed:
    6.0.401 [C:\Program Files\dotnet\sdk]

    .NET runtimes installed:
    Microsoft.AspNetCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
    Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
    Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    Microsoft.WindowsDesktop.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]`

  20. virzak commented on Sep 15, 2022

    @virzak

    @ecxdev If you have .NET 7 preview-7 upgrade to .NET 7 RC-1 and it should fix it. Either that or specify version 6 in global.json

  21. ecxdev commented on Sep 15, 2022

    @ecxdev
    <Target Name="RemoveDuplicateAnalyzers" BeforeTargets="CoreCompile">
        <!-- Work around https://github.com/dotnet/wpf/issues/6792 -->
    
        <ItemGroup>
            <FilteredAnalyzer Include="@(Analyzer->Distinct())" />
            <Analyzer Remove="@(Analyzer)" />
            <Analyzer Include="@(FilteredAnalyzer)" />
        </ItemGroup>
    </Target>
    

    The project was rebuilt successfully when I deleted this previously inserted code from the project file.

  22. added a commit that references this issue on Sep 28, 2022
  23. ghost locked as resolved and limited conversation to collaborators on Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bugregression

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions