Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify a custom code analysis ruleset for multiple projects in a solution using Visual Studio 2019?

I've got a custom code analysis ruleset that I want to apply to all configurations of multiple projects in my solution but can't see how I can do it.

To be clear, I'm looking for a way (if any) of doing this in a single step rather then editing the properties of each project from the IDE.

I found this guidance so far: https://learn.microsoft.com/en-us/visualstudio/code-quality/how-to-configure-code-analysis-for-a-managed-code-project?view=vs-2019#specify-rule-sets-for-multiple-projects-in-a-solution

But it doesn't seem to be correct. In Visual Studio 2019, if I go to Analyze > Configure Code Analysis > For Solution I get a blank property page with the message:

NOTE: This property page has been deprecated and will be removed in a future product release.

Is there another way I can do this? I have lots of projects :(

Thanks.

like image 258
Chris Avatar asked Sep 12 '19 10:09

Chris


People also ask

How to view code analysis in Visual Studio 2019?

In Solution Explorer, right-click the FabrikamFiber. Web project node and select Analyze | Run Code Analysis. The Code Analysis feature runs through static code analysis rules as defined by Microsoft and displays the results in the Code Analysis window. Scroll through the list of results and read a few of them.

Where is code analysis in Visual Studio?

Open the solution in Visual Studio. On the Analyze menu, select Configure Code Analysis for Solution. If necessary, expand Common Properties, and then select Code Analysis Settings.

What is a rule set in coding?

A rule set is a grouping of code analysis rules that identify targeted issues and specific conditions for that project. For example, you can apply a rule set that's designed to scan code for publicly available APIs. You can also apply a rule set that includes all the available rules.

How do I create a custom rule in FxCop?

To build custom rules, you would first need to create a Class Library project in Visual Studio and include references to the FxCop library. Note that the FxCop library is available in the FxCopSdk. dll file which in turn is present in the FxCop installation directory in your system.


3 Answers

I've had no answers about if there's a way to do this in Visual Studio so I've had to resort to altering .csproj files directly in a batch fashion.

This script I found by John Robbins is excellent: https://www.wintellect.com/batch-updating-changing-visual-studio-projects-with-powershell/

After installation, my usage was like this in case anyone is interested:

dir -recurse *.csproj | Set-ProjectProperties -OverrideDefaultProperties -CustomConfigurationProperties @{ "CodeAnalysisRuleSet" = ".\SystemStandard.ruleset" }

dir -recurse *.csproj | Set-ProjectProperties -OverrideDefaultProperties -CustomConfigurationProperties @{ "RunCodeAnalysis" = "false" }

dir -recurse *.csproj | Set-ProjectProperties -OverrideDefaultProperties -CustomConfigurationProperties @{ "TreatWarningsAsErrors" = "true" } 
like image 84
Chris Avatar answered Oct 16 '22 11:10

Chris


To specify a rule set for a project, use the CodeAnalysisRuleSet MSBuild property.

To do that, there are several ways you can customize your build

like image 3
Paulo Morgado Avatar answered Oct 16 '22 10:10

Paulo Morgado


If I understand the question correctly, this can be done quite easily by following the steps outlined in this blog post.

The Directory.Build.props approach

The guide is written with StyleCop in mind but the same step should work with any analyzer.

  1. Create a file named Directory.Build.props (use this exact casing) along with your .sln file, i.e. at the top-level of your project. Its content should be something like this:
<Project>
  <PropertyGroup>
    <!-- This part specifies the ruleset file name. Change to something
         more appropriate if not using StyleCop. -->
    <CodeAnalysisRuleSet>$(SolutionDir)StyleCop.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <!-- This part adds StyleCop as a reference in all projects + makes the 
       top-level stylecop.json file be used by all projects. Skip this
       altogether if you are not spefically using StyleCop. -->
  <ItemGroup>    
    <PackageReference Include=”StyleCop.Analyzers” Version=”1.1.1-rc.108" PrivateAssets=”all” />
    <AdditionalFiles Include=”$(SolutionDir)stylecop.json” Link=”stylecop.json” />
  </ItemGroup>
</Project>
  1. Create a StyleCop.ruleset file with your analyzer configuration.

That's it. The next time you run dotnet build or build your project in Visual Studio (you might have to close/reopen the solution if you have it open), these rules should apply.


For reference, here is the Directory.Build.props file in a project of mine: https://github.com/perlun/perlang/blob/master/Directory.Build.props

like image 2
Per Lundberg Avatar answered Oct 16 '22 11:10

Per Lundberg