Adding R# and StyleCop to your project build process

Most .Net projects, because the team composition isn’t ideal, will tend to force a series of what project leads like to call good coding standards. These are then checked by tools like StyleCop and R# (R# will check for a lot more, not just good coding standards). But for anyone to get feedback on these on a multi solution system, you need to wire them into your build process, this is where your options will open and what you pick will impact the way developers look at it. With StyleCop you have more options than R#, because it’s such an enforce tool in some industries like the finance, there are more community contributions to artefacts that are pluggable in either the TFS build workflow or simply MSBuild.

The right option: MSBuild task

I have found out that the best way to wire these tools is through MSBuild. This is due to the fact that developers do not run these tools very often or willingly in many cases. Having them integrated as MSBuild tasks will produce build warnings in Visual Studio as the developer continuous builds the solution as part of his development cycle. This will also give each developer a build output from within Visual Studio exactly the same as a Continuous Integration build that’s triggered through TFS, so if you have a clean error log you’re guaranteed to have a clean build summary on the TFS CI build.

These MSBuild tasks should be applied on a specific configuration, to free Debug from validation passes and not degrade the development experience, I usually create a DebugCI configuration that I run all CI builds on. Most developers also tend to have violations throughout the duration of an entire project, having code validation on a specific configuration also makes the other builds free of violation warnings so that people can focus on potential dangerous warnings instead of having to look through hundreds of them.

Adding StyleCop to a project

Simply do Install-Package StyleCop.MSBuild on the package manager console to install the StyleCop.MSBuild NuGet package. This will add StyleCop to your project, but for all configurations. Unload the project in Visual Studio and Edit the project file, or optionally just edit right away in your preferred text editor. Look for the line that imports the StyleCop targets file:

<Import Project=”..\..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets” Condition=”Exists(‘..\..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets’)” />

And add a condition for the specific configuration you want to target, in my case that’s DebugCI

<Import Project=”..\..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets” Condition=”Exists(‘..\..\packages\StyleCop.MSBuild.4.7.49.1\build\StyleCop.MSBuild.Targets’) And ‘$(Configuration)’ == ‘DebugCI’” />

Adding R# to a project

Jetbrains exposes their common R# tooling assemblies through a NuGet package, just add it to the project that you want to set it up for in the solution (note that not all of the projects need to include the package). The package, when included in the project will import a targets file that in turn references another targets file that does the UsingTask MSBuild task.

Unlike StyleCop, R# is added to a single project, targets the solution file and contains a project filter, so I usually add this to the output project of a solution that has the least chances of being refactored. Like StyleCop, don’t forget to add a condition for a specific configuration.

  <Target Name=”AfterBuild”>
    <InspectCode SolutionFile=”..\[MySolutionFile].sln” IncludedProjects=”[MyProject_1];[MyProject_2]” Condition=” ‘$(Configuration)’ == ‘DebugCI’ ” />
  </Target>

2 thoughts on “Adding R# and StyleCop to your project build process

    1. djfr Post author

      Thanks for the suggestion! At the time I was adding this to a Continuous Integration pipeline, the NuGet package didn’t existed.
      I have edited the post to reference the NuGet package instead and I removed the UsingTask that is now done by the package when added to a project through a targets import.

      Reply

Leave a comment