1

I have created 2 custom analyzer using template "Analyzer with Code Fix (.NET Standard)" in VS 2019. Each analyzer has different project, and package project as well. Also I have Common project with some common shared code that are using every analyzer.

To be more clear, Custom Analyzer solution looks like:

enter image description here

csproj file for analyzer (Analyzer1 and Analyzer2) is:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
      <TargetFramework>netstandard2.0</TargetFramework>
      <IsPackable>false</IsPackable>
      <PackageId>*$(MSBuildProjectFile)*</PackageId>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" />
      <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
  </ItemGroup>

  <ItemGroup>
      <ProjectReference Include="..\Common\Common.csproj" />
  </ItemGroup>
</Project>

Producing Nuget is by Package csproj files, i.e. for Analyzer1 is:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
      <IncludeBuildOutput>false</IncludeBuildOutput>
      <SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
      <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

    <PropertyGroup>
        <PackageId>Analyzer1</PackageId>
        <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
        <Description>CustomAnalyzer</Description>
        <DevelopmentDependency>true</DevelopmentDependency>
        <NoPackageAnalysis>true</NoPackageAnalysis>

        <TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>

        <Version>1.0.2</Version>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\Analyzer1\Analyzer1.csproj" />
    </ItemGroup>

    <Target Name="_AddAnalyzersToOutput">
        <ItemGroup>
            <TfmSpecificPackageFile Include="$(OutputPath)\Analyzer1.dll" PackagePath="analyzers/dotnet/cs" />
            <TfmSpecificPackageFile Include="$(OutputPath)\Common.dll" PackagePath="analyzers/dotnet/cs" />
        </ItemGroup>
    </Target>
</Project>

When I produce nupkg file both dlls are in folder "analyzers\dotnet\cs":

  • Analyzer1.dll or Analyzer2.dll
  • Common.dll

I include Nuget packages in final project in csproj file as:

<ItemGroup>
    <PackageReference Include="Analyzer1" Version="1.0.2" />
    <PackageReference Include="Analyzer2" Version="1.0.2" />
</ItemGroup>

When using those 2 analyzers, I got this situation:

enter image description here

Goal: I want to have independent analyzers that all uses some shared code from Common project. Everything works except showing Common.dll that are not actually analyzer but code that analyzer depends on.

QUESTION: Is it possible to hide Common.dll from Dependencies/Analyzers and only have separate analyzers (Analyzer1, Analyzer2...) in VS 2019 or VS2022?

Note: Yes, I know that if I structure analyzer differently (put all them into one project, and then also put shared code in the same project) final Nuget will contain only one dll and will not have this problem, but still is it possible what I've asked?

Spaso Lazarevic
  • 906
  • 8
  • 16
  • 1
    Try to put Common.dll to separate Nuget package. – Sergey Miryanov Dec 28 '21 at 08:24
  • But the goal is to have independent analyzer, so anyone could pick whichever analyzer they want to use without thinking do we need something extra. – Spaso Lazarevic Dec 28 '21 at 10:17
  • Put common in a separate NuGet and let A1 and A2 reference that NuGet as a dependency. That way common will be downloaded when either A1 or A2 or both are added to your solution. – jessehouwing Mar 02 '22 at 09:28

0 Answers0