Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Last active January 31, 2024 22:43
Show Gist options
  • Save MelbourneDeveloper/dfb17737c1a74641c86641531b51e5eb to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/dfb17737c1a74641c86641531b51e5eb to your computer and use it in GitHub Desktop.
Directory.Build.props Enable All Errors
[*.{cs,vb}]
dotnet_diagnostic.CA1062.severity = none
# IDE0022: Use block body for method
csharp_style_expression_bodied_methods = true
# IDE0032: Use auto property
dotnet_style_prefer_auto_properties = true
#CSharpier Incompatible Rules
dotnet_diagnostic.SA1009.severity = none
dotnet_diagnostic.SA1111.severity = none
dotnet_diagnostic.SA1633.severity = none
dotnet_diagnostic.SA1502.severity = none
#Don't like these
dotnet_diagnostic.SA1512.severity = none
dotnet_diagnostic.SA1623.severity = none
dotnet_diagnostic.SA1515.severity = none
dotnet_diagnostic.SA1516.severity = none
#Not sure why this is necessary
dotnet_diagnostic.CA1016.severity = none
#What's the recommendation these days?
dotnet_diagnostic.SA1309.severity = none
dotnet_diagnostic.SA1101.severity = none
#Too onerous
dotnet_diagnostic.SA1615.severity = none
dotnet_diagnostic.SA1600.severity = none
dotnet_diagnostic.SA1611.severity = none
#This is an analyzer bug when using records
dotnet_diagnostic.SA1313.severity = none
#This also seems to be a bug when using array initializers
dotnet_diagnostic.SA1010.severity = none
dotnet_diagnostic.SA1011.severity = none
# IDE0040: Add accessibility modifiers
dotnet_style_require_accessibility_modifiers = never
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<WarningsNotAsErrors></WarningsNotAsErrors>
<Deterministic>true</Deterministic>
<LangVersion>Latest</LangVersion>
<DebugSymbols>true</DebugSymbols>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>true</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzers>true</RunAnalyzers>
<Nullable>enable</Nullable>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0"/>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"/>
</ItemGroup>
</Project>
@sharwell
Copy link

sharwell commented Jan 29, 2024

Initial notes (commenting on Revision 2):

  1. Several items in Directory.Build.props here are unnecessary

    1. RunAnalyzersDuringBuild defaults to the value of RunAnalyzers, so if both are the same only the latter needs to be included. Also, the default value here is true, so both of these could be removed.
    2. RunAnalyzersDuringLiveAnalysis defaults to the value of RunAnalyzers, so if both are the same only the latter needs to be included. Also, the default value here is true, so both of these could be removed.
    3. CodeAnalysisTreatWarningsAsErrors defaults to the value of TreatWarningsAsErrors, so if the values are the same only the latter needs to be included.
  2. StyleCop.Analyzers version 1.1.118 only supports C# 7 and earlier. If you want to use newer versions of C#, you need to use a newer preview release (ideally the latest beta version).

  3. In general, lines with the syntax dotnet_diagnostic.{id}.severity should go in .globalconfig instead of .editorconfig.

  4. Setting the value dotnet_style_prefer_auto_properties = true will not enable IDE0032 for command line builds. Starting with Visual Studio 17.9 Preview 2 (dotnet/roslyn#69843), the recommended way to enable this would be to set dotnet_style_prefer_auto_properties = true:warning. For earlier versions, you'll need to have a separate line:

    dotnet_style_prefer_auto_properties = true:warning
    dotnet_diagnostic.IDE0032.severity = warning
    
  5. I don't believe there is a way to "enable all rules" for several reasons:

    1. There are many rules which conflict with each other
    2. There are rules that enforce styles which are no longer recommended
    3. There are some rules which result in catastrophic performance overhead when attempting to run on an entire solutions with certain coding styles

@MelbourneDeveloper
Copy link
Author

Hi @sharwell . Thanks for all the comments. Let's unpack these one by one.

StyleCop.Analyzers version 1.1.118 only supports C# 7 and earlier. If you want to use newer versions of C#, you need to use a newer preview release (ideally the latest beta version).

I had not used StyleCop much, and only discovered this yesterday. It's a good package because it seems to add a lot of formatting rules that have been sorely missing in the C# experience. I mostly work in Dart these days, and the formatter is complete and integrated into the IDE. Currently, I use CSharpier and I have to manually format the code before I push it.

I noticed that some of the rules are buggy, and this is probably because it doesn't support the latest version. Still, I'm not keen to install a beta version of the package. If the analyzers become too problematic with the latest version of C#, I will have to fall back on CSharpier only.

@MelbourneDeveloper
Copy link
Author

@sharwell

In general, lines with the syntax dotnet_diagnostic.{id}.severity should go in .globalconfig instead of .editorconfig.

Oh man. There's a new file format now? I didn't see this one creep in. There have been so many different formats, it's hard to keep up. I can migrate over to this new format. Can you tell me why this new format exists, and why it's better than editor config? There is documentation here, but it's not clear what the advice is.

@sharwell
Copy link

Can you tell me why this new format exists, and why it's better than editor config

It applies to all files in a build and not just the ones matching the filter (i.e. it works like a proper replacement for .ruleset files). The format is equivalent to .editorconfig aside from section headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment