llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.02k stars 11.96k forks source link

[clang-format] does not respect .clang-format-ignore (version 18.1.8) #108337

Closed Dangeroustuber closed 3 weeks ago

Dangeroustuber commented 1 month ago

Hello, i am unable to make git-clang-format not format my C# files and for it to then only format my C++ that i stage in Git. clang-format is installed via pip and i am on windows. I want to use this for my precommit hook in a mixed C++ and C# project but running the command will format my C# files in a wrong manner.

clang format version: clang-format version 18.1.8

Reproduce:

one line linux command for the folders and files:

mkdir test-clang-format && cd test-clang-format && git init && touch test.cpp test.cs && mkdir src && touch src/test2.cpp src/test2.cs && echo '#include <iostream> int main() { std::cout << "Hello from test.cpp"; }' > test.cpp && echo 'using System; class Program { static void Main() { Console.WriteLine("Hello test.cs"); } }' > test.cs && echo '#include <iostream> int main() { std::cout << "Hello src/test2.cpp"; }' > src/test2.cpp && echo 'using System; class Program { static void Main() { Console.WriteLine("Hello test2.cs"); } }' > src/test2.cs

  1. Take a git-clang-format.exe and clang-format.exe and put it in a folder.
  2. Run git init in the folder
  3. Add a folder called src, with a file called test.cpp and a file called test.cs. Add some code into each
  4. git add the src folder or files individually
  5. Add a .clang-format and a .clang-format-ignore that looks like this:

.clang format:

---
UseTab: ForContinuationAndIndentation
IndentWidth: 4
TabWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
IndentCaseLabels: false
ColumnLimit: 180
SortIncludes: false

.clang-format-ignore:

src/*
  1. Run the command .\git-clang-format.exe --binary .\clang-format.exe -v --staged
  2. Output that i get:
Running clang-format on the following files:
    src/test2.cpp
    src/test2.cs
warning: in the working copy of 'src/test2.cpp', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'src/test2.cs', LF will be replaced by CRLF the next time Git touches it
old tree: f505a0103b68f71f76b044b83b94748d61361505
new tree: eda0caff2420a96e5c158d0ac738a3bbbf674107
changed files:
    src/test2.cpp
    src/test2.cs
  1. To keep testing and validating, make some changes in the two files and then stage them

image of test setup: image

What do i expect:

In this particular case it should not format any of the files seeing as we tell it to ignore the src folder.

Other things i've tried: I have also tried using *.cs in the ignore file and it still does the same thing. I also tried not having a src folder and just having the .cpp file and .cs file in the same directory as the ignore file and then using the "*" pattern in the ignore file. But this produces the same result.

Help:

Either my patterns are wrong for the ignore file or this ignore file does not seem to work for me at all. It feels like it's completely ignored itself. The pattern for the reproduce example i get from this quote in the 18.1.8 docs for clang.

https://releases.llvm.org/18.1.8/tools/clang/docs/ClangFormat.html

"To match all files in a directory, use e.g. foo/bar/*"

owenca commented 1 month ago

This may be fixed by #102629.

Dangeroustuber commented 1 month ago

Aha, okey. So i can try this again when the first 19 offical release is out?

edit: Also is this an issue with clang-format or git-clang-format?

owenca commented 1 month ago

Aha, okey. So i can try this again when the first 19 offical release is out?

Yep.

edit: Also is this an issue with clang-format or git-clang-format?

I would say git-clang-format, which must be updated to skip files ignored by clang-format when .clang-format-ignore was added.

owenca commented 3 weeks ago

Duplicate of #102459.