Enforce .NET code style in CI with dotnet format

 
 
  • Gérald Barré

This post is part of the series 'Coding style'. Be sure to check out the rest of the blog posts of the series!

In a previous post, I wrote about using an .editorfile to define the coding style. Visual Studio understands this file and provides quick fixes to make your code match the coding style. You can easily rename symbols and add spaces where it's needed. However, it's easy to miss some warnings and commit a code that doesn't match the coding style. In this post, I'll show you how to enforce the coding style by using a step in the CI pipeline.

dotnet format allows to format the files in a solution as shown in the previous post. But it can also check that your code matches the coding style defined in the file .editorconfig. You can use --check to prevent the tool from actually writing files and to make it return a non-zero exit code when a file doesn't match the coding style.

Shell
dotnet tool update -g dotnet-format
dotnet format --check

dotnet format returns a non-zero value when the code doesn't respect the coding style

You can integrate dotnet format in your CI pipeline to ensure every commit respect the coding style.

#GitHub Actions

YAML
name: lint
on:
  push:
    branches:
      - '*'

env:
  DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
  DOTNET_NOLOGO: true

defaults:
  run:
    shell: pwsh

jobs:
  lint_csharp:
    runs-on: windows-2019
    steps:
    - uses: actions/checkout@v2

    - name: Setup .NET Core
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: '5.0.x' # SDK Version to use; x will use the latest version of the 5.0 channel

    # Format the output of dotnet format
    - name: Add dotnet-format problem matcher
      uses: xt0rted/dotnet-format-problem-matcher@v1

    # Install dotnet format as a global tool
    - name: Install dotnet format
      run: dotnet tool update --global dotnet-format

    # Run dotnet format --dry-run --check
    # By default, the task ensure the exit code is 0
    # If a file needs to be edited by dotnet format, the exit code will be a non-zero value
    # So the task will fail
    - name: Run dotnet format
      run: dotnet format --check --verbosity diagnostic

#Azure Pipelines

YAML
stages:
- stage: build
  jobs:
  - job: "lint"
    pool:
      vmImage: 'ubuntu-latest'

    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '5.0.x' # SDK Version to use; x will use the latest version of the 5.0 channel

    # Install dotnet format as a global tool
    - task: DotNetCoreCLI@2
      displayName: 'Install dotnet format'
      inputs:
        command: 'custom'
        custom: 'tool'
        arguments: 'update -g dotnet-format'

    # Run dotnet format --dry-run --check
    # By default, the task ensure the exit code is 0
    # If a file needs to be edited by dotnet format, the exit code will be a non-zero value
    # So the task will fail
    - task: DotNetCoreCLI@2
      displayName: 'Lint dotnet'
      inputs:
        command: 'custom'
        custom: 'format'
        arguments: '--check --verbosity diagnostic'

If a file doesn't respect the coding style, this will print details and the build will fail:

If you don't want to use the DotNetCoreCLI@2 task, you can use the script task

YAML
stages:
- stage: build
  jobs:
  - job: "lint"
    pool:
      vmImage: 'ubuntu-latest'

    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '5.0.x'

    - script: 'dotnet tool update -g dotnet-format && dotnet format --check --verbosity diagnostic'
      displayName: 'Lint dotnet'

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub