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 align your code with the coding style. You can easily rename symbols and add missing spaces. However, it's easy to overlook some warnings and commit code that doesn't match the coding style. In this post, I'll show you how to enforce the coding style using a step in your CI pipeline.

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

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 into your CI pipeline to ensure every commit respects 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 comply with the coding style, the pipeline will print details and the build will fail:

If you prefer not to use the DotNetCoreCLI@2 task, you can use a script task instead:

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?