dotnet-format(1)

dotnet format

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

NAME

dotnet-format - Formats code to match editorconfig settings.

SYNOPSIS

    dotnet format [<PROJECT | SOLUTION>]
      [--binarylog <BINARY-LOG-PATH>] [--diagnostics <DIAGNOSTICS>]
      [--exclude <EXCLUDE>] [--exclude-diagnostics <EXCLUDE-DIAGNOSTICS>]
      [--include <INCLUDE>] [--include-generated]
      [--no-restore] [--report <REPORT-PATH>] [--severity <LEVEL>]
      [-v|--verbosity <LEVEL>] [--verify-no-changes] [--version]

    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.

•   --binarylog <BINARY-LOG-PATH>

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

•   --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.

•   --exclude <EXCLUDE>

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

•   --exclude-diagnostics <EXCLUDE-DIAGNOSTICS>

    A space-separated list of diagnostic IDs to exclude when fixing code style or third-party issues. Default value is none. For a list of built-in analyzer rule IDs that you can specify, see the list of IDs for code-analysis style rules.

•   --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.

•   --include-generated

    Formats files generated by the SDK.

•   --no-restore

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

•   --report <REPORT-PATH>

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

•   --severity <LEVEL>

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

•   --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.

•   --version

    Displays version information.

•   -v|--verbosity <LEVEL>

    Sets the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. For more information, see <xref:Microsoft.Build.Framework.LoggerVerbosity>.

•   -?|-h|--help

    Prints out a description of how to use the command.

  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 <LEVEL>

    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 <LEVEL>

    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