c# - How to disable .NET analyzers from a specific NuGet package - Stack Overflow

admin2025-04-17  3

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?

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?

Share Improve this question asked Jan 30 at 20:17 Kyle McClellanKyle McClellan 99310 silver badges29 bronze badges 1
  • FWIW, I believe that any analyzer rules with a severity of "none" aren't run. So if you can't find a good way to remove the dll entirely, at least they shouldn't have much of an impact if you turn them off with .editorconfig. – StriplingWarrior Commented Jan 31 at 19:38
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://anycun.com/QandA/1744892341a89103.html