nunit / nunit.analyzers

Roslyn analyzers for writing unit tests with NUnit
MIT License
88 stars 30 forks source link

Instance of analyzer can not be created - missing dependency for Microsoft.CodeAnalysis? #771

Open StarWars999123 opened 1 month ago

StarWars999123 commented 1 month ago

Hi, while I was able to install the package, compile and refactor my projects with Visual Studio 2022, I faced the following warning on CI upon compile with VS2019:

CSC : warning CS8032: An instance of analyzer NUnit.Analyzers.ClassicModelAssertUsage.ClassicModelAssertUsageAnalyzer cannot be created from C:.nuget\packages\nunit.analyzers\4.2.0\analyzers\dotnet\cs\nunit.analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

I referenced the project in my SDK style .csproj targeting net472/net48 as follows:

    <PackageReference Include="NUnit.Analyzers" Version="4.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

Is my package reference (created automatically) faulty or are you missing a dependency reference to Microsoft.CodeAnalysis?

mikkelbu commented 1 month ago

Hi @StarWars999123 Which version of VS2019 does you CI use? (it should at least be 16.3)

StarWars999123 commented 1 month ago

VS 16.10.3. So shouldn't be the reason why.

manfred-brands commented 1 month ago

@StarWars999123 Unlike with other NuGet packages, the Microsoft.CodeAnalysis depends on the SDK you use to compile your code with as the same DLL is loaded by the compiler. Version 3.11.0 should be part of the latest .NET 5 SDK.

From a command line, can you type: dotnet --list-sdks? I have confirmed that .NET 5.0.416 has version 3.11 of Microsoft.CodeAnalysis. If you have an earlier version of .NET 5 SDK, I can't find what versions they had.

When you build your project on the command line with: dotnet build <project.csproj>, does that build? If yes, type dotnet --version to see what SDK is used.

If you already have VS2022, there is no reason to use VS2019. We target .NET Framework projects, but use the latest .NET 8.0 SDK (and VS2022) to do so. Some of the later C# language features don't depend on the target framework and can be used.

Alternatively, the latest VS2019 is version 16.11.38. Maybe updating to that version fixes things.

StarWars999123 commented 1 month ago

So, I am not able to update those systems. For certain reasons, those systems are still stuck with primarily VS2019. dotnet --List-sdks outputs: 1.1.14 2.1.202 2.1.509 2.1.802 5.0.301 6.0.201

Right now, we were only able to reference the NUnitAnalyzer with a condition to be ignored on CI builds.

manfred-brands commented 1 month ago

According to the NET 5.0 download page .NET 5.0.4xx came with VS2019 16.11.14. The version 5.0.3xx isn't even available for download. The 2nd digit of VS seems to match the one for Microsoft.CodeAnalysis. I presume your VS 16.10.3 comes with version 3.10.0.

If you can't update VS2019 to the latest version, I see you have .NET 6.0.201 installed. You can add a global.json like:

{
  "sdk": {
    "version": "6.0.200",
    "rollForward" : "latestPatch",
    "allowPrerelease": false
  }
}

Otherwise, you need NUnit package 3.3.0 as the last version that depended on Microsoft.CodeAnalysis version 3.8.0