docs/compilers/Rule Set Format.md
This document, in conjunction with the Rule Set Schema, describes the structure of .ruleset files used by the C# and Visual Basic compilers to turn diagnostic analyzers on and off and to control their severity.
This document only discusses the required and common parts of .ruleset files; for a full set, please see the schema file.
The following demonstrates a small but complete example of a .ruleset file.
<RuleSet Name="Project WizBang Rules"
ToolsVersion="12.0">
<Include Path="..\OtherRules.ruleset"
Action="Default" />
<Rules AnalyzerId="System.Runtime.Analyzers"
RuleNamespace="System.Runtime.Analyzers">
<Rule Id="CA1027" Action="Warning" />
<Rule Id="CA1309" Action="Error" />
<Rule Id="CA2217" Action="Warning" />
</Rules>
<Rules AnalyzerId="System.Runtime.InteropServices.Analyzers"
RuleNamespace="System.Runtime.InteropService.Analyzers">
<Rule Id="CA1401" Action="None" />
<Rule Id="CA2101" Action="Error" />
</Rules>
</RuleSet>
Rule set files can be passed to csc.exe and vbc.exe using the /ruleset switch, which takes an absolute or relative file path. For example:
vbc.exe /ruleset:ProjectRules.ruleset ...
vbc.exe /ruleset:..\..\..\SolutionRules.ruleset ...
vbc.exe /ruleset:D:\WizbangCorp\Development\CompanyRules.ruleset ...
Within MSBuild project files the rule set can be specified via the CodeAnalysisRuleSet property. For example:
<PropertyGroup>
<CodeAnalysisRuleSet>ProjectRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
Note that because the rule set is specified via a property rather than an item, IDEs like Visual Studio will not show the rule set as a file in your project by default. For this reason it is common to explicitly include the file as an item as well:
<ItemGroup>
<None Include="ProjectRules.ruleset" />
</ItemGroup>
RuleSetThe RuleSet is the root element of the file.
Attributes:
Children: Include, Rules
IncludePulls in the settings from the specified rule set file. Settings in the current file override settings in the included file.
Parent: RuleSet
Attributes:
Children: None.
RulesHold settings for rules from a single diagnostic analyzer.
Parent: RuleSet
Attributes:
Children: Rule
RuleSpecifies what action to take for a given diagnostic rule.
Parent: Rules
Attributes:
Children: None.
Notes:
Within a Rules element, a Rule with a given Id may only appear once. The C# and Visual Basic compilers impose a further constraint: if multiple Rules elements contain a Rule with the same Id they must all specify the same Action value.
The difference between the Hidden and None actions is subtle but important. When a diagnostic rule is set to Hidden instances of that rule are still created, but aren't displayed to the user by default. However, the host process can access these diagnostics and take some host-specific action. For example, Visual Studio uses a combination of Hidden diagnostics and a custom editor extension to "grey out" dead or unnecessary code in the editor. A diagnostic rule set to None, however, is effectively turned off. Instances of this diagnostic may not be produced at all; even if they are suppressed rather than being made available to the host.