Skip to content

clang-format autoformatting not working properly when using a .clang-format file #12680

@thernstig

Description

@thernstig

Environment

  • OS and Version: Windows 11 23H2
  • VS Code Version: 1.92.2
  • C/C++ Extension Version: v1.21.6
  • If using SSH remote, specify OS of remote machine: wsl (Ubuntu 20.04)

Bug Summary and Steps to Reproduce

Bug Summary:
I am not using any VS code settings overrides for the C_Cpp.clang_format. options. I have a .clang-format with the following content:

BasedOnStyle: Chromium
InsertNewlineAtEOF: true

The C file src/main.c contents are:

#include <stdio.h>

/**
 * @brief Some info
 *
 * @param argc - The main `arg`
 * @param argv - The not main arg
 * @return int
 */
int main(int argc, char const* argv[]) {
  int a, b;
  printf("Foo mains\n");
  return 0;
}

If using the Format Document command from within VS Code it does not add a new line at EOF (End Of File). However, if I invoke the clang-format command from a terminal, it does add a new line at EOF:

> clang-format -i src/main.c

Steps to reproduce:

  1. Copy & paste the above files and test it via the VS code command Format Document.

Expected behavior:
It should format the file and add a new line at EOF.

Configuration and Logs

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c99",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        },
        {
            "name": "CMake",
            "compileCommands": "${config:cmake.buildDirectory}/compile_commands.json",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}
-------- Diagnostics - 9/4/2024, 1:10:29 PM
Version: 1.21.6
Current Configuration:
{
    "name": "Linux",
    "includePath": [
        "/home/tobias/code/c-test/**"
    ],
    "defines": [],
    "compilerPath": "/usr/bin/gcc",
    "cStandard": "c99",
    "cppStandard": "gnu++14",
    "intelliSenseMode": "linux-gcc-x64",
    "configurationProvider": "ms-vscode.cmake-tools",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "compilerPathInCppPropertiesJson": "/usr/bin/gcc",
    "configurationProviderInCppPropertiesJson": "ms-vscode.cmake-tools",
    "mergeConfigurations": false,
    "browse": {
        "path": [
            "/home/tobias/code/c-test/**",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Custom browse configuration: 
{
    "browsePath": [
        "/home/tobias/code/c-test/src"
    ],
    "compilerPath": "/usr/bin/gcc",
    "compilerArgs": [],
    "compilerFragments": [
        "-g"
    ]
}
cpptools version (native): 1.21.6.0
Translation Unit Mappings:
[ /home/tobias/code/c-test/src/main.c - source TU]:
Translation Unit Configurations:
[ /home/tobias/code/c-test/src/main.c ]:
    Process ID: 30660
    Memory Usage: 15 MB
    Compiler Path: /usr/bin/gcc
    Includes:
    System Includes:
        /usr/lib/gcc/x86_64-linux-gnu/9/include
        /usr/local/include
        /usr/include/x86_64-linux-gnu
        /usr/include
    Standard Version: c17
    IntelliSense Mode: linux-gcc-x64
    Other Flags:
        --gcc
        --gnu_version=90400
Total Memory Usage: 15 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 4908
LSP: (received) cpptools/didChangeActiveEditor: file:///home/tobias/code/c-test/src/main.c
LSP: (invoked) cpptools/didChangeActiveEditor: file:///home/tobias/code/c-test/src/main.c
LSP: (received) cpptools/formatDocument: file:///home/tobias/code/c-test/src/main.c (id: 161)
LSP: (invoked) cpptools/formatDocument: file:///home/tobias/code/c-test/src/main.c (id: 161)
Formatting document: file:///home/tobias/code/c-test/src/main.c
Formatting Engine: clangFormat
/home/tobias/.vscode-server/extensions/ms-vscode.cpptools-1.21.6-linux-x64/bin/../LLVM/bin/clang-format -style=file -fallback-style=LLVM --Wno-error=unknown -assume-filename=/home/tobias/code/c-test/src/main.c
LSP: Sending response (id: 161)
LSP: (received) textDocument/willSaveWaitUntil: file:///home/tobias/code/c-test/src/main.c (id: 162)
LSP: (invoked) textDocument/willSaveWaitUntil: file:///home/tobias/code/c-test/src/main.c (id: 162)
LSP: Sending response (id: 162)
willSaveWaitUntil: 0ms
LSP: (received) textDocument/didSave: file:///home/tobias/code/c-test/src/main.c
LSP: (invoked) textDocument/didSave: file:///home/tobias/code/c-test/src/main.c
Intellisense update pending for: file:///home/tobias/code/c-test/src/main.c
tag parsing file: /home/tobias/code/c-test/src/main.c
LSP: (received) cpptools/fileChanged: file:///home/tobias/code/c-test/src/main.c
LSP: (invoked) cpptools/fileChanged: file:///home/tobias/code/c-test/src/main.c
IntelliSense update scheduled and TU acquisition started for: file:///home/tobias/code/c-test/src/main.c
Update IntelliSense time (sec): 0.006
LSP: (received) cpptools/getFoldingRanges: file:///home/tobias/code/c-test/src/main.c (id: 163)
LSP: (invoked) cpptools/getFoldingRanges: file:///home/tobias/code/c-test/src/main.c (id: 163)
LSP: Sending response (id: 163)
LSP: (received) cpptools/getDocumentSymbols: file:///home/tobias/code/c-test/src/main.c (id: 164)
LSP: (invoked) cpptools/getDocumentSymbols: file:///home/tobias/code/c-test/src/main.c (id: 164)
LSP: Sending response (id: 164)
Database safe to open.
LSP: (received) cpptools/didChangeActiveEditor: file:///home/tobias/code/c-test/src/main.c
LSP: (invoked) cpptools/didChangeActiveEditor: file:///home/tobias/code/c-test/src/main.c
LSP: (received) cpptools/didChangeTextEditorSelection
LSP: (invoked) cpptools/didChangeTextEditorSelection
LSP: (received) cpptools/getCodeActions: file:///home/tobias/code/c-test/src/main.c (id: 165)
LSP: (invoked) cpptools/getCodeActions: file:///home/tobias/code/c-test/src/main.c (id: 165)
LSP: Sending response (id: 165)

Other Extensions

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions