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.
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.
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.
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.
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.
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" }
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
If I understand the question correctly, this can be done quite easily by following the steps outlined in this blog post.
Directory.Build.props
approachThe guide is written with StyleCop in mind but the same step should work with any analyzer.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With