Format.cmake icon indicating copy to clipboard operation
Format.cmake copied to clipboard

Exclude paths/files from being formatted

Open carun opened this issue 8 months ago • 5 comments
trafficstars

We use kind of mono repo and thus our repo consists of a mix of languages such as C++, Python, Go and Java. The tree is somewhat akin to this.

How do we exclude certain paths and files from being formatted? Alternatively, is there a way to include only certain paths?

$ tree -a
.
├── .clang-format
├── .cmake-format
├── CMakeLists.txt
├── cpp1
│   └── main.cpp
├── cpp2
│   └── main.cpp
├── go1
│   └── main.go
├── go2
│   └── main.go
├── java1
│   └── Main.java
├── java2
│   └── Main.java
├── proto
│   ├── Message.proto
│   └── Service.proto
├── python1
│   └── main.py
└── python2
    └── main.py

carun avatar Mar 12 '25 20:03 carun

See https://clang.llvm.org/docs/ClangFormat.html#clang-format-ignore

ClausKlein avatar Mar 12 '25 22:03 ClausKlein

I think the python script in this repo is overriding it. See:

/tmp/repo (main)$ clang-format --version
Ubuntu clang-format version 18.1.3 (1ubuntu1)
/tmp/repo (main)$
/tmp/repo (main)$ ll .clang-format*
-rw-r--r-- 1 arun arun 1110 Mar 12 16:20 .clang-format
-rw-r--r-- 1 arun arun   10 Mar 12 16:25 .clang-format-ignore
/tmp/repo (main)$
/tmp/repo (main)$ cat .clang-format-ignore
java*
go*
/tmp/repo (main)$ cd build
/tmp/repo/build (main)$
/tmp/repo/build (main)$ ninja fix-format
[1/2] cd /tmp/repo && /bin/python3 /tmp/repo/build/_deps/format.cmake-src/git-clang-format.py --binary=/bin/clang-format 4b825dc642cb6eb9a060e54bf8d69288fbee4904 -f
FAILED: _deps/format.cmake-build/CMakeFiles/fix-clang-format /tmp/repo/build/_deps/format.cmake-build/CMakeFiles/fix-clang-format
cd /tmp/repo && /bin/python3 /tmp/repo/build/_deps/format.cmake-src/git-clang-format.py --binary=/bin/clang-format 4b825dc642cb6eb9a060e54bf8d69288fbee4904 -f
Configuration file(s) do(es) not support Java: /tmp/repo/.clang-format
error: `/bin/clang-format -lines=1:5 java1/Main.java` failed
[2/2] cd /tmp/repo && /usr/bin/cmake -DGIT_PROGRAM=/bin/git -DCMAKE_FORMAT_PROGRAM=/bin/cmak...e-build -DCMAKE_FORMAT_EXCLUDE= -P /tmp/repo/build/_deps/format.cmake-src/cmake-format.cmake ninja: build stopped: subcommand failed.

/tmp/repo/build (main)$

carun avatar Mar 12 '25 23:03 carun

I think the python script in this repo is overriding it

I know, that's the problem with this cmake module!

Try:

git ls-files ::*.cpp ::*.json | xargs clang-format -i

ClausKlein avatar Mar 13 '25 07:03 ClausKlein

I know how to run clang-format. The bug report is about this cmake module. So explicit clang-format invocation is not the solution.

carun avatar Mar 13 '25 08:03 carun

see https://github.com/TheLartians/Format.cmake/issues/41

The implementation is not as good as it should with an old local copy of the clang-format.py script.

That is the real problem.

ClausKlein avatar Mar 15 '25 11:03 ClausKlein