dotnet format: Enforce Code Style in CI
dotnet format formats code to match .editorconfig and analyzer rules, or verifies that it already matches without writing changes.
In CI you want format to fail the build when code is not formatted, not to silently rewrite it. --verify-no-changes is the gate.
What it does
dotnet format runs whitespace, code-style, and analyzer formatters over a project or solution, applying fixes based on .editorconfig. With --verify-no-changes it makes no edits and exits non-zero if anything would change, which is what a CI check wants.
Common usage
dotnet format
dotnet format --verify-no-changes
dotnet format whitespace --verify-no-changes
dotnet format --severity warn --verify-no-changesOptions
| Flag | What it does |
|---|---|
| --verify-no-changes | Make no changes; exit non-zero if any are needed |
| --severity <level> | Minimum diagnostic severity to fix: info, warn, error |
| --include <paths> | Restrict to specific files or folders |
| whitespace / style / analyzers | Run only that subcommand category |
| --no-restore | Skip the implicit restore |
In CI
Run dotnet format --verify-no-changes as a dedicated check step so unformatted code fails instead of being auto-committed. Match the --severity to your .editorconfig so the local and CI behavior agree. As of recent SDKs dotnet format ships in the SDK, so no separate tool install is needed.
Common errors in CI
A non-zero exit with "Formatted code file 'X'" lines under --verify-no-changes simply means the file is not formatted; run dotnet format locally and commit. NETSDK1004 ("Assets file not found") means restore has not run; drop --no-restore or restore first. An older standalone dotnet-format tool conflicting with the SDK built-in can cause "command not found" or version mismatches.