This page is about configuring analyzers for at least a whole folder or project. On suppressing just single analyzer violations see the "Using the analyzers during development" page. For further details see the official docs.
Place a Directory.Build.props file into the project's folder (or folder with set of projects) with the following contents:
<Project>
</Project>
MSBuild only loads in the closest Directory.Build.props file to the project being built. So this empty props file will supersede any parent Directory.Build.props. To also disable .NET SDK analysis override them from a .globalconfig file placed into the given project's folder. There you can disable any unwanted rules, like disabling .NET code style analysis completely:
dotnet_analyzer_diagnostic.category-Style.severity = none
If you rely on Lombiq.Analyzers
to set up compiler properties such as <LangVersion>
you may still want to import the parent Directory.Build.props file and just disable the code analyzers. Put this into the Directory.Build.props file instead:
<Project>
<PropertyGroup>
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
</Project>
Or if you added Lombiq.Analyzers
to only a single project as a package reference, you can just add the <RunCodeAnalysis>false</RunCodeAnalysis>
element.
By default the dotnet build
command runs analyzers and produces code analysis warnings if there are any but it makes the build slower. Pass the /p:RunCodeAnalysis=false
parameter to disable analyzers during build, like:
dotnet build MySolution.sln /p:RunCodeAnalysis=false
If not all the configuration in this project is suitable for your solution then you can also override them globally. This way, the default configuration will be merged with your custom configuration and you can override any number of rules conveniently at one place for all projects in your solution.
- Create your own .globalconfig file with your own rule configuration, similar to this project's .globalconfig files (see the official docs on this file format).
- Place the file into the folder under which you want the configuration to apply to all projects.
- Now every rule you've configured in your .globalconfig file will take precedence over the default ones. For everything else, the default ones will be applied.
Note that if you add your .globalconfig file to the solution you'll get GUI support for it in Visual Studio and you'll be able to configure rules without manually editing the configuration text.
More complex configuration is also available:
- You can similarly add such .globalconfig files to subfolders, to just override rules for projects in that folder.
- Such files can be placed outside of the folder hierarchy too and referenced from csproj or props files with the
<GlobalAnalyzerConfigFiles>
element, see the docs. - You can further adjust the precedence of .globalconfig files with their names and the
global_level
entry, see the docs.
You can suppress specific analyzer rules for a whole project from code as well. Add a GlobalSuppressions.cs file to your project, add SuppressMessage
attributes to it like below:
// This file is used by Code Analysis to maintain SuppressMessage attributes that are applied to this project.
// Project-level suppressions either have no target or are given a specific target and scoped to a namespace, type,
// member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage(
"Minor Code Smell",
"S1199:Nested code blocks should not be used",
Justification = "UI tests commonly have small inner blocks for managing one-off elements like dropdowns.",
Scope = "module")]
You can't as easily do the same as with .globalconfig files with .editorconfig rules. It's not possible to define explicit inheritance between .editorconfig files so the only option is to use the folder hierarchy: The Build.props files of this project copy the default .editorconfig file into the solution root. If you put your projects below that in the folder hierarchy and use your own .editorconfig there then the latter will take precedence and you can override the default rules. E.g. you can override certain analyzer rules for a whole folder (even a folder within a project) like this:
[*.{config,csproj,json,props,targets}]
indent_size = 4
The project includes further configuration for StyleCop.Analyzers
and SonarAnalyzer.CSharp
in their own formats. These files are referenced as <AdditionalFiles>
elements in the props files corresponding to .NET Core or later and .NET Framework. From your own Directory.Build.props file you need to recreate the contents of the original props file but reference your custom configuration file instead.