35

Our team uses the Code Analysis feature with a custom ruleset to cause our build to fail if we forget to do things like null checks on method arguments.

However, now as we create a new .NET Core project, it doesn't look like Code Analysis is a feature of these new projects. There is no UI for it in the Project Properties area, and adding a custom ruleset to the project as recommended here only appears to affect StyleCop Analyzers (the SAxxxx rules).

Is there any way to enable Code Analysis (CAxxxx) rules in a .NET Core project?

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315

2 Answers2

38

Update 2021

FxCopAnalyzers have been deprecated, and it is now recommended to use the more limited Microsoft.CodeAnalysis.NetAnalyzers package.

See https://github.com/dotnet/roslyn-analyzers and https://learn.microsoft.com/en-us/visualstudio/code-quality/migrate-from-fxcop-analyzers-to-net-analyzers?view=vs-2019 for more details.

Update

Apparently the right way to do this is to install the Microsoft.CodeAnalysis.FxCopAnalyzers NuGet package. This works great, even on ASP.NET Core projects, and doesn't require the <RunCodeAnalysis> flag at all.

Original Answer

I realized that there's another tag in the csproj file which actually enables code analysis. The <PropertyGroup> tag in my .csproj file now looks like this:

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
    <CodeAnalysisRuleSet>..\MyCompanyCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
    <RunCodeAnalysis>true</RunCodeAnalysis>
  </PropertyGroup>

And it works great, at least on normal projects. An ASP.NET Core project is producing the following errors:

CA0055 : Could not identify platform for 'C:\Source\...\bin\Debug\netcoreapp1.1\....dll'.
CA0052 : No targets were selected.
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 2
    Is there documentation for your updated answer when using the nuget package? For example, how do you use a custom ruleset if you go the nuget route? – Scott Lin Nov 15 '17 at 23:14
  • 4
    @ScottLin: You still need to add the `` tag to your project file. The NuGet approach will still look to that tag to decide what rules to check for. – StriplingWarrior Nov 16 '17 at 20:13
  • This is deprecated. https://github.com/dotnet/roslyn-analyzers use Microsoft.CodeAnalysis.NetAnalyzers instead – maxisam Feb 04 '21 at 18:44
4

Normally, the only thing you need to do is install the Microsoft.CodeAnalysis.FxCopAnalyzers nuget on your project.

But as mentioned correctly, this does not work, especially for .Net Core (currently in vs2017).

In order to work for aspnet core projects, as well, and resolve the error:

"Could not identify platform for ..."

Manually modify the project's csproj file and make sure to not insert the RunCodeAnalysis tag. Make the PropertyGroup like that:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <CodeAnalysisRuleSet>..\MyStylecop.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Also, if you need to put a given ruleset file, make sure to place it to the correct path, as seen above ..\MyStylecop.ruleset. MyStylecop.ruleset is the file with the rules (actually the ones suppresed I think - so it is inverse logic).

For example my ruleset file is:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Default stylecop settings" Description="This rule set contains all rules (as warnings), with a few specific supressions." ToolsVersion="15.0">
  <IncludeAll Action="Warning" />
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="CA1004" Action="None" />
    <Rule Id="CA1006" Action="None" />
    <Rule Id="CA1020" Action="None" />
    <Rule Id="CA1025" Action="None" />
    <Rule Id="CA1032" Action="None" />
    <Rule Id="CA1054" Action="None" />
    <Rule Id="CA1055" Action="None" />
    <Rule Id="CA1056" Action="None" />
    <Rule Id="CA1062" Action="None" />
    <Rule Id="CA1300" Action="None" />
    <Rule Id="CA1303" Action="None" />
    <Rule Id="CA1704" Action="Warning" />
    <Rule Id="CA1709" Action="None" />
    <Rule Id="CA2007" Action="None" />
    <Rule Id="CA2225" Action="None" />
    <Rule Id="CA2227" Action="None" />
    <Rule Id="CA2233" Action="None" />
    <Rule Id="CA2234" Action="None" />
    <Rule Id="CA2237" Action="None" />
    <Rule Id="CS1591" Action="None" />
    <Rule Id="CA1715" Action="None" />
  </Rules>
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA1101" Action="None" />
    <Rule Id="SA1116" Action="None" />
    <Rule Id="SA1117" Action="None" />
    <Rule Id="SA1118" Action="None" />
    <Rule Id="SA1208" Action="None" />
    <Rule Id="SA1600" Action="None" />
    <Rule Id="SA1601" Action="None" />
    <Rule Id="SA1602" Action="None" />
    <Rule Id="SA1623" Action="None" />
    <Rule Id="SA1633" Action="None" />
    <Rule Id="SA1634" Action="None" />
    <Rule Id="SA1637" Action="None" />
    <Rule Id="SA1640" Action="None" />
    <Rule Id="SA1652" Action="None" />
    <Rule Id="SA0001" Action="None" />
    <Rule Id="SA1314" Action="None" />
  </Rules>
</RuleSet>
cnom
  • 3,071
  • 4
  • 30
  • 60
  • 1
    In current VS2017, as of today it works and runs all recommended rulesets on addition of nuget package, no edits , changes in .csproj is needed – Sundara Prabu Jun 18 '19 at 14:31