Search code examples
c#msbuildcompiler-constructionstatic-code-analysis

How can we get C# compiler to treat static code analysis errors as warnings


During development it can be tedious to make the code adhere to all the static code analysis rules we have. We'd love instead to then make all of the static code analysis rules be treated as warnings when doing locally building when developing, while when we push and the build runs on our CI, we want it to treat them as errors.

It would be great if there was a similar MSBuild option to the <TreatWarningsAsErrors/> just flipped e.g. <TreatErrorsAsWarnings/>, preferably in MSBuild since we have packaged up our rules into a common package used by all projects. Having to maintain a separate ruleset for this scenario is too much of a hassle.

Right now we are actually turning off all rules locally and only have them on for our CI. This then requires discipline from every developer to remember to build it locally for release to get the errors in the code before pushing. While warnings would be very visible all the time and a friendly reminder to fix them before pushing.


Solution

  • Thanks @MarcGravell for the pointers. I believe it led me down the correct path. We've been meaning to switch to .editorconfig files from the XML based ones. And luckily it turns out one can package these up in a NuGet, like we do with the XML based rulesets.

    So from what I can figure out, I could then add a conditional for Debug builds that sets all rules to warning with a global override in its own .editorconfig file.

    Anyone looking to package up these rules into a NuGet package, I found this article: https://hackernoon.com/how-to-use-common-editorconfig-as-a-nuget-package