Kyle McClellan
Kyle McClellan

Reputation: 993

How to disable .NET analyzers from a specific NuGet package

I have a .NET project that references a library from another project via an internal NuGet server. The referenced project uses third party code analyzers (StyleCop) also referenced via NuGet and does not mark the assets as private. Consequently, these analyzers are pulled in and ran as part of my project.

I understand that analyzer rules can be suppressed using a combination of .editorconfig and/or NoWarn. However, I would like to prevent the analyzers from running at all for my project. I've tried setting ExcludeAssets=analyzers in my package reference, but this doesn't appear to have an effect.

<PackageReference Include="LibraryWithAnalyzers" Version="*" ExcludeAssets="analyzers" />

Is there something I am missing?

Upvotes: 2

Views: 86

Answers (1)

Kyle McClellan
Kyle McClellan

Reputation: 993

Not exactly an ideal solution, but adding the following target seems to work:

<Target Name="DisableStyleCop" BeforeTargets="CoreCompile">
  <ItemGroup>
    <Analyzer Remove="@(Analyzer)" Condition="'%(Filename)' == 'StyleCop.Analyzers' OR '%(Filename)' == 'StyleCop.Analyzers.CodeFixes'" />
  </ItemGroup>
</Target>

Thanks to this answer for setting me on the right track.

This workaround would not be not needed if the referenced project had set PrivateAssets="all" when referencing StyleCop. Whether or not this was an oversight or due to heavy-handed stylistic opinions is not clear.

Upvotes: 0

Related Questions