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 and static analysis recommendations 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 issues. Default value is whichever IDs are listed in the .editorconfig file. For a list of built-in code style 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 (excluding code style rules).

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 Quality rules.

Options
  • --diagnostics <DIAGNOSTICS>

    A space-separated list of diagnostic IDs to use as a filter when fixing non code style 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 quality rules. For third-party analyzers refer to their documentation.

  • --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/
    
  • Fix a specific code style issue:

    dotnet format style --diagnostics IDE0005 --severity info
    
  • Fix all code style issues that have severity info, warning or error:

    dotnet format style --severity info
    
  • Fix a specific (non code style) analyzer issue:

    dotnet format analyzers --diagnostics CA1831 --severity warn
    
  • Fix all non code style issues that have severity info, warning or error:

    dotnet format analyzers --severity info