dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.1k stars 1.17k forks source link

Visual Studio 17.2.6 Breaks CommunityToolkit.Mvvm #6792

Closed jamiehankins closed 2 years ago

jamiehankins commented 2 years ago

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](https://github.com/dotnet/roslyn/files/9097397/ObservablePropertyTest.zip)
RikkiGibson commented 2 years ago

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.
jamiehankins commented 2 years ago

@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

RikkiGibson commented 2 years ago

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

image

In this case the paths appear to be:

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.

k-u-s commented 2 years ago

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

DingpingZhang commented 2 years ago

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" />
Tum4ik commented 2 years ago

I also have faced the same problem with Refit.

tan-force commented 2 years ago

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

glennawatson commented 2 years ago

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

tan-force commented 2 years ago

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

Tum4ik commented 2 years ago

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"
    }
    }
  4. Rebuild solution.
aahz16384 commented 2 years ago

Source generators still working in actual VS2022 preview.

LadislavLang commented 2 years ago

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

candritzky commented 2 years ago

We have the same problem with a WPF (class library) project that uses Refit:

...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(8,18): error CS0101: The namespace 'MyWpfClassLib_tde0zyvz_wpftmpRefitInternalGenerated' already contains a definition for 'PreserveAttribute' [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(5,6): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(6,6): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute [...\MyWpfClassLib_tde0
zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(7,6): error CS0579: Duplicate 'global::System.AttributeUsage' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(7,6): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(8,6): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(9,6): error CS0579: Duplicate 'global::MyWpfClassLib_tde0zyvz_wpftmpRefitInternalGenerated.PreserveAttribute' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(11,6): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(10,6): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(11,6): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(12,6): error CS0579: Duplicate 'global::MyWpfClassLib_tde0zyvz_wpftmpRefitInternalGenerated.PreserveAttribute' attribute [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(14,6): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(15,19): error CS8646: 'SettingsValidationService.IVersionInfoRestClient.GetConfigurationAsync(CancellationToken)' is explicitly implemented more than once. [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(15,19): error CS8646: 'SettingsValidationService.IVersionInfoRestClient.GetVersionInfoAsync(CancellationToken)' is explicitly implemented more than once. [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(20,51): error CS0102: The type 'Generated.MyClassLibIVersionInfoRestClient' already contains a definition for 'Client' [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(21,48): error CS0102: The type 'Generated.MyClassLibIVersionInfoRestClient' already contains a definition for 'requestBuilder' [...\MyWpfClassLib\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(24,16): error CS0111: Type 'Generated.MyClassLibIVersionInfoRestClient' already defines a member called 'MyClassLibIVersionInfoRestClient' with the same parameter types [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(33,52): error CS0111: Type 'Generated.MyClassLibIVersionInfoRestClient' already defines a member called 'GetConfigurationAsync' with the same parameter types [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(41,140): error CS0111: Type 'Generated.MyClassLibIVersionInfoRestClient' already defines a member called 'GetVersionInfoAsync' with the same parameter types [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(49,135): error CS0111: Type 'Generated.MyClassLibIVersionInfoRestClient' already defines a member called 'global::MyWpfClassLib.Settings.SettingsValidationService.IVersionInfoRestClient.GetConfigurationAsync' with the same parameter types [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]
...\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IVersionInfoRestClient.g.cs(57,223): error CS0111: Type 'Generated.MyClassLibIVersionInfoRestClient' already defines a member called 'global::MyWpfClassLib.Settings.SettingsValidationService.IVersionInfoRestClient.GetVersionInfoAsync' with the same parameter types [...\MyWpfClassLib_tde0zyvz_wpftmp.csproj]

Something seems to be broken in the GenerateTemporaryTargetAssembly build task.

CyrusNajmabadi commented 2 years ago

I don't believe this is the right repo for this issue. @rainersigwald do you know where this may need to go?

saivineeth100 commented 2 years ago

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.

Cannot Install .NET SDK (6.0.301) as it is showing error - Another version of this product is already installed

ThomasGoulet73 commented 2 years ago

I apologize for the regression. It was caused by my changes in dotnet/wpf#6534 which were backported to .Net 6 in dotnet/wpf#6680.

I posted workaround for this issue here: https://github.com/dotnet/wpf/pull/6680#issuecomment-1183455941. This is an alternative to this workaround: https://github.com/dotnet/wpf/issues/6792.

I'm working on a fix that can hopefully be included in .Net 6.0.8.

ArtyomAfanasov commented 2 years ago

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.

Cannot Install .NET SDK (6.0.301) as it is showing error - Another version of this product is already installed

@saivineeth100 , I had the same problem. You may have version 6.0.302 installed. I uninstalled 6.0.302 using the control panel. After that I was able to install 6.0.301.

rainersigwald commented 2 years ago

(I typed up most of this before @thomasGoulet73 replied with an authoritative answer, but am going to post it since it shows my debugging process)

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

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

Looking at this a bit more closely, looks like one of them has IsImplicitlyDefined=true metadata.

Searching through Find in Files for Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll, it looks like that's coming from this blob of C:\Program Files\dotnet\sdk\6.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Analyzers.targets

  <!-- .NET Analyzers -->
  <ItemGroup Condition="$(EnableNETAnalyzers)">
    <Analyzer
      Condition="'$(Language)' == 'VB'"
      Include="$(MSBuildThisFileDirectory)..\analyzers\Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers.dll"
      IsImplicitlyDefined="true" />
    <Analyzer
      Condition="'$(Language)' == 'C#'"
      Include="$(MSBuildThisFileDirectory)..\analyzers\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"
      IsImplicitlyDefined="true" />
    <Analyzer
      Include="$(MSBuildThisFileDirectory)..\analyzers\Microsoft.CodeAnalysis.NetAnalyzers.dll"
      IsImplicitlyDefined="true" />
  </ItemGroup>

And that's the only copy I see explicitly mentioned in MSBuild XML.

Looking through the Search Log tab for Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll, I can see that it's passed to the compilers (expected), and also handled in _HandlePackageFileConflicts, an SDK task that is supposed to handle the case where you have (for instance) the same reference applied via a NuGet package and via the targeted runtime, which should be deduplicating.

At the input to that task it's not duplicated:

image

It's also not duplicated on the output side of that task:

image

Ah! All of that is in the main build of the project, but the actual failure is in c:\git\ObservablePropertyTest\ObservablePropertyTest_kb232yxo_wpftmp.csproj, which is a (horrible) implementation detail of XAML builds: the project is copied with modifications and the copy is built within the normal build.

The copy has all of its references and analyzers flattened, so its build doesn't have to waste time resolving them. So it's getting the analyzer from this line (note that there's no metadata on this one):

    <Analyzer Include="C:\Program Files\dotnet\sdk\6.0.302\Sdks\Microsoft.NET.Sdk\targets\..\analyzers\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll" />

But it's also getting added by the Microsoft.NET.Sdk.Analyzers.targets logic, which is the bug.

rainersigwald commented 2 years ago

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>
ThomasGoulet73 commented 2 years ago

@rainersigwald I too prefer @jlaanstra and your modifications. Mine was more about reverting my changes than fixing this issue by removing duplicates. I'll redirect my comment to yours.

michael-hawker commented 2 years ago

FYI @Sergio0694 as well, not sure if V1 and V2 source generators are effected or what, but figured you should be in the loop here too.

JochemPalmsens commented 2 years ago

We have a similar problem with 17.2.6/6.0.302 but are not using CommunityToolkit.MVVC as we are developing a WinUI3 app. Could it be that other community toolkits (like CommunityToolkit.WinUI.UI.Controls) have similar issues? Will the suggested fix also resole that?

antoinebj commented 2 years ago

Could it be that other community toolkits (like CommunityToolkit.WinUI.UI.Controls) have similar issues?

It will break most (if not all) libraries that rely on source generators and are used through NuGet packages. So it also breaks Refit for example.

Will the suggested fix also resole that?

I don't know about the ones that involve MSBuild instructions in the .csproj or .props/.targets files, I suppose that if they work for one they will work for all. But the one which instructs to use the previous .NET SDK version through global.json will work for sure.

dipeshmsft commented 2 years ago

Reopening till the fix gets into .NET 6

kipusoep commented 2 years ago

I had weird issues like 'void' and 'string' being undefined. Fixed it using this comment; https://github.com/dotnet/wpf/issues/6792#issuecomment-1183530305

JochemPalmsens commented 2 years ago

I had weird issues like 'void' and 'string' being undefined. Fixed it using this comment; #6792 (comment)

That's intellisense trying to find the code analysers in the wrong SDK directory.

Either:

oscarjaergren commented 2 years ago

This is one of the most frustrating errors, can't get it working with the workarounds

saivineeth100 commented 2 years ago

This is one of the most frustrating errors, can't get it working with the workarounds

Use this https://github.com/dotnet/wpf/issues/6792#issuecomment-1183633402, It worked for me without changing installation

rbaudouinn commented 2 years ago

For a workaround @ThomasGoulet73 I tend to prefer @jlaanstra's from #6680 (comment) 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>

That worked for me !!!

anwarjaved commented 2 years ago

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.

Cannot Install .NET SDK (6.0.301) as it is showing error - Another version of this product is already installed

@saivineeth100 , I had the same problem. You may have version 6.0.302 installed. I uninstalled 6.0.302 using the control panel. After that I was able to install 6.0.301.

I removed 6.0.302 and installed 6.0.301 but still facing same issue.

saivineeth100 commented 2 years ago

@anwarjaved Use this https://github.com/dotnet/wpf/issues/6792#issuecomment-1200313308 Its working for me without uninstalling anything

danielchalmers commented 2 years ago

still getting this on VS 17.4 Preview 1 with .NET 7 Preview 7

anwarjaved commented 2 years ago

Its fixed for me in VS 17.3 and Toolkit 8

candritzky commented 2 years ago

Its fixed for me in VS 17.3 and Toolkit 8

Interesting. I just tried VS 2022 17.3 including .NET SDK 6.0.400 and the issue is still not fixed for me. The "duplicate PreserveAttribute" compile error still occurs on WPF projects that are using Refit (6.3.2).

anwarjaved commented 2 years ago

Yes I am targeting 6.0.301 using global.json file

anwarjaved commented 2 years ago

Install .net 6.0.301 SDK

Its fixed for me in VS 17.3 and Toolkit 8

Interesting. I just tried VS 2022 17.3 including .NET SDK 6.0.400 and the issue is still not fixed for me. The "duplicate PreserveAttribute" compile error still occurs on WPF projects that are using Refit (6.3.2).

anwarjaved commented 2 years ago

you may need to uninstall some SDK using dotnet-core-uninstall Tool https://docs.microsoft.com/en-us/dotnet/core/additional-tools/uninstall-tool?tabs=windows

candritzky commented 2 years ago

Install .net 6.0.301 SDK

Its fixed for me in VS 17.3 and Toolkit 8

Interesting. I just tried VS 2022 17.3 including .NET SDK 6.0.400 and the issue is still not fixed for me. The "duplicate PreserveAttribute" compile error still occurs on WPF projects that are using Refit (6.3.2).

Sure, the workaround of installing .NET SDK 6.0.301 and pinning the version down via global.json is still working. But the SourceGenerator issue that was introduced in 6.0.302 is still not fixed.

anwarjaved commented 2 years ago

this is my global.json file

{ "sdk": { "version": "6.0.301", "rollForward": "disable" } }

zlatanov commented 2 years ago

I can also confirm that with the latest update VS 17.3.0 and SDK 6.0.400, the duplication of the analyzers is still present.

It seems that the fix wasn't backported - https://github.com/dotnet/wpf/blob/v6.0.8/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFX.targets.

Why?

//cc @dipeshmsft

ThomasGoulet73 commented 2 years ago

The fix was backported to .Net 6 but it was not shipped with .Net 6.0.8. It should be included in .Net 6.0.9 according to the milestone for the PR responsible for backporting to .Net 6 (dotnet/wpf#6800).

danielchalmers commented 2 years ago

The fix was backported to .Net 6 but it was not shipped with .Net 6.0.8. It should be included in .Net 6.0.9 according to the milestone for the PR responsible for backporting to .Net 6 (#6799).

does that also mean it won't be in until .NET 7 RC1? Thank you @ThomasGoulet73

glennawatson commented 2 years ago

6.0.9 according to Thomas by the looks.

JawadJaber commented 2 years ago

I have faced the same issue today, and have solved it by adding the CommunityToolkit.Mvvm as a project reference and not as nuget package. My WPF project is .NET Framework 4.7.2

kfrancis commented 2 years ago

The workaround worked here, WPF (net6.0-windows) using .net 6.0.400 on 17.3

Redvo commented 2 years ago

Pointing out, that Winodws Update (at least on W11) may automatically update your SDK to broken version.

I installed SDK 6.0.301 (.NET 6.0.6) yesterday and everything worked for me. Today, the build failed again with duplication of analyzers error.

Turns out, Windows Update updated .NET SDK to version 6.0.303 (.NET 6.0.7) as part of 2022-08 .NET 6.0.8 Security Update for x64 Client (KB5016990) update.

Tested it manually:

  1. uninstalled SDK 6.0.303
  2. installed SDK 6.0.301
  3. listed SDKs
  4. ran Windows Update
  5. listed SDKs again.

list-sdk

nemosumnihil commented 2 years ago

I had the same problem today. I used this work-around....https://github.com/dotnet/wpf/pull/6680#issuecomment-1183455941 - just placed in csproj file and it seems it will work until 6.0.9 is released.

anwarjaved commented 2 years ago

I am still facing this issue even after global.json setting and work-around in .csproj file

I had the same problem today. I used this work-around....#6680 (comment) - just placed in csproj file and it seems it will work until 6.0.9 is released.

antoinebj commented 2 years ago

I am still facing this issue even after global.json setting and work-around in .csproj file

I can't tell why the csproj workaround doesn't work for you, but the global.json probably doesn't work because Windows Update replaced your SDK with version 6.0.303, which has the bug.

RikkiGibson commented 2 years ago

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

edit: ah, just saw this:

The fix was backported to .Net 6 but it was not shipped with .Net 6.0.8. It should be included in .Net 6.0.9 according to the milestone for the PR responsible for backporting to .Net 6 (#6799).

does that also mean it won't be in until .NET 7 RC1? Thank you @ThomasGoulet73