Skip to content

Home for the dotnet-format command

License

Notifications You must be signed in to change notification settings

phil-allen-msft/format

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotnet-format

dotnet-format

Nuget

Branch Windows (Debug) Windows (Release) Linux (Debug) Linux (Release) Localization (Debug) Localization (Release)
main Build Status Build Status Build Status Build Status Build Status Build Status

dotnet-format is a code formatter for dotnet 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. At this time dotnet-format is able to format C# and Visual Basic projects with a subset of supported .editorconfig options.

New in v5.1.225507

New Features

  • Can now specify that format run against a solution filter with dotnet format solution.slnf
  • Can now filter diagnostics with dotnet format --fix-analyzers --diagnostics ID0001
  • Can now generate a MSBuild binary log with dotnet format --binary-log PATH
  • Can now support analyzers which update non-code files, such as the PublicApiAnalyzers

Breaking Changes

  • Implicit restore when fixing code style or 3rd party analyzers (disable with --no-restore)
  • Adopt csc style for warnings and errors
  • Warnings and errors are now written to the standard error stream

Changes

How To Use

dotnet-format is now part of the .NET 6 SDK. Invoking the dotnet format command will fix whitespace, code style, and analyzer issues by default. dotnet-format will look in the current directory for a project or solution file and use that as the workspace to format. If more than one project or solution file is present in the current directory, you will need to specify the workspace to format. You can control how verbose the output will be by using the --verbosity option.

There are also 3 subcommands to target specific scenarios:

  • dotnet format whitespace: fixes whitespace
  • dotnet format style: runs code style analyzers
  • dotnet format analyzers: runs third party analyzers

Common options for all commands

  • --no-restore: Doesn't execute an implicit restore before formatting.
  • --include-generated: Format files generated by the SDK
  • --include: A list of relative file or folder paths to include in formatting. All files are formatted if empty.
  • --exclude: A list of relative file or folder paths to exclude from formatting.
  • --verbosity: Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]
  • --report: Accepts a file path, which if provided, will produce a json report in the given directory.
  • --binarylog: Log all project or solution load information to a binary log file.
  • --verify-no-changes: Verify no formatting changes would be performed. Terminates with a non-zero exit code if any files would have been formatted.

Unique options for dotnet format

  • --diagnostics: A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues.
  • --severity: The severity of diagnostics to fix. Allowed values are info, warn, and error.

Note: if the user specifies a severity here it is used for both style and analyzers.

Unique options for dotnet format whitespace

  • --folder: Whether to treat the workspace argument as a simple folder of files.

Unique options for dotnet format style

  • --diagnostics: A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues.
  • --severity: The severity of diagnostics to fix. Allowed values are info, warn, and error.

Unique options for dotnet format analyzers

  • --diagnostics: A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues.
  • --severity: The severity of diagnostics to fix. Allowed values are info, warn, and error.

Examples

Add format after dotnet and before the command arguments that you want to run:

Examples Description
dotnet format Formats and runs analysis for the project or solution in the current directory.
dotnet format -v diag Formats and runs analysis with very verbose logging.
dotnet format <workspace> Formats and runs analysis for a specific project or solution.
dotnet format <workspace> --severity error Formats, fixes codestyle errors, and fixes 3rd party analyzer errors.
dotnet format whitespace <workspace> --folder Formats a particular folder and subfolders.
dotnet format style <workspace> Fixes only codestyle analyzer warnings.
dotnet format style <workspace> --severity error --no-restore Fixes only codestyle analyzer errors without performing an implicit restore.
dotnet format style <workspace> --diagnostics IDE0005 Fixes only codestyle analyzer warnings for the IDE0005 diagnostic.
dotnet format analyzers <workspace> --severity error Fixes only 3rd party analyzer errors.
dotnet format --include Program.cs Utility\Logging.cs Formats the files Program.cs and Utility\Logging.cs
dotnet format --verify-no-changes Formats but does not save. Returns a non-zero exit code if any files would have been changed.
dotnet format --report <report-path> Formats and saves a json report file to the given directory.
dotnet format --include test/Utilities/*.cs --folder Formats the files expanded from native shell globbing (e.g. bash). Space-separated list of files are fed to formatter in this case. Also applies to --exclude option.
dotnet format --include 'test/Utilities/*.cs' --folder With single quotes, formats the files expanded from built-in glob expansion. A single file pattern is fed to formatter, which gets expanded internally. Also applies to --exclude option.
ls tests/Utilities/*.cs | dotnet format --include - --folder Formats the list of files redirected from pipeline via standard input. Formatter will iterate over Console.In to read the list of files. Also applies to --exclude option.

How to install Development Builds

Development builds of dotnet-format are being hosted on Azure Packages. You can visit the dotnet-format Azure Packages page.

You can install the latest build of the tool using the following command.

dotnet tool install -g dotnet-format --version "7.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json

Note: After installing .NET 6 Preview 7 SDK or higher you will need to run the dotnet-format global tool by invoking dotnet-format. Invoking dotnet format will invoke the SDK's copy of dotnet-format.

How To Build From Source

You can build and package the tool using the following commands. The instructions assume that you are in the root of the repository.

build -pack
# The final line from the build will read something like
# Successfully created package '..\artifacts\packages\Debug\Shipping\dotnet-format.7.0.0-dev.nupkg'.
# Use the value that is in the form `7.0.0-dev` as the version in the next command.
dotnet tool install --add-source .\artifacts\packages\Debug\Shipping -g dotnet-format --version <version>
dotnet format

Note: On macOS and Linux, .\artifacts will need be switched to ./artifacts to accommodate for the different slash directions.

How To Uninstall

You can uninstall the tool using the following command.

dotnet tool uninstall -g dotnet-format

About

Home for the dotnet-format command

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 55.5%
  • PowerShell 25.6%
  • Shell 16.7%
  • CMake 1.9%
  • Batchfile 0.2%
  • Dockerfile 0.1%