dotnet / roslyn-analyzers

MIT License
1.59k stars 465 forks source link

Nuget package not working for Xamarin.Mac project #6391

Open wiegell opened 1 year ago

wiegell commented 1 year ago

Analyzer

Diagnostic ID: CA****

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 7.0.0 (Latest)

Describe the bug

Nuget package is not working when installed to Xamarin.Mac project.

Steps To Reproduce

  1. Open VS for Mac
  2. Create new Cocoa app
  3. Install .netanalyser nuget package
  4. Create standard .editorconfig file in solution root

Actual behavior

No CA**** warnings in editor or at compile

Expected behavior

Should show warnings without workaround

Workaround

The nuget package is only mentioned in the Xamarin .csproj in "import tags". If installed to a .netcore 3.1 project it will instead reference a non-local copy from $USER/.nuget. The .netcore import looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Microsoft.CodeAnalysis.NetAnalyzers" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
</Project>

If you copy the PackageReference above to the .csproj file of the Xamarin.Mac project, then delete the hidden .vs folder, then the CA**** warnings appear. This however messes up the package manager of Visual Studio, so that all packages referenced with <Reference> tags no longer appear in the solution explorer / packages or in the nuget package manager GUI. This however can be mitigated by once again deleting the PackageReference tag in the .csproj (make sure to build the project before deletion) and the analyzer will continue to work, since it's now registered in the json file in .vs/{PROJECTNAME}/xs/project-cache/{PROJECTNAME}-debug.json

Additional context

Possibly related to https://github.com/dotnet/roslyn-analyzers/issues/4776 It's worth mentioning, that the deprecated FxCop analyser is functioning without workaround.

Versions

VS for mac, v 17.4.1 (build 28)

SDKs etc. Visual Studio Community 2022 for Mac Version 17.4.1 (build 28) Installation UUID: 413a9513-2680-4d5a-94a7-3293a290ac24 Runtime .NET 6.0.9 (64-bit) Architecture: X64 Roslyn (Language Service) 4.4.0-6.22578.12+3c6ab8e1715e5b080fb7bb77070810ab71e09387 NuGet Version: 6.3.1.1 .NET SDK (x64) SDK: /usr/local/share/dotnet/sdk/7.0.100/Sdks SDK Versions: 7.0.100 6.0.403 6.0.402 6.0.401 5.0.408 5.0.405 3.1.425 3.1.424 3.1.423 3.1.416 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks .NET Runtime (x64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.0 6.0.11 6.0.10 6.0.9 5.0.17 5.0.14 3.1.31 3.1.30 3.1.29 3.1.22 Xamarin.Profiler Version: 1.8.0.19 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler Updater Version: 11 Xamarin Designer Version: 17.4.0.136 Hash: d49c9ff6d3 Branch: remotes/origin/d17-4 Build date: 2022-12-02 15.34.42 UTC Apple Developer Tools Xcode 13.4.1 (20504) Build 13F100 Xamarin.Android Not Installed Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.12 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Android SDK Manager Version: 17.4.0.54 Hash: 6eabb9e Branch: remotes/origin/d17-4 Build date: 2022-12-02 15.34.47 UTC Android Device Manager Version: 0.0.0.1206 Hash: 886af39 Branch: 886af39 Build date: 2022-12-02 15.34.47 UTC Xamarin.Mac Version: 8.12.0.2 (Visual Studio Community) Hash: 87f98a75e Branch: d17-3 Build date: 2022-07-25 20:18:54-0400 Xamarin.iOS Xamarin.iOS not installed. Can't find mtouch or the Version file at /Library/Frameworks/Xamarin.iOS.framework/Versions/Current. Build Information Release ID: 1704010028 Git revision: f17dcd4436244cc06ed14dc5238c4d3fa788144f Build date: 2022-12-02 15:32:32+00 Build branch: release-17.4 Build lane: release-17.4 Operating System Mac OS X 12.5.0 Darwin 21.6.0 Darwin Kernel Version 21.6.0 Sat Jun 18 17:07:25 PDT 2022 root:xnu-8020.140.41~1/RELEASE_X86_64 x86_64
wiegell commented 1 year ago

The problem is solved by changing reference style of the project, see https://github.com/xamarin/xamarin-macios/issues/17145 The issue is kept open, as this necessity should be better documented