Visual Studio 2017 StyleCop Analyzers template for .NET Core

With the new Visual Studio 2017 out you might have been busy upgrading your .NET Core solutions to the new format and accidentally found out that StyleCop Analyzers stopped working? Automated upgrade will just upgrade projects, however will not adjust Visual Studio 2017 StyleCop Analyzers settings. Therefore, I’ve updated my template’s source code and added this quick guide.

First, you need to upgrade or install StyleCop Analyzers nuget package (as of now the current version is 1.1.0-beta004). Command to get the latest pre-release version:

Install-Package StyleCop.Analyzers -Pre

.NET Core compatibility. In Visual Studio 2017 StyleCop Analyzers work much better compared to Visual Studio 2015, since you get real-time warnings/errors highlight, fix suggestions that work really well together with ReSharper.

I have following settings on:

  • Make warnings to be treated as errors (disabled by default).
  • Suppress 1701;1702;1705 warnings (enabled by default).

To enable a ruleset that we previously had in “_stylecop” folder in the solution folder, you need to manually edit project file and add following.


    <AdditionalFiles Include="$(SolutionDir)\_stylecop\stylecop.json" Link="stylecop.json" />

This specifies what ruleset to use, and adds a link to StyleCope Analyzers settings file for additional settings. “stylecop.json” could be linked manually using Visual Studio’s UI as well, however applying this change to multiple project files might be easier to copy-paste the code snippet.

In my test solution I have .NET Core application and a library to show how rulesets are shared between projects. My folders and files structure is displayed in the screenshot below.

Visual Studio 2017 StyleCop Analyzers solution files

Important. After quick research I was unable to find how to enable spelling dictionary in StyleCop Analyzers in VS2017, hence “CustomDictionary.xml” file is just a placeholder for now. I will update the post once I find this.

You can easily test if StyleCop Analyzers work by removing method documentation comments and adding extra space after curly braces, and you will see errors highlighted in red right away! See screenshot below.

Visual Studio 2017 StyleCop Analyzers real-time style error highlighting

All in all, just a few small changes and we have StyleCop Analyzers working again in Visual Studio 2017 projects again. Happy coding!

All source code can found on my GitHub.

A previous blog post about Visual Studio 2015 is here.


Update 17/06/2017

Seems like Visual Studio 2017 versions prior VS 2017 Preview 2 (15.3.0-pre.2.0+26606.0) may not pick up StyleCop.Analyzers ruleset during development and wrongly highlight code in red but would work just fine during the build.

Update 24/05/2017

As Sam mentioned in the comment you need to set an absolute path to the ruleset file to make StyleCop.Analyzers work. As in the code snippet above, you could use $(SolutionDir) macro to achieve this. More macros on MSDN.

  • Ben Leonard

    tried out your code for vs 2017. Added a rule to ignore but it is still picking it up. Example rule was SA1300.

    • I’ve just tried adding rule, and it shows a warning after renaming “Class1” to “class1.” And changing “Warning” to “None” disabled the warning. Would you mind sending me the files you’ve modified, e.g. StyleCopeRules.ruleset?

      • Ben Leonard

        I used your github repo and the same effect with vs 2017?

        • I’ve noticed that in the latest stable version of VS2017 (15.2.0 26430.13) StyleCop.Analyzers picks up rules from the file correctly during the build, however in the code editing window it would still highlight the code in red.
          Visual Studio 2017 Preview 2 (15.3.0-pre.2.0+26606.0) handles this better.

  • Sam Piper

    Just a small thing I discovered, you have to use an absolute path to link the ruleset file in the .NET Core csproj file, using a relative path doesn’t work at all. Your example works because you use $(SolutionDir) to calculate an absolute path.

  • Trent Whiteley

    I’ve created a custom Analyzer/CodeFix, compiled/packaged it in Visual Studio 2017 and then installed the NuGet package in another solution in VS 2017 (.NET Core solution). The package installed successfully, but I don’t see an Analyzer section with my analyzer nor do I see any results. I know I can jerry rig this to work by adding an with an tag, but I shouldn’t really have to go to this extreme. Have I missed something?

    • Seems like VS2017 doesn’t have as good code analyzers support as VS2015 had, however VS 2017 Preview 2 (15.3.0-pre.2.0+26606.0) looks a little bit more promising :)