bashmsys2clang-format

clang-format style file path format is not working


I'm using clang-format 15. According tot he documentation it is possible to specify custom style file with the --style option using --style='file:<format_file_path>.

However, I'm having problem with actual path specification that doesn't produce the Invalid value for -style error. If the format file is local like this, everything is fine:

clang-format --dry-run --fcolor-diagnostics --verbose --style='file:cmt.txt' -Werror ./template.h

But if it is located somewhere else, nothing works. I've tried these possibilities:

clang-format --dry-run --fcolor-diagnostics --verbose --style='file:/cmt.txt' -Werror ./template.h
clang-format --dry-run --fcolor-diagnostics --verbose --style="file:/cmt.txt" -Werror ./template.h
clang-format --dry-run --fcolor-diagnostics --verbose --style=file://cmt.txt -Werror ./template.h
clang-format --dry-run --fcolor-diagnostics --verbose --style=file:\/cmt.txt -Werror ./template.h

Is there some special character combination that clang-format will accept? I'm using bash 5.2.15 in msys2.


Solution

  • I was struggling with the same issue working with absolute paths and found out that this doesn't work

    clang-format --style=file:/c/.clang-format -n /c/src/main.c
    

    But both of the following works

    clang-format --style=file:c:\.clang-format -n c:\src\main.c
    clang-format --style=file:c:\.clang-format -n /c/src/main.c
    

    So, the root of the issue is that the '--style=file' option specifically doesn't work with some formats of path. In your case the correct command should be the following:

    clang-format --dry-run --fcolor-diagnostics --verbose --style="file:../cmt.txt" -Werror ./template.h
    

    To transform the absolute path in a proper format for '--style-file' option you can use the following command in your shell (like PowerShell) on Windows:

    $LINTER_PATH_WIN_ABS = Resolve-Path "..\cmt.txt"
    

    Test its contents after running the provided command to make sure it worked as expected:

    Write-Host $LINTER_PATH_WIN_ABS
    clang-format --dry-run --fcolor-diagnostics --verbose --style=file:$LINTER_PATH_WIN -Werror ./template.h