Clangd and compile_commands.json

Hello!

I have recently decided to start using neovim for my IDE. To get started I copied over GitHub - nvim-lua/kickstart.nvim: A launch point for your personal nvim configuration into my init.lua. I am currently trying to get clangd to use my “compile_commands.json” file but I haven’t had much luck.

Clangd appears to not be able to not find header files or other libraries.

I started looking into the lsp.log files. It seem that the files are being loaded.

The lsp.log can be found. nevim_clangd_issue/lsp.log at main · jwallace42/nevim_clangd_issue · GitHub.

I currently not quite sure how to proceed.

Thanks in advance!

Cheers,

Josh

P.S If this works I will never use CLION again :).

It might be a relative vs absolute path issue. I see in the log the error is:

message = "'nav2_bt_navigator/navigators/navigate_to_pose.hpp' file not found"

Which I presume is a valid path relative to the project root. But the compile_commands.json file is in a subdirectory of the build/ dir.
I don’t know the exact heuristic used by clangd to resolve paths but I know that there is the concept of “project root”. How are you setting the project root in your lsp setup? Typically it looks for a .git dir. If that’s the method you’re using and your project root isn’t the root of a git repo, then that could cause the issue. Another method of setting project root is to look for the compile_commands file, which would also cause a problem since yours is in the build/ dir, not project root.

My compile_commands.json files have absolute paths in them. CMake generates them and also for Makefile projects I use the compiledb utility to create the file (and it generates absolute paths). In the past I used to use compdb and bear which do similar things but may be of use to you.

When I have a project where the compile_commands.json file is in the build for, I create a link to it in the project root:

ln -s build/compile_commands.json ./

But it looks like you have several compile_commands.json files. I’m not sure how to handle that. Maybe one of those json files is your “main” one, and you can link that one?

Edit: this might help: Makefile command to generate compile_commands.json with entries for c++ headers · GitHub

Thanks for the reply!

The actual root directory is nav2_ws and the tree looks like the following.

├── build
├── install
└── src
    └── navigation2
            ├── package1
            └── package2

The git repo is navigation2. My compile commands are all absolute paths so I am not quite sure how that error is generated.

I currently don’t set my working directory at all :). I just open nvim in the root directory.

The only compile_commands.json I need is the one in the build directory I don’t need the others in the sub directories. Is there something special I need to do to set my working directory?

So,

the real issue was with “Too many error emitted, stopping now”. The solution can be found here.

@danngreen thank you for making me question everything I was doing :).