Roslyn analyzer missing assembly warning
Asked Answered
A

2

9

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?

Adore answered 26/4, 2018 at 15:40 Comment(0)
T
3

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.

Telemeter answered 3/9, 2020 at 20:5 Comment(0)
A
7

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.

Adore answered 30/4, 2018 at 13:31 Comment(1)
Similar issues may arise with source generators. A description of how to address the issue is included at github.com/dotnet/roslyn/blob/master/docs/features/… under "Use functionality from NuGet packages." Look specifically at the discussion of "generation-time dependencies."Adore
T
3

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.

Telemeter answered 3/9, 2020 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.