📅 November 29, 2021

dotnet format in Continuous Integration

One of my favorite dotnet CLI tools is dotnet format. dotnet format is a code formatter that applies style preferences to a project or solution. Preferences are read from an .editorconfig file, if present, otherwise a default set of preferences will be used. With the release of .NET 6, dotnet format is now part of the SDK. Previously, you needed to install dotnet format separately.

A scenario where dotnet format comes in super handy is in continuous integration. dotnet format can run in a build pipeline (preferably as a verification check) to verify that the code being committed matches the style guidelines and format of your team standards. You can utilize the --verify-no-changes option to verify that no formatting changes would be performed on files in your specified project. Below is what this task might look like in an Azure DevOps build pipeline:

- task: DotNetCoreCLI@2
  displayName: 'dotnet format --verify-no-changes'
    command: 'custom'
    custom: 'format'
    arguments: '--verify-no-changes --verbosity diagnostic'

If you put that task in a pipeline that triggers (maybe during a pull request) before code is merged into the main branch, it will catch code formatting or styling that doesn't match the team standards. This allows for more code review time to be spent on other things rather than formatting and styling concerns.

# development | dotnet | devops