15

I just discovered dotnet-format, but as far as I understand it's a command line tool that has to be called manually. How can I apply dotnet-format on saving a file in Visual Studio 2019? (not Visual Studio Code!)

2
  • See Format document on Save extension Commented Apr 4, 2021 at 12:57
  • @AlexanderPetrov ah thanks, got it that dotnet-format is simply the command line tool for the built-in Visual Studio formatting. Thanks! Commented Apr 4, 2021 at 13:18

4 Answers 4

1

I've gathered all the instructions in one place for multiple IDEs, including Visual Studio, to make them easier to find and use

Visual Studio

  1. Install WillFuqua.RunOnSave extension
  2. Create .onsaveconfig file in the root folder in your project
[*.cs]
command = dotnet
arguments = format "{solution_directory}\YourProjectName.sln" --include {filename} --verbosity diagnostic
  1. Replace YourProjectName.sln with your actual solution file name ({solution_directory} and {filename} remain unchanged)
  2. (Optional) Delete --verbosity diagnostic if you don’t need detailed logs (see docs)
  3. (Optional) Add more options for RunOnSave extension, for example "always_run = true" (see docs)
  4. Restart Visual Studio
  5. Change any *.cs file (especially when always_run is disabled) and save it, then wait a few seconds or minutes

The formatting should be applied automatically.


If the instructions above don't work, try the following troubleshooting steps:

  • Run dotnet format in your project. If the command fails, the issue is likely with the tool itself (e.g., not installed, incorrect .editorconfig configuration, or another setup problem)
  • Go to "View" -> "Output" -> "Run On Save" (from the dropdown menu) and look at the output. A correct output should look like this:

How to open Run on Save

Special thanks to waf for this extension!

VS Code

  1. Install emeraldwalk.RunOnSave extension
  2. Create .vscode/settings.json file in the root folder in your project
{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.cs$",
        "cmd": "dotnet format ./YourProjectName.sln --include ${relativeFile} --verbosity diagnostic"
      }
    ]
  }
}
  1. Replace ./YourProjectName.sln with your actual solution file name (everything else remains unchanged)

Rider

This article offers a solid guide to setting up formatting (see the Dotnet Format section). However, some parts are slightly outdated. The main difference lies in the arguments:

  • Program: dotnet
  • Arguments: format $SolutionPath$ --include $FileName$ --verbosity diagnostic

Everything else remains the same

Sign up to request clarification or add additional context in comments.

Comments

0

Although this is not what the question was about, I have dotnet-format setup to run as a pre-commit hook.

#!/bin/sh
dotnet-format
exit 0

For the on-save formatting and other gimmicks, I use CodeMaid. https://marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid

2 Comments

My only issue with this approach is that the developer has to install the pre-hook library for this to be triggered. I'm looking for a native solution that works without installing extra tools. Maybe at Build time as a BuildTask, or even using git itself without external dependencies.
If I clone some random repository, git is not going to execute anything, since that would be a security risk. If I want git to run something, I have to opt-in.
0

You can configure your project to run this command automatically before build. But it will consume build time

<Project>
    <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec Command="dotnet format --severity warn --verbosity diagnostic" />
</Target>

Comments

0

You can add this to your csproj

<PropertyGroup>
  <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>

This setting enables code style enforcement on build. Any code style violations will be reported as build warnings or errors according to the .editorconfig file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.