question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Visual Studio 17.2.6 Breaks CommunityToolkit.Mvvm

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:19
  • Comments:57 (9 by maintainers)

github_iconTop GitHub Comments

65reactions
rainersigwaldcommented, Jul 13, 2022

For a workaround @ThomasGoulet73 I tend to prefer @jlaanstra’s from https://github.com/dotnet/wpf/pull/6680#issuecomment-1183552170 with a small modification to use MSBuild engine functionality instead of a task:

<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>
27reactions
Tum4ikcommented, Jul 13, 2022

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.
Read more comments on GitHub >

github_iconTop Results From Across the Web

MVVM Toolkit error MVVMTK0008 - .NET Community Toolkit
While updating the C# language version will generally work with no issues, as the compiler has to be resilient to missing types and...
Read more >
VS2017 Pro v 15.9.53 c# .net 4.7.2 After Adding ...
I am trying to learn how to use the Microsoft CommunityToolkit.mvvm for a work project. I am building the project via VS2017 Right...
Read more >
Support Center
Hello,. According to Visual Studio 17.2.6 Breaks CommunityToolkit.Mvvm #6792 and Fix build when using source generators coming from NuGet ...
Read more >
Issue with Resharper, .NET Core 6 and MVVM Community ...
Hi. I am getting lots of errors on a project which uses WPF, the MVVM Community Toolkit and .NET Core 6. The issues...
Read more >
CommunityToolKit 8.0 컴파일 에러가 납니다. - 닷넷데브
Visual Studio 17.2.6 Breaks CommunityToolkit.Mvvm. opened 10:50PM - 12 Jul 22 UTC. closed 04:51PM - 14 Jul 22 UTC.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found