Setting up C# editorconfig Code Cleanup on build/save and commit
Asked Answered
S

2

19

Our team want to enforce styling rules in our C# project. I read somewhere some time that Microsoft said that ".editorconfig is the future" so we want to use this. NOTE: We don't want to use ReSharper.

C# has a lot of great rules that can be defined in editorconfig now, see Microsofts own editorconfig guide

We want to use this, and enforce that the rules set in the editor config is followed both when coding in Visual Studio and enforce that the code commited to git is following the rules.

When adding the .editorconfig rules, we get great linting on our files like this:

use var warning

Running a fully enabled "Code Cleanup" in Visual Studio 2019 it completely formats our code as desired:

run code cleanup

Question 1: How can we make the "Run Code Cleanup" run automatically on save/build? Even if we set certain rules as severity ":error" the compiler still don't complain about issues in C# files on build.

NOTE: I have tried the plugin for Visual Studio called Format document on Save but it does not follow all the rules set in the editorconfig (only a few, like fixing tabs/spaces and end of file newline)

We would also like to make sure that all commits to our git repository gets formated.

There is a tool called dotnet-format that is supposed to format the code according to the editorconfig rules.

We would like to add a pre-commit hook that runs the following 2 commands:

dotnet tool install -g dotnet-format
dotnet-format

This would work fine, but the issue is that dotnet-format also don't fix the issues in files with code giving severity ":error".

dotnet-format behaves the same way "Format document on Save" does, only fixing a few things like tabs/spaces and end of file newline.

EDIT: dotnet-format appearently only supports a few of the rules for now as per their Wiki

Question 2: How can we, from a command line, run a command behaving the same way as the "Code Cleanup" command in Visual Studio 2019 does?

Saito answered 29/3, 2019 at 9:2 Comment(2)
Welcome to SO! Please, even if there is any relation between questions, try to not to ask more than one question at once. In this case, there is no direct relationship, so please, split in 2 different questions.Gualtiero
if you are interested on how to run the cleanup on builds: medium.com/c-sharp-progarmming/…Randers
I
5

I am able just to answer the your first question:

I have been looking for this and I found a extension called Code Cleanup on Save, you will just have to install it and configure it at Tool->Options->Code Clean up on Save, decide what of your profiles you want to set and that's all, I hope it will help you!

Iguana answered 21/10, 2019 at 9:23 Comment(2)
Welcome to SO! It is a problem of the original question (there should be just one), already noted.Gualtiero
Similar extension FormatDocumentOnSave github.com/Elders/VSE-FormatDocumentOnSave seems more popular and have more recent updatesPry
C
2

Because CageE's answer is behind a signin-wall:

.NET code style analysis is disabled by default, on build for all .NET projects. You can enable code style analysis for .NET projects by setting the EnforceCodeStyleInBuild property to true.

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Cooperstein answered 22/7, 2023 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.