Search code examples
visual-studiomsbuildvisual-studio-2019editorconfig

Specify separate .editorconfig files between debug and release configurations


I am using VS 2019 16.8.3 and I want to specify some code analysis rules (dotnet_diagnostic.CAXXXX.severity) in the solution .editorconfig file, only for release builds.

When I add rules in a .editorconfig file debug build time increases by a few minutes. All Analyzers checkboxes are not selected to "Run on build" in project properties.

So I want to exclude debug build from code analysis. Is it possible to specify a .editorconfig file only for release builds?

Or is it possible to disable this .editconfig for builds and only apply for manual code analysis?


Solution

  • The .editorconfig file is not a good choice to add the custom analysis rules due to your configuration conditions.

    The best way is to use MSBuild function Directory.Build.props file to add them which is more pure.

    1) Create a ruleset file and add any code analysis which you want to use for Debug

    2) add a file called Directory.Build.props under your project folder

    enter image description here

    3) add these under the file:

    <Project>
    
      <PropertyGroup Condition="'$(Configuration)'=='Debug'">
    
        <!--the full path of your ruleset file for Debug mode-->
        <CodeAnalysisRuleSet>xxx\RuleSet1.ruleset</CodeAnalysisRuleSet>
      </PropertyGroup>
    
    
    </Project>
    

    So when you build your project with Release, the ruleset will skip due to the msbuild condition and only works for Debug mode.

    You can also add another ruleset for Release mode. And add the code Analysis under the corresponding configuration in the respective ruleset files.