Compile-time only Nuget dependency (FxCop)
Asked Answered
W

1

7

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?

Washery answered 13/8, 2018 at 19:27 Comment(0)
W
9

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>
Washery answered 13/8, 2018 at 22:47 Comment(1)
Any ideas on how to have the same behavior while using packages.config?Norford

© 2022 - 2024 — McMap. All rights reserved.