pocc / pre-commit-hooks

C/C++ hooks to integrate with pre-commit
Apache License 2.0
323 stars 67 forks source link

file not found [clang-diagnostic-error] #include "lib1.h" ^~~~~~~~ #62

Open dshengChen opened 1 year ago

dshengChen commented 1 year ago

my pre-commit file look like:

fail_fast: false
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0  # Use the ref you want to point at
    hooks:
    -   id: trailing-whitespace
    -   id: check-added-large-files
-   repo: https://github.com/pocc/pre-commit-hooks
    rev: master
    hooks:
      - id: clang-format
        args: [-i]
      - id: clang-tidy
      # - id: oclint
      # - id: uncrustify
      # - id: cppcheck
      # - id: cpplint

and it's a cmake c++ projects and I added set(CMAKE_EXPORT_COMPILE_COMMANDS ON) on my cmakelist.txt and cmake generate file namedcompile_commands.json and it`s contain the file c++ src file path.

{
  "directory": "/Users/chendongsheng/github/cmake_test/build",
  "command": "/usr/bin/clang++ -I/Users/chendongsheng/github/cmake_test/lib1/. -I/Users/chendongsheng/github/cmake_test/lib2/. -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -o lib1/CMakeFiles/lib1.dir/aaa.cpp.o -c /Users/chendongsheng/github/cmake_test/lib1/aaa.cpp",
  "file": "/Users/chendongsheng/github/cmake_test/lib1/aaa.cpp"
},

but when i try to run pre-commit run --all-files

I got error like:

[WARNING] The 'rev' field of repo 'https://github.com/pocc/pre-commit-hooks' appears to be a mutable reference (moving tag / branch).  Mutable references are never updated after first install and are not supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.  Hint: `pre-commit autoupdate` often fixes this.
trim trailing whitespace.................................................Passed
check for added large files..............................................Passed
clang-format.............................................................Passed
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1

/Users/chendongsheng/github/cmake_test/app/main.cpp:1:10: error: 'lib1.h' file not found [clang-diagnostic-error]
#include "lib1.h"
         ^~~~~~~~
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "lib1/aaa.cpp"
No compilation database found in /Users/chendongsheng/github/cmake_test/lib1 or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "app/main.cpp"
No compilation database found in /Users/chendongsheng/github/cmake_test/app or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 error generated.
Error while processing /Users/chendongsheng/github/cmake_test/app/main.cpp.
Found compiler error(s).
dshengChen commented 1 year ago
(openai) ➜  cmake_test git:(master) ✗ pre-commit --version
pre-commit 3.2.1
(openai) ➜  cmake_test git:(master) ✗ clang-tidy --version
Homebrew LLVM version 16.0.6
  Optimized build.
image
dshengChen commented 1 year ago

My workSpace look like:

(openai) ➜  cmake_test git:(master) ✗ tree -L 1
.
├── CMakeLists.txt
├── aaa.plist
├── app
├── build
├── defaults.cfg
├── lib1
├── lib2
├── readme.md
└── tmp

and compile_commands.json is under folder build.

when I copy ./build/compiler_commands.json into ./lib1/ and ./lib2/ folder. The problem is fixed.

so I thinkd it's shoudle because the pre-commit can't find the file compiler_commands.json.

how coulde other peopel not see this problem? Is I miss something setting?