/commands/dotnet-cli/dotnet-format
dotnet format --verify-no-changes: format C# code and check it in CI
- tool
- .NET CLI
- versions
- .NET 6+, .NET 10
- os
- linux, macos, windows
dotnet format rewrites the C# (and Visual Basic) files of a project or solution to follow the rules in .editorconfig: whitespace and indentation, code style preferences and fixable analyzer warnings. Add --verify-no-changes to change nothing and exit with code 2 when a file is not formatted, which is the check to run in CI. dotnet format whitespace runs only the fast whitespace pass.
Check, fix, check again
dotnet new console -o App > /dev/null
cd App
printf 'class Program\n{\nstatic void Main() { System.Console.WriteLine("hi");}\n}\n' > Program.cs
dotnet format whitespace --verify-no-changes > /dev/null 2>&1
echo "verify exit code: $?"
dotnet format whitespace
dotnet format whitespace --verify-no-changes
echo "verify exit code: $?"
cat Program.csOutput
verify exit code: 2
verify exit code: 0
class Program
{
static void Main() { System.Console.WriteLine("hi"); }
}The badly indented file made --verify-no-changes fail with exit code 2; its messages name each file and line, such as Fix whitespace formatting. After dotnet format whitespace rewrote the file, the same check passed with exit code 0.
Subcommands and options
| Command | What it does |
|---|---|
| dotnet format | Run whitespace, style and analyzer fixes on the project or solution in the folder |
| dotnet format whitespace | Only indentation, spacing and line endings (fast, no build) |
| dotnet format style | Code style rules (IDExxxx), such as var usage or braces |
| dotnet format analyzers | Fixes from analyzers referenced by the project |
| dotnet format --verify-no-changes | Change nothing; exit code 2 if anything would change |
| dotnet format --include src/Api/ | Only these files or folders (--exclude for the opposite) |
| dotnet format --severity warn | Also fix style and analyzer issues reported at warning level, not only errors |
| dotnet format Shop.slnx | Pick the project or solution when the folder has several |
| dotnet format --report out/ | Write a JSON report of what was changed or found |
Pitfalls
- Exit codes.
0means nothing to change (or everything fixed) and2means--verify-no-changesfound files to format; other failures, such as a project that does not load, also exit non-zero. A CI script should treat any non-zero code as a failure. - Several projects in one folder. Like other
dotnetcommands,dotnet formatwithout a path fails with MSB1011; pass the solution or project file. - Style and analyzer passes need a restore. Without restored packages they cannot load the project; run
dotnet restorefirst, or use--no-restoreonly when CI already restored. - Line endings. On Windows with Git's
core.autocrlf, files can switch between CRLF and LF and appear unformatted. Setend_of_linein.editorconfigand a matching.gitattributes.
How to confirm it worked
dotnet format --verify-no-changes exiting with 0 confirms that every file follows the rules; run it in the same CI job as the build. git diff after dotnet format shows exactly what it changed. JavaScript projects have the same check pattern with deno fmt --check or Prettier.
FAQ
Do I need to install dotnet-format?
No. Since the .NET 6 SDK, dotnet format is part of the SDK. The old global tool of the same name is only needed for .NET Core 3.1 and .NET 5 SDKs.
Where does dotnet format take its rules from?
From .editorconfig files in the project folders and their parents: indentation, line endings, using order, and the severity of code style (IDExxxx) and analyzer rules. Without an .editorconfig it applies the default .NET conventions.
Why does dotnet format take so long?
The full command loads the project with MSBuild and runs whitespace, style and analyzer passes. dotnet format whitespace is much faster because it does not need to build semantic information; use it in pre-commit hooks.