Search code examples
.net-corenuget.net-standardfxcoproslyn-code-analysis

Compile-time only Nuget dependency (FxCop)


I am creating a .Net Standard 2.0 Nuget package and I want to perform static code analysis for the build. However, when I add the Microsoft.CodeAnalysis.FxCopAnalyzers package it becomes a runtime dependency, so any projects that reference my Nuget package are also installing analyzers. I feel those should be opt-in concerns by the calling code.

Is there anyway to prevent that dependency from being enforced?


Solution

  • I ended up finding the solution on my own. It just took a little more research on the Package Reference syntax in project files, specifically Controlling Dependency Assets. The key was to open up the *.csproj file and add a PrivateAssets node like the following (note ExcludeAssets and IncludeAssets are set to the default values and could have been omitted):

    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.1">
      <PrivateAssets>all</PrivateAssets>
      <ExcludeAssets>none</ExcludeAssets>
      <IncludeAssets>all</IncludeAssets>
    </PackageReference>