How to create clangd the indexes from a script

We use clangd as language server in vscode. but the indexes take too long to create because of the size of our codebase. So I’m looking for a way to create the indexes automatically from our build system and distribute it to the developers when they clone their workspace
I tried using clangd-indexer to create the indexes using the following command:
clangd-indexer --executor=all-TUs compile_commands.json 1> clangd.dex 2> indexer.log
and add the clangd.dex file to the arguments in vscode to pass to clangd using --index-file but that does not seem to do anything and still clangd tries to compile the files. How can I tell clangd to only use the indexes that have been created by the clangd-indexer and don’t bother trying to rebuild them?


Have you looked at the remote index of clangd?

but that does not seem to do anything and still clangd tries to compile the files

Clangd will still parse files when they’re opened – this is needed for the majority of clangd’s features (such as semantic highlighting, inlay hints, code completion of local symbols, and all other “translation-unit local” features). The index is only used for “cross translation unit” featurs like “find all references in project”.

However, using --index-file should successfully prevent clangd from running its “background indexer” to parse all files in the project (not just open files) to build an index in the background.

thanks. so what does the created indexes in clangd.dex actually do?

so what does the created indexes in clangd.dex actually do?

It’s used instead of the background index for the cross-TU features like “find references”, code completion proposals from other files, “find symbol in workspace”, etc.

I see …
what confuses me is that it seems that clangd is still rebuilding the indexes for all files.
is it actually using what is in the clangd.dex to create the indexes under the .cache directory?

I will also try to use this.
thanks for the link

It shouldn’t be, if you’re seeing this then something is going wrong.

Can you share clangd logs for further diagnosis?

I restarted the whole thing
Will try to share the logs later.
If it matters, Im using rather older version of clangd (llvm-11)
Has there been known issues?

I can’t think of a specific issue off the top of my head, but that’s a very old version so you may well be running into an issue that has since been fixed.

I would recommend trying clangd 16 from Releases · clangd/clangd · GitHub.

I don’t think I can provide the logs.
but I think I got some handle on it.
Nevertheless, the problem I have is that indexing takes too long (about 20 hours). I’m thinking of a way to do it on a cluster and do it per file basis instead of clumping everything in one clangd-indexer command.
my goal is to create what clangd creates in .cache/clangd/index for each individual file but I see that there is a naming convention there and a hash is added at the end of filename.
Question: is it possible to create these files using clangd-indexer and use it with clangd?

clangd-indexer does not currently support creating an index in the “background index” format that clangd builds in the background, but I think this would be a valuable enhancement. We have Offline background index preparation · Issue #587 · clangd/clangd · GitHub on file about it.

thank you.
Then how can I combine two index files that clangd-indexer generates?
Or maybe it is possible to use multiple index files from clangd?

I don’t think that’s currently possible, it’s a new functionality that would have to be implemented.

Merging indexes is not necessarily a trivial operation: say one indexing process indexes a.cpp, and another indexing process indexes b.cpp, but a.cpp and b.cpp both include a common header common.hpp. Both indexing processes will generate an .idx file for common.hpp, and the merging process would need to combine their respective contents.

thank you.
I will read through the links you sent and will get back to you if I have more question.