Friday, August 19, 2016

Static Code Analysis in ASP.NET Core

To ensure conformance of coding styles in team, I wanted to enable the Static Code Analysis on our ASP.NET Core project. I spent couple of hours identifying the current version of StyleCop and additional few hours to configure it on my solution. This blog outlines the configuration of my project to perform StyleCop static code analysis and also to override some of the default rules.

For the .NET Compiler Platform ("Roslyn") StyleCop provides the static code analysis. Here is the GitHub location for this tool.


As per the documentation mentioned on the GitHub site, StyleCop can be installed using NuGet Package Manager.




Alternatively you can manually add the dependency in project.json
    "StyleCop.Analyzers": "1.0.0"

It is recommended that the package be marked as build type as shown below:
    "StyleCop.Analyzers": {
      "version": "1.0.0",
      "type": "build"
    }

Once StyleCop is installed, you can notice the warnings after building the project



To effectively utilize StyleCop, it needs to be configured as below:

  • One of the default rule, SA1633, requires that you need to specify the copyright information. Here is the copyright text I am using on top of each class file
//---------------------------------------------------------------------------------------
// <copyright file="ChartFactory.cs" company="Nootus">
//  Copyright (c) Nootus. All rights reserved.
// </copyright>
// <description>
//  This is factory class which is used to create and format data for displaying charts
// </description>
//---------------------------------------------------------------------------------------
  • For this copyright to properly work, we need to define a config file
  • Add “stylecop.json” to the project or solution
  • Specify the proper copyrightText. Here is my stylecop.json
{
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "companyName": "Nootus",
      "copyrightText": "Copyright (c) Nootus. All rights reserved."
    }
  }
}
  • The compiler needs to be told about this config file. For that add the following to the project.json
  "buildOptions": {
    "additionalArguments": [ "/additionalfile:stylecop.json" ]
  }
  • Note that you need to specify the relative path of stylecop.json

The above configuration should enable default rules on the project.

In addition to running the default rules, you can also specify particular rules or override any of the rules. For my requirement, I want to disable the xmlDoc option (rule SA1652). Below are the configuration steps need to disable this rule.
  • Create a rule file where you can specify the override. I am calling this file as stylecop.ruleset. Here is my rule file where I am specifying not to check the SA1652 rule.
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Override Rules" Description="Override Rules for the solution." ToolsVersion="14.0">
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA1652" Action="None" />
  </Rules>
</RuleSet>
  •  This rule file needs to be specified in the build option of the compiler. Modify the project.json to include the rulefile as shown below
"buildOptions": {
  "additionalArguments": [ "/ruleset:stylecop.ruleset", "/additionalfile:stylecop.json"]
 }
With the above configuration our project will be ready for the Static Code Analysis by StyleCop. Next step is to get conformance of coding styles in team. Happy coding :)