Reputation: 4868
I am trying to set global rules for my team. We are using VS2012 with TFS for our C# projects. I'd like to suppress some of the warnings and also treat some of the warnings as errors. I found the way to do it on the project level - project properties -> build tab.
But we have solution with more than hundred projects and I am looking for some easier way to set those rules globally.
Upvotes: 9
Views: 5339
Reputation: 982
You can add a Directory.Build.targets
file in the solution path by adding this content:
<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="$(SolutionDir)GlobalSuppressions.cs"/>
</ItemGroup>
</Project>
or add the ItemGroup
if you already have the file, and create a GlobalSuppressions.cs
file in the same path like as follow:
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this solution.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style",
"IDE1005:Delegate invocation can be simplified.", Justification = "<Pending>")]
// ... other suppression messages
In the IDE, on each project you will see a GlobalSuppressions.cs
file as a link to the same file in the solution path. This way you only have to keep one file updated.
At least in Visual Studio 2022 Community, the only disadvantage is that you cannot add an item via context menu to the global suppression .cs
file, because the IDE creates a new GlobalSuppressions1.cs
file in the current project, but you can always apply it from popup as In source (attribute)
and then manually move the line to the solution-level suppression file.
Upvotes: 0
Reputation: 3173
As of MSBuild 15 (circa 2017) you can use Directory.Build.Props
file in the top folder of your solution. The syntax is the same as csproj, fsproj, or vbproj file and the entries are treated as though they are injected into all project files. You will need to restart Visual Studio to apply the changes. (thanks Jumbo!)
<Project>
<PropertyGroup>
<WarningsAsErrors>CS4014, CS1998</WarningsAsErrors>
</PropertyGroup>
</Project>
Upvotes: 5
Reputation: 35901
A solution is just a (pretty dumb) container for projects. If you open it in a text editor you'll quickly see you can't extend it, only add projects/items.
What you want is one or more common msbuild files specifying all needed options for compiler/linker/whatever tools you use, and Import
it in every single project. We've been using this for years and it's very convenient (though part of the convenience is probably we also wrote a small tool to generate project files to automatically import the global properties so we don't have to mess with them manually)
Alternatively you could add a machine wide file, look in $(MSBuildToolsPath)\Microsoft.CSharp.targets to see where to place those files. I'm not going to copy/paste the content here, but the very first lines basically check if there are user definded files in eg $(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\$(MSBuildThisFile)\ImportBefore and if so they're all imported before all common msbuild stuff. Likewise the end of the Microsoft.CSharp.targets contains similar logic to import files after all common msbuild stuff.
Upvotes: 7