7

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.

jumbo
  • 4,670
  • 5
  • 39
  • 44
  • I'm afraid you have a to write a macro for that because those settings are not kept in the solution. – rene Aug 31 '13 at 09:34
  • You can use the same ruleset file for multiple projects - I assume you are already doing that, and just want a way to avoid specifying that file in each project manually? – Matthew Watson Aug 31 '13 at 09:49
  • I just started to dig deeper into these things. So I must say I am not really sure. I thought that rulesets are defined for code-analysis and that it is something different than compiler warnings. So far I didn't touch rulesets... – jumbo Aug 31 '13 at 10:01
  • 2
    Yeah, like @MatthewWatson said, you can refer to the same ruleset from multiple **.csproj** files, in a (e.g.) common superdirectory, via `..\..\my_rules.ruleset`, but you will have to include it in each **.csproj** separately. By the way, you can also skip the whole ruleset thing and just put (e.g.) `[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.CodeAnalysis.CSharp.Features", "IDE0040")]` in the **AssemblyInfo.cs**, but now you're not sharing much, and you still have to do it for each project. – Glenn Slayden Nov 21 '18 at 07:42

2 Answers2

7

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.

stijn
  • 34,664
  • 13
  • 111
  • 163
  • So I see I will have to write some script to update all project files anyway (machine wide file is not an option for now), am I right? – jumbo Sep 01 '13 at 19:44
  • that indeed seems the only way (apart from manually editing all files); shouldn't be that hard however: all you need to do is insert a single line like `` right before the existing `` line - I'd suggest using a realtive path to the common.props – stijn Sep 02 '13 at 07:45
2

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>
VoronoiPotato
  • 3,113
  • 20
  • 30
  • 1
    Oh, awesome! This works. This is the answer now. It does solve my problem exactly. Thank you. (note: Visual Studio needed to be restarted before it started to work) – jumbo Nov 01 '22 at 08:44