Enabling Microsoft's Code Analysis on .NET Core Projects

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?

Answers


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.

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>

Need Your Help

Detecting attached USB devices under Mac OSX

c macos usb

First of all a statement: I'm a newbie when it comes to programming for the Mac. However I have an assignment which requires USB communication with a device so I thought I'll start with something b...

Make a 2D pixel plot with matplotlib

python grid matplotlib plot pixel

I got the following data from some calculations: