2

I have a compile_commands.json that reads

[
  {
    "directory": ".",
    "command": "clang++ -I/usr/local/include -I/usr/local/opt/libomp/include -std=c++2a -MT release/source.o -MMD -MP -MF release/source.d Wall -O3 -fopenmp -flto -DNDEBUG -c -o release/source.o source.cpp",
    "file": "source.cpp"
  },
  ...
]

for every source file. Compiling this succeeds. However, the clangd languageserver reports to VS Code that includes cannot be found that actually reside in /usr/local/include. The first lines of clangd --check read

I[14:19:21.165] Homebrew clangd version 15.0.7
I[14:19:21.166] Features: mac+xpc
I[14:19:21.166] PID: 52129
I[14:19:21.166] Working directory: /Users/...
I[14:19:21.166] argv[0]: clangd
I[14:19:21.166] argv[1]: --check=source.cpp
I[14:19:21.166] Entering check mode (no LSP server)
I[14:19:21.168] Testing on source file /Users/.../relative_cohomology.cpp
I[14:19:21.168] Loading compilation database...
I[14:19:21.182] Loaded compilation database from /Users/.../compile_commands.json
I[14:19:21.183] Generic fallback command is: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -resource-dir=/usr/local/Cellar/llvm/15.0.7_1/lib/clang/15.0.7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -- "/Users/.../source.cpp"
I[14:19:21.183] Parsing command...
I[14:19:21.184] internal (cc1) args are: -cc1 -triple x86_64-apple-macosx13.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name relative_cohomology.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=13.1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 820.1 "-fcoverage-compilation-dir=/Users/..." -resource-dir /usr/local/Cellar/llvm/15.0.7_1/lib/clang/15.0.7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/15.0.7_1/lib/clang/15.0.7/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -fdeprecated-macro "-fdebug-compilation-dir=/Users/..." -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -no-round-trip-args -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c++ "/Users/.../source.cpp"

I am not sure if the last line should contain any of the command line arguments that I gave, which it clearly does not. What's going wroing?

Note: clang++ is /usr/local/opt/llvm/bin/clang++ from Homebrew, as is clangd. Both are version 15.0.7. Working with a compile_flags.txt works.

Apparently, the culprit is "directory": ".". With an explicitly given working directory, it works. Why?

Bubaya
  • 615
  • 3
  • 13

1 Answers1

0

The Generic fallback command line in the log suggests that clangd is failing to find a suitable entry for the file you're opening in the compile_commands.json.

I suspect that's because your compile_commands.json is not well-formed. Based on the snippet you've shown, this may be because the value of directory is ".", which is not valid (it needs to be an absolute path; more details can be found in the specification).

Please see also my comment on your other question cautioning against authoring compile_commands.json manually, which is prone to running into issues like this. Better to use a dedicated tool like Bear or compiledb.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • I first used a tool that produces these entries, so I didn't expect "." is wrong. I switched to Bear. – Bubaya Mar 06 '23 at 12:17