LLVM Discussion Forums

Cannot get clangd 10's background index to work

I am using clangd 10.0.0 in vim. I have the following options set

let g:ycm_clangd_args = [’-log=verbose’, ‘-pretty’, ‘-index’, ‘-background-index’, ‘-j=4’]
let g:ycm_clangd_uses_ycmd_caching = 0
let g:ycm_clangd_binary_path = exepath(“clangd”)

However, no background index is generated and cross TU GoToDefinition does not work. Can someone please tell me what I am doing wrong?

do you have a compile_commands.json in your project root telling clangd what all files in your project are?

if not, you unfortunately need to have that, otherwise clangd currently cannot figure out all the files you have in your project and will only index the files you are working on.
For generating compile_commands.json see http://clangd.llvm.org/config.html#compileflags

if you have a compile_commands.json, please share clangd logs so that we can take a more detailed look.

Thanks for your prompt reply. I do have a compile_commands.json and among the “loaded” files I can GoToDefinition. I cannot share my logs because of certain restrictions. Perhaps you can mention what errors to look for and I can tell you if they happened.

if the definition you are looking for is not part of the “loaded” files, you got 3 options:

  • clangd failed to index the relevant translation unit
  • clangd is still indexing, and hasn’t made it to the particular file yet
  • the file is not part of your compile_commands.json

you can verify the first two by passing -log=verbose to clangd and searching the looks for the name of the file you are looking for and check for the last one by searching for the name in the compile_commands.json instead.

if it is the first one, we can’t really help much without the code + compile commands for that file.
if it is the second, you just need to wait
if it is the last one, you need to figure out why your build system(or compile commands generator) doesn’t include that file in the database.

Thanks for your reply.

As it turns out

  1. There is no failure to index the relevant translation unit
  2. I do not see clangd indexing any more files
  3. The file is part of the compile_commands.json

Based on your response, it seems to me that clangd is not indexing in the background. Are my options for indexing correct as mentioned in my first post?


Yes they are correct. You can even just have

let g:ycm_clangd_args = [’-log=verbose’, ‘-pretty’, ‘-j=4’]

as -background-index and -index is already on by default.

Unfortunately I can’t help further without any logs. You can try a clangd built from head, or a weekly snapshot at https://github.com/clangd/clangd/releases to see if your problem is fixed at head.

After reading through the code for the references to
llvm::Optional getProjectInfo(PathRef File) const override;
I think that clangd may not be resolving the project root dir correctly. When I gave it the explicit command line option “-compile-commands-dir”, it began to index other files (I see it in the clangd logs).

The problem is I am using bazel, I use extra actions to generate the compile_commands.json. This file is in a directory above the “source” directory. Whereas bazel generates files in its own cache dir different from my source directory

Thanks again for all your help.