19

Can you disable styleCop in VS?

Scenario:

  1. Press "Disable StyleCop" button
  2. Run/debug some test code
  3. The button automatically, enable StyleCop again. Therefore you have to actively disable it again it you want to run without StyleCop.
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Chris G.
  • 23,930
  • 48
  • 177
  • 302

5 Answers5

10

You can disbale StyleCop for the entire solution by placing a Settings.StyleCop in the root of your solution folder, with the following contents:

<StyleCopSettings Version="105">
  <GlobalSettings>
    <BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
  </GlobalSettings>
</StyleCopSettings>

You'll need to restart Visual Studio after doing so.

Dimitri Troncquo
  • 354
  • 2
  • 11
  • This is the simplest and best solution since it doesn't require creating a new build configuration. – pymaxion Jul 10 '18 at 14:28
  • Adding a new `Settings.StyleCop` file with these contents did not turn off StyleCop for me with a .NET Core WebAPI project in VS 2019, where StyleCop is installed via nuget and rules are in a `.editorconfig` file, even after closing and reopening the solution. – ruffin Feb 11 '22 at 17:59
7

I've setup a separate build configuration that doesn't run code analysis.

I now have the following configurations in VS:

  • Release
  • Debug
  • Debug (No code analysis)

You have to manually choose which configuration you want to build (i.e. step 3 in your list would be a manual step)

In the build targets file I've included code along these lines:

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug (No code analysis)' ">
    <RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>

<PropertyGroup>
    <DefineConstants Condition="('$(RunCodeAnalysis)'=='true') and '$(Language)'=='C#' ">CODE_ANALYSIS;$(DefineConstants)</DefineConstants>
</PropertyGroup>
Montgomery 'monty' Jones
  • 1,061
  • 2
  • 14
  • 27
  • I tried to [create such a build configuration according to your instructions](http://stackoverflow.com/questions/20302700/how-to-create-a-visual-studio-build-configuration-without-stylecop-code-analysis), but failed. Perhaps, you can give me a hint what I did wrong? – Lernkurve Nov 30 '13 at 16:36
2

This requires you to edit the .sln file.

http://stylecop.codeplex.com/discussions/285902

Filip
  • 656
  • 4
  • 8
  • 2
    The link does not say anything about disabling STyleCop. Maybe content is removed? Can you please update the answer with relevant content if you remember. This is why link-only answers are discouraged. – Aniket Thakur Jun 18 '18 at 09:57
1

The most effective way to achieve something like what you want would be to exclude all the files in your project from StyleCop analysis for one build configuration. If you add the following ItemGroup to your project file:

<ItemGroup>
    <ExcludeFromStyleCop Include="**\*.cs" Condition=" '$(Configuration)' == 'DebugNoStyleCop' " />
<ItemGroup/>

...that will exclude all .cs files in your project from StyleCop analysis when the "DebugNoStyleCop" configuration is selected. Obviously you can choose some other configuration name that will suit you better by making the appropriate substitution. It's not quite the once-only "off" button that you'd like but it's fairly close.

1

For a way to do this through the VS2013 UI, in StyleCop 4.7.49 (I'm not sure what the minimum versions for this are), you can:

  1. right-click on a project in Project Explorer
  2. select "StyleCop Settings"
  3. on the "Rules" tab of the dialog that opens, uncheck the "C#" root of the Enabled rules tree