How to enforce .NET format using EditorConfig and GitHub Actions
Principal author: Xiaodi Yan
Coding style is a personal preference. However, it is important to have a consistent coding style across the team. Automating the code formatting process can help us reduce the time spent on code reviews and improve the code quality. In this how-to article, you will learn how to enforce your .NET code formatting using EditorConfig and GitHub Actions.
We will look into:
- How to create an EditorConfig file
- How to format your code locally
- How to config GitHub Actions to check the code formatting
Prerequisites
To complete this how-to guide, you should have the following prerequisites installed and configured:
- .NET 6 SDK or later
- Visual Studio 2022
- A .NET project in GitHub repository
1 - Creating an EditorConfig file
An EditorConfig file defines and maintains consistent coding styles regardless of which editor or IDE you use. It is a text file that contains a collection of settings that define how to format code. EditorConfig supports various programming languages and IDEs. Defining an EditorConfig file in the code repository can help us enforce the coding style for developers working on the same project.
For more information about EditorConfig, see EditorConfig.
To create an EditorConfig file, you have a few options:
Option 1: Creating an EditorConfig file in Options dialog
Open your .NET project/solution in Visual Studio 2022. Click Tools > Options > Text Editor > C# > Code Style > General. Then click Generate .editorconfig file from settings.
Choose the root directory of your project/solution and name the file
.editorconfig
. Click Save.
Option 2: Creating an EditorConfig file in Solution Explorer
Open your .NET project/solution in Visual Studio 2022. Right-click the project/solution in Solution Explorer and click Add > New Item.
In the search box, type
editorconfig
. You will see two templates as follows:Select the èditorconfig File (.NET) template and rename it to
.editorconfig
. Click Add. This will create an EditorConfig file in the root directory of your project/solution with default .NET code style settings.
Option 3: Creating an EditorConfig file using the dotnet CLI
Open a command prompt and navigate to the root directory of your project/solution.
Run the following command to create an EditorConfig file:
dotnet new editorconfig
This will create an EditorConfig file in the root directory of your project/solution with default .NET code style settings.
Note
The .editorconfig
file will be applied to all the files in the same directory and its subdirectories. You can override the settings in a subdirectory by creating another .editorconfig
file in that subdirectory. However, it is recommended to have only one .editorconfig
file in the root directory of your project/solution to avoid confusion unless you have a good reason to do so.
2 - Editing the EditorConfig file
You can edit the .editorconfig
file in Visual Studio 2022 or any text editor. It is recommended to use Visual Studio 2022 to edit the file as it provides a better user experience.
Open the
.editorconfig
file in Visual Studio 2022. You can see the default settings for the code style.Edit the code style settings as needed. Note that different teams may have different code style settings. Discuss with your team members and agree on a set of code style settings before editing the file.
3 - Formatting your code locally
You have a couple of options to format your code locally.
Enforcing code style on save
Click Tools > Options > Text Editor > Code Cleanup in Visual Studio 2022. Select the Run Code Cleanup profile on Save checkbox.
Click Configure Code Cleanup. You can choose what fixes to run on save. Click OK.
Open a C# file in your project/solution. Make some changes to the code and save the file. The code will be formatted automatically. You can also press
Ctrl + K, Ctrl + E
to format the code manually.
Enforcing code style on build
Open the .csproj
file of your project/solution in Visual Studio 2022. Add the following code to the file:
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
This setting enables code style enforcement on build. Any code style violations will be reported as build warnings or errors according to the .editorconfig
file.
Enforcing code style using dotnet format command
You can also manually run the dotnet format
command to format your code. Here are some examples:
Format all the .NET code files in the root directory of your project/solution and its subdirectories:
dotnet format
Verify the code formatting without making any changes:
dotnet format --verify-no-changes
Specify the path to the project/solution file when formatting the code:
dotnet format ./SampleWebApi/SampleWebApi.sln
Report detailed information:
dotnet format --verbosity diagnostic
You can find more information about the dotnet format
command here.
4 - Configuring GitHub Actions to check the code formatting
GitHub Actions is a CI/CD tool that allows you to automate your software development workflows. You can use GitHub Actions to check the code formatting in your pull requests. This can help us ensure that all the code changes are formatted correctly before merging to the main branch.
Go to your GitHub repository of the .NET project. Click Actions.
In the Continuous integration category, click Configure for .NET.
Rename the workflow name as
dotnet-format.yml
or any name you like.Edit the YAML file as follows. Please update the content per your project/solution. You may need to update the SDK version and the path to the project/solution file.
name: .NET format on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: dotnet-format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore - name: Format run: dotnet format --verify-no-changes --verbosity diagnostic
In the YAML file, we added a job called
dotnet-format
that checks the code formatting. The commanddotnet format --verify-no-changes --verbosity diagnostic
will check the code formatting and report any violations. If there are any violations, the workflow will fail and the pull request will not be merged to the main branch.Click Commit changes to save the changes. If your main branch is protected, you will need to create a pull request and merge it to the main branch.
Check the status of the workflow when a pull request is created. Click Details to see the details of the workflow.
Next steps
In this how-to article, you learned how to enforce your .NET code formatting using EditorConfig and GitHub Actions. To learn more about .NET code styles, please check out the following resources:
- .NET Code style preferences
- Create portable, custom editor settings with EditorConfig
- .NET Code-style rule options
- .NET Code-style rules
- Overview of .NET source code analysis
- GitHub Actions documentation
If you use Azure DevOps Pipelines, you can implement the same workflow using Azure DevOps Pipelines. For more information, see Build, test, and deploy .NET Core apps using Azure DevOps Pipelines.
Community Content