A I
A I

Reputation: 381

clang-format file not working in Visual Studio code

I have put a .clang-format file in my workspace. But that formatting is not applied on my code. I have checked the settings and they are set to None for fallback-style and file for clang_format_style.

The good news is that the fallback Style doesn't work anymore. Which means when I try to Format the Document or Selection nothing happens.

This is what I did to make my custom formatting:

visualstudio.com: Edit C++ in Visual Studio Code

But there is something missing and the formatting is no happening. This is what my .clang-format in my workspace looks like:

{ 
UseTab: 14, 
IndentWidth: 4,
 BreakBeforeBraces: BS_Attach,
 AllowShortIfStatementsOnASingleLine: false,
 IndentCaseLabels: false,
 ColumnLimit: 0 
}

Upvotes: 13

Views: 30717

Answers (6)

chaosink
chaosink

Reputation: 1483

In my case, running clang-format in command line shows no error but it still doesn't work in VSCode.

Finnaly, I found the issue is due to duplicated lines in .clang-format, like the following:

...
AllowShortFunctionsOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
...

Two lines of AllowShortFunctionsOnASingleLine.

Upvotes: 0

Mike Repass
Mike Repass

Reputation: 6982

OMG! This is so frustrating. But at the cost of one Saturday morning, I'm confident I understand the root cause (as hinted by Henrique Bucher in another answer here, thank you Henrique!).

The way VS Code invokes clang-format has a "graceful" failover: if your .clang-format contains unrecognized settings, then it falls back to an entirely different configuration. But it does this SILENTLY!

So what happens:

  1. Your project has a perfectly functional .clang-format and you are happily coding along.
  2. You add something new to .clang-format that is actually invalid (perhaps because it was hallucinated by Github Copilot).
  3. As a result, clang-format now silently fails with your custom file and instead falls back to a completely different configuration (probably BasedOnStyle: Visual Studio or something). But there is no indication this is happening.
  4. Depending on how close your settings are to the fallback, you might not notice this at the time especially if you are only changing/formatting a very small number of lines.
  5. Then when you try to make another change or debug why your formatting suddenly doesn't work, you waste hours of your life.
  6. This is trivial to repro, just open .clang-format and add some gibberish "asdf" to the top of the file and then (in VS Code) format various chunks of code, the formatting with change drastically.

Note that via command line invocation, clang-format does NOT work this way. It fails immediately and verbosely.

So if you are trying to debug why your .clang-format apparently does not apply in VS Code (especially if it did previously), be sure to hop into the Terminal and try a manual invocation.

Copilot giveth, and Copilot taketh away :-(

Upvotes: 5

adam3141
adam3141

Reputation: 33

TL;DR - If the encoding of .clang-format is UTF-16, convert or generate in UTF-8 to solve issue.

I hope this might be helpful for some people.

I usually generate my .clang-format file by using the formatter to dump a styled configuration for me, then I edit the options I need. ie. clang-format.exe --style=Microsoft --dump-config > .clang-format

When trying to format, I kept on getting the Formatting failed - see output window. The output window didn't show anything useful. Entering this on the command line just seemed to want to take input from stdin.

It turns out that the Windows version of clang-format.exe when dumping a configuration is redirected to a file, Windows will dutifully save it in UTF-16.

There is probably some setting somewhere in windows to save in UTF-8 but I fixed this by generating the file using a Linux version of clang-format and then copying to where I needed it in Windows.

Upvotes: 2

Henrique Bucher
Henrique Bucher

Reputation: 4474

Just for documentation if anybody else comes through this issue, I have suffered this issue but due to a different cause. I added a newer option to my .clang-format file that was not supported by the version of clang-format I have installed on my system. It ends up it was failing silently and the symptom was that the file was not being formatted.

So make sure you know which clang-format you are using, the full path, and use that to format one of your files manually, to see if there is any error message being thrown.

Upvotes: 2

cmannett85
cmannett85

Reputation: 22366

I've just suffered from this issue, but it appears to be a minor bug in VS Code. In my settings I had Editor: Format On Save turned on, C_Cpp: Clang_format_style set to file, and C_Cpp: Formatting set to Default (which is stated as clang-format). But nothing happened on save.

To 'fix' it, I forcibly triggered a format by selecting some code and clicking Format Selection which then gave me a dialog whining about there being multiple formatters available and that I have to pick one from the command palette. It looks like the builtin MS Intellisense one and clang-format were fighting for dominance - despite the fact that clang-format was listed as default in the settings! After selecting clang-format here, it was all fine again.

Upvotes: 17

A I
A I

Reputation: 381

For some reason the following options in Formatting

Format on Paste

Format on Save

Format on Save Mode: file

have to be checked. Now it works fine. Also look into the settings.json file made by the App. Double check your settings and then try again.

Upvotes: 0

Related Questions