Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn analyzer missing assembly warning

After creating a Roslyn analyzer package targeting .Net Standard 2.0, when I reference the package in another project, I receive the following error:

'C:\Users\username.nuget\packages\analyzer4\1.0.0.1\analyzers\dotnet\cs\Analyzer4.dll' depends on 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' but it was not found. Analyzers may not run correctly unless the missing assembly is added as an analyzer reference as well.

A repro of the project using the Analyzer is here. This repro is a vanilla .Net Core 2.0 console app with a reference to the analyzer and no other code whatsoever. The analyzer itself was built simply by creating the default Analyzer project in Visual Studio, changing it so that it targets netstandard2.0 instead of netstandard1.3, and then building in release mode to generate the .nupkg file. The analyzer does work properly, as the repro demonstrates, but the warning is generated.

There are discussions of this warning at various places on Github, such as here, but in those cases, the analyzer author had deliberately stripped out some local library code. In this case, the analyzer does not reference any other library.

I am not clear on what exactly it means to add an analyzer as an "analyzer reference" rather than just a regular project reference. I did try changing

<PackageReference Include="Analyzer4" Version="1.0.0.1" />

to

<Analyzer Include="..\LocalPackages\Analyzer4.1.0.0.1.nupkg" />

but that resulted in another error message ("PE image doesn't contain managed metadata").

Can anyone explain what this error means and ideally how to fix it?

like image 882
mbabramo Avatar asked Apr 26 '18 15:04

mbabramo


People also ask

Why Roslyn code analyzer?

Not only do Roslyn code analyzers report the same types of problems that legacy analysis does, but they make it easy for you to fix one or all occurrences of the violation in your file or project. These actions are called code fixes. Code fixes are IDE-specific; in Visual Studio, they're implemented as Quick Actions.

How do I use Roslyn analyzers in an empty Unity Project?

This page explains how to use Roslyn analyzers in an empty Unity Project. Note: Roslyn analyzers are only compatible with the IDEs that Unity publically supports, which are Visual Studio and JetBrains Rider. To use an existing Roslyn analyzer library, install the ErrorProne.NET.CoreAnalyzers library from NuGet inside your project.

What are code fixes in Roslyn?

Not only do Roslyn code analyzers report the same types of problems that legacy analysis does, but they make it easy for you to fix one or all occurrences of the violation in your file or project. These actions are called code fixes. Code fixes are IDE-specific; in Visual Studio,...

Is there a repro of the project using the analyzer?

A repro of the project using the Analyzer is here. This repro is a vanilla .Net Core 2.0 console app with a reference to the analyzer and no other code whatsoever.


2 Answers

Some background on this issue is here. When an analyzer depends on another assembly, then both must be listed as analyzers, but there is generally an exception for the core system assemblies. Unfortunately, it does not appear that .Net standard 2.0 has yet been added to the exceptions list; presumably, that will occur at some point in the future. I was able to make code changes to target the analyzer to .Net Standard 1.3 instead, thus avoiding the warning.

This problem will also arise when adding other assemblies (such as Newtonsoft.Json) into your analyzer. One solution to this is simply not to do so; for example, StyleCop eliminated its dependence on Newtonsoft.Json and simply includes the code for LightJson directly in its assembly. Other solutions might be (1) to manually copy the dll you are depending on (taking it from your packages folder if necessary) into the .nupkg file, recognizing that .nupkg is really just a .zip file; or (2) to use a tool like ILMerge to merge the dependency into your DLL. I have not yet experimented with these approaches, so if someone else can produce a step-by-step explanation of how to integrate this into a build for an analyzer, I will mark that as a correct answer.

like image 109
mbabramo Avatar answered Oct 13 '22 00:10

mbabramo


You can use the approach in the Source Generators Cookbook (Thanks to @mbabramo for the link!).

<ItemGroup>
<PackageReference Include="Analyzer4" Version="1.0.0.1" />
</ItemGroup>

Becomes:

<ItemGroup>
<PackageReference Include="Analyzer4" Version="1.0.0.1" PrivateAssets="all" GeneratePathProperty="true" />
<None Include="$(PkgAnalyzer4)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

This should add the package dlls into your analyzer's folder, and it should work.

like image 45
Rolan Avatar answered Oct 13 '22 02:10

Rolan