If "C_Cpp.clang_format_path" is set to null, or to an old 5.0 version of clang-format, Command-K Command-F works as expected, and everything is formatted as it should be.
However, more recent version of clang-format (first noticed with version clang-format version 5.0.0 (trunk) (llvm/trunk 295482))
Then Command-K Command-F does nothing.
I'm assuming it's a version issue...
Usually when formatting does nothing it's because:
I think the first item is the most prevalent and I have a code change which will output the error so you can address whatever the problem might be. But I haven't fully tested it yet so I'm not sure if it will make it into the March release or not at this point.
Would you have a suggestion to test what arguments are passed to clang-format so that I can better test and report the problem?
I have the same problem.
The same here with
clang-format version 6.0.0-svn326550-1~exp1~20180305180131.61 (branches/release_60)
Formatting doesn't work, there's no error message or any other feedback.
I am also unable to use document formatting with the extension when C_Cpp.clang_format_path is null. I could not find any errors logged anywhere when running Format Document.
The issue appears to be with the included clang-format.exe file on Windows, at least from what I can tell. I tried running it directly from C:\Users\{user}\.vscode\extensions\ms-vscode.cpptools-0.15.0\LLVM\bin, but got the following errors:
The code execution cannot proceed because VCRUNTIME140.dll was not found. Reinstalling the program may fix this problem.The code execution cannot proceed because MSVCP140.dll was not found. Reinstalling the program may fix this problem.I tried copying MSVCP140.dll and VCRUNTIME140.dll from C:\Windows\System32 into the same directory as clang-format.exe but it resulted in this error instead: The application was unable to start correctly (0xc000007b). Click OK to close the application.
I am able to get code formatting to work by installing LLVM separately and setting C_Cpp.clang_format_path to the external install's location. It's much easier to set up the extension when the included clang format worked though, so hopefully this can be fixed!
@danbsite The executable is the same as is shipped from http://releases.llvm.org/5.0.0/LLVM-5.0.0-win32.exe , although we do a Microsoft signing which alters the binary slightly. I've double checked and the binary appears to be the correct 32-bit binary so I don't know why you'd be having a problem unless your 32-bit Windows installation is corrupt. The clang-format we ship is 32-bit, so you should be copying the files from SysWOW64 and not System32, assuming your OS is 64-bit.
@sean-mcmanus I just did a fresh install of Windows when I encountered this issue, but I will try again. Thank you for the information!
I was able to fix the issue for myself by installing the x86 version of this: https://www.microsoft.com/en-us/download/details.aspx?id=53587
I had tried this earlier but used the wrong installer so it didn't work, as I didn't realize the included clang-format.exe was 32 bit. Thank you again @sean-mcmanus!
I find it strange that I needed to install this manually as I previously did not need to, but maybe in my old Windows setup I just already had a version installed from another program.
I'm glad that you was able to fix it.
On Ubuntu though it still silently fails without any error message.
@mgr-inz-rafal Do you have the 0.16.0-insiders released March 9th? The March 8th version didn't have the additional error messages: https://github.com/Microsoft/vscode-cpptools/releases/tag/v0.16.0-insiders .
@sean-mcmanus No, I currently have Version 0.15.0: February 15, 2018 installed.
Will get back to you after an update.
I also don't have clang-format working without any error message, this is on macOS:
$ clang-format -version
clang-format version 6.0.0 (tags/RELEASE_600/final)
VSCode Version 1.21.1 (1.21.1), macOS 13.3.3.
I have both C_Cpp.clang_format_style and C_Cpp.clang_format_path set.
@davydden We're planning to release 0.16.0 today, which should show the errors that clang-format is generating (you could also try the 0.16.0-insiders.
@sean-mcmanus
We're planning to release 0.16.0 today, which should show the errors that clang-format is generating (you could also try the 0.16.0-insiders.
thanks for the prompt reply, I will wait for the release and post back here tomorrow or so...
p.s. I indeed have 0.15.0 installed, forgot to mention this in the post above.
@sean-mcmanus i tried 0.16.0, Format selection indeed showed something like
Format failed, see output window for details
but output window was clean.
Subsequent calls produces neither errors nor output logs.
@sean-mcmanus
nevermind, I found logs in GUI (output tab and then C/C++), was not aware of this.
So the error is
Formatting failed:
/Users/<path-to>/clang-format -style=/users/<path-to>/.clang-format -fallback-style=LLVM -assume-filename=<path-to>/file.cc
Invalid value for -style
whereas C_Cpp.clang_format_style starts with the capital U:
"C_Cpp.clang_format_style":"/Users/..."
so perhaps that's the reason.
@davydden We don't accept style being a path. I don't think that's valid, right? Did clang-format 6.0 change to allow this? We do a lowercase operation on the style unless it starts with a {. We could change this to check for other conditions if they are valid. I've only seen .clang-format files being retrieved from the file system (current directory or parent) using the "file" option.
UPDATE: Yeah, your input to style appears to be incorrect according to http://clang.llvm.org/docs/ClangFormat.html (unless they haven't updated the docs yet to reflect this new input option).
@sean-mcmanus frankly, I am not sure how to interpret what clang 7.0 says
clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.
When using -style=file, clang-format for each input file will try to find the .clang-format file located in the closest parent directory of the input file. When the standard input is used, the search is started from the current directory.
I would say file is a path to .clang_format.
ah, it's not a path, it's really just a key... damn. Ok, i guess i was just using this wrong...
that's what help says:
-style=
- Coding style, currently supports:
LLVM, Google, Chromium, Mozilla, WebKit.
Use -style=file to load style configuration from
.clang-format file located in one of the parent
directories of the source file (or current
directory for stdin).
Use -style="{key: value, ...}" to set specific
parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 8}"
EDIT: it worked perfectly fine by picking up .clang-format from the project root. Sorry the noise!
Yeah, I understand how the docs could lead to that confusion -- I remember at least one other user hitting this issue as well. We could add it to our backlog to check for this type of error and warn or correct it.
Thanks for confirming. For the rest of the users on this issue, can you please try out 0.16.0 and look at the "C/C++" log channel of the Output window when your formatting fails? If you are unable to resolve your clang-format issues with the logging message, please open a new issue and we can help you resolve it.
I am going to close this issue now.
Hi!
I see the issue is closed, but I promised you feedback, so here it is.
After updating extension to 0.16.1 I can see debug info in the console:
Formatting failed:
/usr/bin -style={ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0 } -fallback-style=LLVM -sort-includes=0 -assume-filename=/home/rafal/tmp/test.cc
That clearly indicates problem with my "C_Cpp.clang_format_path" config, which is now solved.
Thanks!
Formatting works perfectly.
Most helpful comment
I'm glad that you was able to fix it.
On Ubuntu though it still silently fails without any error message.