dotnet format

This article applies to: ✔️ .NET 6.x SDK and later versions

Name

dotnet format - Formats code to match editorconfig settings.

Synopsis

dotnet format [<PROJECT | SOLUTION>] [command] [options]

dotnet format -h|--help

Description

dotnet format is a code formatter that applies style preferences to a project or solution. Preferences will be read from an .editorconfig file, if present, otherwise a default set of preferences will be used. For more information, see the EditorConfig documentation.

Arguments

PROJECT | SOLUTION

The MSBuild project or solution to run code formatting on. If a project or solution file is not specified, MSBuild searches the current working directory for a file that has a file extension that ends in proj or sln, and uses that file.

Options

None of the options below are required for the dotnet format command to succeed, but you can use them to further customize what is formatted and by which rules.

  • --diagnostics <DIAGNOSTICS>

    A space-separated list of diagnostic IDs to use as a filter when fixing code style or third-party issues. Default value is whichever IDs are listed in the .editorconfig file. For a list of built-in analyzer rule IDs that you can specify, see the list of IDs for code-analysis style rules.

  • --severity

    The minimum severity of diagnostics to fix. Allowed values are info, warn, and error. The default value is warn.

  • --no-restore

    Doesn't execute an implicit restore before formatting. Default is to do implicit restore.

  • --verify-no-changes

    Verifies that no formatting changes would be performed. Terminates with a non zero exit code if any files would have been formatted.

  • --include <INCLUDE>

    A space-separated list of relative file or folder paths to include in formatting. The default is all files in the solution or project.

  • --exclude <EXCLUDE>

    A space-separated list of relative file or folder paths to exclude from formatting. The default is none.

  • --include-generated

    Formats files generated by the SDK.

  • -v|--verbosity <LEVEL>

    Sets the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. Default value is m[inimal].

  • --binarylog <BINARY-LOG-PATH>

    Logs all project or solution load information to a binary log file.

  • --report <REPORT-PATH>

    Produces a JSON report in the directory specified by <REPORT_PATH>.

  • -h|--help

    Shows help and usage information

Subcommands

Whitespace

dotnet format whitespace - Formats code to match editorconfig settings for whitespace.

Description

The dotnet format whitespace subcommand only runs formatting rules associated with whitespace formatting. For a complete list of possible formatting options that you can specify in your .editorconfig file, see the C# formatting options.

Options

  • --folder

    Treat the <PROJECT | SOLUTION> argument as a path to a simple folder of code files.

Style

dotnet format style - Formats code to match EditorConfig settings for code style.

Description

The dotnet format style subcommand only runs formatting rules associated with code style formatting. For a complete list of formatting options that you can specify in your editorconfig file, see Code style rules.

Options

  • --diagnostics <DIAGNOSTICS>

    A space-separated list of diagnostic IDs to use as a filter when fixing code style or third-party issues. Default value is whichever IDs are listed in the .editorconfig file. For a list of built-in analyzer rule IDs that you can specify, see the list of IDs for code-analysis style rules.

  • --severity

    The minimum severity of diagnostics to fix. Allowed values are info, warn, and error. The default value is warn

Analyzers

dotnet format analyzers - Formats code to match editorconfig settings for analyzers.

Description

The dotnet format analyzers subcommand only runs formatting rules associated with analyzers. For a list of analyzer rules that you can specify in your editorconfig file, see Code style rules.

Options
  • --diagnostics <DIAGNOSTICS>

    A space-separated list of diagnostic IDs to use as a filter when fixing code style or third-party issues. Default value is whichever IDs are listed in the .editorconfig file. For a list of built-in analyzer rule IDs that you can specify, see the list of IDs for code-analysis style rules.

  • --severity

    The minimum severity of diagnostics to fix. Allowed values are info, warn, and error. The default value is warn.

Examples

  • Format all code in the solution:

    dotnet format ./solution.sln
    
  • Clean up all code in the application project:

    dotnet format ./src/application.csproj
    
  • Verify that all code is correctly formatted:

    dotnet format --verify-no-changes
    
  • Clean up all code in the src and tests directory but not in src/submodule-a:

    dotnet format --include ./src/ ./tests/ --exclude ./src/submodule-a/