microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.52k stars 1.55k forks source link

C_Cpp.clang_format_style doesn't work when set to "file" #12569

Closed chscott closed 2 months ago

chscott commented 2 months ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary:

Problem config:

  "C_Cpp.clang_format_fallbackStyle": "none",
  "C_Cpp.clang_format_style": "file",
  "C_Cpp.formatting": "clangFormat",
  "C_Cpp.loggingLevel": "Debug",

With this configuration, formatting a document does nothing. I see this output:

LSP: (received) cpptools/formatDocument: file:///c%3A/Temp/jsonman.c (id: 72)
LSP: (invoked) cpptools/formatDocument: file:///c%3A/Temp/jsonman.c (id: 72)
Formatting document: file:///c%3A/Temp/jsonman.c
Formatting Engine: clangFormat
c:\Users\Chad\.vscode\extensions\ms-vscode.cpptools-1.21.6-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file -fallback-style=none --Wno-error=unknown -assume-filename=C:\Temp\jsonman.c
LSP: Sending response (id: 72)

Working config:

  "C_Cpp.clang_format_fallbackStyle": "none",
  "C_Cpp.clang_format_style": "file:${workspaceFolder}/.clang-format",
  "C_Cpp.formatting": "clangFormat",
  "C_Cpp.loggingLevel": "Debug",

With this configuration, it works. I see this output:

LSP: (received) cpptools/formatDocument: file:///c%3A/Temp/jsonman.c (id: 81)
LSP: (invoked) cpptools/formatDocument: file:///c%3A/Temp/jsonman.c (id: 81)
Formatting document: file:///c%3A/Temp/jsonman.c
Formatting Engine: clangFormat
c:\Users\Chad\.vscode\extensions\ms-vscode.cpptools-1.21.6-win32-x64/bin/../LLVM/bin/clang-format.exe -style=file:C:\Users\Chad\src\lib-utils/.clang-format -fallback-style=none --Wno-error=unknown -assume-filename=C:\Temp\jsonman.c
LSP: Sending response (id: 81)
LSP: (received) textDocument/didChange: file:///c%3A/Temp/jsonman.c
LSP: (invoked) textDocument/didChange: file:///c%3A/Temp/jsonman.c
LSP: (received) cpptools/didChangeTextEditorSelection
LSP: (invoked) cpptools/didChangeTextEditorSelection
LSP: (received) textDocument/didSave: file:///c%3A/Temp/jsonman.c
LSP: (invoked) textDocument/didSave: file:///c%3A/Temp/jsonman.c

Steps to reproduce:

As above

Expected behavior:

The workspace .clang-format should be found when "C_Cpp.clang_format_style": "file".

Configuration and Logs

As above

Other Extensions

No response

Additional context

No response

sean-mcmanus commented 2 months ago

@chscott You appear to be describing "by design" behavior of clang-format -- clang-format doesn't have any knowledge about your workspace, which is a VS Code concept. It looks for the .clang-format file in the folder of the file and it's parent, so in your example it would look only in C:/temp and C:/ and not your workspace folder.