How to enforce ReSharper code style rules in the build? - Part 3
How to enforce ReSharper code style rules in the build with a NuGet package, and how to configure the NuGet package source.
How to enforce ReSharper code style rules in the build? - Part 3
A rule which can be ignored will be ignored. When ReSharper shows only warnings, developers are busy with the deadlines and the warnings stay for months. If we want the code quality to be consistent across all teams, the rules must be enforced at a place where nobody can skip them, and that place is the build. In the previous article, we discussed what is ReSharper, the code style rules and the severity levels. In this article, we are going to see how the code style enforcement policy works, how to enable it for a solution, and how to configure the NuGet package source to get it.
TL;DR Summary
- The code style enforcement policy is a build step, and it is injected into the Visual Studio project file.
- We cherry-pick the most important rules from the ReSharper rule sets and ship them with the policy.
- Both are packaged as a NuGet package, and the package is available in our internal NuGet feed on Azure DevOps.
- The policy and the rules are associated with the project and the solution file respectively during the package installation.
- When ReSharper validation fails, the policy makes the Visual Studio build fail, so developers must fix the issues to make the build succeed.
- The package is a tool type package, so it will not add any binaries or dependencies to your project.
- Include the ReSharper team shared settings file into source control.
Why do we need the build enforcement?
By default, ReSharper reports the issues as warnings. It highlights them in the editor but never stops the developer. As a result of this, the issues which are important for us are still reaching the repository. We need a way to say “these rules are mandatory”, without asking every developer to change their own settings.
Because of the above reasons, we have created a code style enforcement policy as a build step.
How does the enforcement policy work?
Let us take a closer look at the parts,
- Enforcement policy - It is a build step which is injected into the Visual Studio project file.
- Cherry-picked rules - ReSharper has 2200+ rules. We selected the most important ones to enforce some best practices, and we ship them as rule sets along with the policy.
- NuGet package - The policy and the rule sets are shipped together as a NuGet package. During the package installation, the enforcement policy is associated with the project file and the cherry-picked rules are associated with the solution file.
The following diagram depicts what the package adds to a solution,

ReSharper makes use of these rule sets, starts to validate, and highlights the failures in red underlined squiggles. When developers forget to take any action on the validation errors before the build, the enforcement policy makes the Visual Studio build fail. So, developers must fix all the code style issues reported by ReSharper to make the build succeed.
The following diagram explains the flow during the build. The flow highlighted in purple is the happy path, a build without any violation,

Must-Do Items:
- Ensure the ReSharper is installed on your machine.
- Install the code style enforcement policy to all your Visual Studio solutions.
- Include the ReSharper team shared settings file into source control. It is added to your solution during the first build after the enforcement policy is added to the solution.
- Clean all the ReSharper code style validation errors and warnings before the build.
- Cleaned code should work as expected without impacting the functionality.
Warning: Don’t fix the style issues blindly. After the clean-up, run your tests, otherwise a “small rename” can break the functionality.
How to enable the ReSharper code style enforcement policy?
Follow the following instructions to enable the policy for a solution. We need the NuGet package source in Visual Studio for this, so configure it first if you haven’t done it (refer the next section),
- Open your solution file in Visual Studio.
- Ensure you have configured the NuGet package source for our internal feed. If not, refer the “How to configure the NuGet package source?” section.
- Go to Solution Explorer and right-click on the solution name.
- Click on Manage NuGet Packages for Solution, it will bring the package manager window for the solution.
- Change the Package source to our internal feed.
- Go to the Browse tab and search for the code analysis package name which is given by your team.
- Select the package, it will bring the project selection section on the package manager page.
- Select the main project of the solution, it is not necessary to choose all the projects.
- Click on the Install button to install the package on the selected project. It is a tool type package, so it will not add any binaries to your project or dependencies.
Now, the solution is enabled with the ReSharper enforcement policy. This policy is applied to every build that you do in Visual Studio. After the build, it lists all the code style errors in the Error List window. In general, these errors are produced by the ReSharper code inspector, so you can proactively fix them by fixing the ReSharper warnings before the build.
Note: There is one limitation with the Error List. The errors produced by the code style policy won’t navigate to the source file when we double click on them. But it displays the file name and the line number of the issue, so use them to find the code.
How to configure the NuGet package source?
The package is available in a NuGet feed under Azure DevOps Artifacts. Follow the following instructions to configure it as a package source in Visual Studio,
- Login to Azure DevOps and go to the Artifacts page.
- Select the feed which hosts the code analysis package.
- Click on Connect to Feed, it will bring the help page to configure this feed for various applications and tools.
- Click on Visual Studio, it will bring the help page for Visual Studio. Copy the Name and Source items from the page for later usage.
- Open Visual Studio and go to Tools → Options. It will bring the Options window.
- Go to NuGet Package Manager → Package Sources section.
- Click on the plus button which is available at the top right corner. It will add a new package source entry in the list with a default name and source.
- Paste or enter the copied values into the Name and Source inputs respectively.
- Click on the Update button to save the changes, and then click the OK button to close the Options window.
Just to pulse check, go to the Manage NuGet Packages menu on any opened project or solution. You should be able to see the new package source under the Package source dropdown, which is available at the top right corner of the page. Change to the new source and go to the Browse tab, now you should see all the packages which are available in this feed. It may ask you to log in to Azure DevOps when you try to access the list of packages.
Wrap up
In this article, we have looked at why the build enforcement is needed, how the policy and the cherry-picked rules are shipped as a NuGet package, how it fails the build, and how to enable it for a solution. I appreciate your time and efforts to read this article. In the next article, I will walk you through the ReSharper installation, the license activation, the dotCover must-dos and how to troubleshoot the slowness.
Related Posts
ReSharper guidelines: installation, license, code coverage and troubleshooting - Part 4
The ReSharper best practices, how to install it and activate the license, the dotCover must-dos and how to troubleshoot the slowness.
What is ReSharper? Code style rules, severity and dotCover - Part 2
What ReSharper is, how its 2200+ code style rules and severity levels work, and what dotCover and code coverage tell us.
Why we need ReSharper to improve the code quality? - Part 1
Why small code style issues become technical debt, and why we need immediate feedback and build enforcement with ReSharper.