Skip to content

dotnet-format

GitHub stars formatter GitHub release (latest SemVer) GitHub last commit GitHub commit activity GitHub contributors

dotnet format is the official .NET code formatter that automatically applies consistent code styling and formatting rules to C# and VB.NET projects. It integrates directly with the .NET SDK and uses EditorConfig settings and analyzer rules to ensure uniform code appearance.

Key Features:

  • Official .NET Tool: Native integration with .NET SDK and MSBuild for seamless project integration
  • EditorConfig Support: Respects .editorconfig files for consistent formatting across teams and editors
  • Analyzer Integration: Works with .NET analyzers and code style rules for comprehensive formatting
  • Solution/Project Scope: Can format entire solutions, specific projects, or individual files with precise control
  • Incremental Formatting: Only formats changed files or specific file patterns to optimize performance
  • Multiple Rule Categories: Handles whitespace, style preferences, and third-party analyzer rules
  • Verification Mode: Verify mode ensures code is properly formatted without making changes

Note: dotnet-format requires a .sln or .csproj file to run correctly.

dotnet-format documentation

sdk - GitHub

Configuration in MegaLinter

Variable Description Default value
CSHARP_DOTNET_FORMAT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
CSHARP_DOTNET_FORMAT_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
Ex: -s --foo "bar"
CSHARP_DOTNET_FORMAT_CLI_LINT_MODE Override default CLI lint mode
⚠️ As default value is project, overriding might not work
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
project
CSHARP_DOTNET_FORMAT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".cs"]
CSHARP_DOTNET_FORMAT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
CSHARP_DOTNET_FORMAT_PRE_COMMANDS List of bash commands to run before the linter None
CSHARP_DOTNET_FORMAT_POST_COMMANDS List of bash commands to run after the linter None
CSHARP_DOTNET_FORMAT_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling CSHARP_DOTNET_FORMAT and its pre/post commands None
CSHARP_DOTNET_FORMAT_DISABLE_ERRORS Run linter but consider errors as warnings true
CSHARP_DOTNET_FORMAT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
CSHARP_DOTNET_FORMAT_CLI_EXECUTABLE Override CLI executable ['dotnet']

MegaLinter Flavors

This linter is available in the following flavors

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 127 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 65 Docker Image Size (tag) Docker Pulls
dotnetweb Optimized for C, C++, C# or VB based projects with JS/TS 74 Docker Image Size (tag) Docker Pulls
formatters Contains only formatters 18 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .cs

How the linting is performed

dotnet-format is called once on the whole project directory (project CLI lint mode)

  • filtering can not be done using MegaLinter configuration variables,it must be done using dotnet-format configuration or ignore file (if existing)
  • VALIDATE_ALL_CODEBASE: false doesn't make dotnet-format analyze only updated files

Example calls

dotnet format --verify-no-changes
dotnet format Solution.sln --verify-no-changes
dotnet format Folder/Solution.sln --verify-no-changes
dotnet format Project.csproj --verify-no-changes
dotnet format

Help content

Description:
  Formats code to match editorconfig settings.

Usage:
  dotnet-format [<PROJECT | SOLUTION>] [command] [options]

Arguments:
  <PROJECT | SOLUTION>  The project or solution file to operate on. If a file is not specified, the command will search the current directory for one. [default: /]

Options:
  -?, -h, --help                                                           Show help and usage information
  --version                                                                Show version information
  --diagnostics                                                            A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues. []
  --exclude-diagnostics                                                    A space separated list of diagnostic ids to ignore when fixing code style or 3rd party issues. []
  --severity <error|info|warn>                                             The severity of diagnostics to fix. Allowed values are info, warn, and error.
  --no-restore                                                             Doesn't execute an implicit restore before formatting.
  --verify-no-changes                                                      Verify no formatting changes would be performed. Terminates with a non-zero exit code if any files would have been formatted.
  --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. []
  --include-generated                                                      Format files generated by the SDK.
  -v, --verbosity <d|detailed|diag|diagnostic|m|minimal|n|normal|q|quiet>  Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]
  --binarylog <binary-log-path>                                            Log all project or solution load information to a binary log file.
  --report <report-path>                                                   Accepts a file path which if provided will produce a json report in the given directory.

Commands:
  whitespace <PROJECT | SOLUTION>  Run whitespace formatting. [default: /]
  style <PROJECT | SOLUTION>       Run code style analyzers and apply fixes. [default: /]
  analyzers <PROJECT | SOLUTION>   Run 3rd party analyzers and apply fixes. [default: /]

Installation on mega-linter Docker image