question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot navigate to definition of member in header in files that are outside of compilation database and are alternatively indexed by clangd-indexer

See original GitHub issue

In short

Go to Definition results in No definition found for [...] in a file that is outside the compilation database (compile_commands.json), but is indexed using clangd-indexer.

Details

I have a project with sources that are build inside the project and pre built libraries that come in by a package manager and bring debug sources in separate folders with them.

- /build
   /**/somewhereHere/**/compile_commands.json
- /src
    ... hpp cpp files
- /externalSources
    *.a files
    - /debug
        hpp cpp files
.clangd
index.dex

I use a compilation database for background indexing and use an external index to index the library sources. (setup this by using .clangd config file) The indexed library sources are overall pretty good navigatable. So in general this approach seems to work for me.

In these lib sources there are some class members that are defined/initialised directly in the header (.hpp file) like this:

myType myClassVar {initvalue};

What fails

These members are referenced in the implementation of a class method (.cpp file). When I try to navigate back to their definition in the header file it cannot be found. This behavior applies to multiple members, that all share the property of beeing defined in the header and are referenced in the source.

What works

There are also definitions of members in the header that belong to the main project, so they are in the compile_commands.json. For these sources the problematic navigation works. So it has to do with the external index.

Investigation

In the index file (which is in YAML format) I can see that the member defined in the header has a declaration and a definition. Both point to the header file. That seems correct. In the indexes references section there is an entry that shows a correct defintion location of the member.

Additional information

I cannot share the project and I was unable to reproduce this in a minimum example project. In a minimal example the problematic navigation always worked.

Logs When I use verbose logging and start the Go to Definition function this is the output of the

clangd log (sensible information replaced with [...]
V[09:59:46.130] <<< {"id":15,"jsonrpc":"2.0","method":"textDocument/definition","params":{"position":{"character":10,"line":107},"textDocument":{"uri":"file:///c%3A/Users/[...]/swdev/[...]/debug/portable/WebSocket/MongooseTextWebSocket.cpp"}}}

I[09:59:46.130] <-- textDocument/definition(15)
V[09:59:46.132] ASTWorker running Definitions on version 3 of c:\Users\[...]\swdev\[...]\debug\portable\WebSocket\MongooseTextWebSocket.cpp
I[09:59:46.133] --> reply:textDocument/definition(15) 2 ms
V[09:59:46.133] >>> {"id":15,"jsonrpc":"2.0","result":[]}

I[09:59:46.133] --> textDocument/clangd.fileStatus
V[09:59:46.133] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"idle","uri":"file:///c:/Users/[...]/swdev/[...]/debug/portable/WebSocket/MongooseTextWebSocket.cpp"}}

System information Clangd version (from the log, or clangd --version): clangd version 15.0.3 (https://github.com/llvm/llvm-project 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1) clangd extension version: v0.1.23 Operating system: Windows 10

LLVM (http://llvm.org/):
  LLVM version 15.0.3
  Optimized build.
  Default target: x86_64-pc-windows-msvc

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ToBoMicommented, Dec 8, 2022

I set up the libs with compile_commands.json files, put the libs together with their compile_commands.json in a workspace and that works pretty well. Thanks for pointing me in that direction.

0reactions
ToBoMicommented, Nov 30, 2022

I don’t see how you can get around having a compile_commands.json containing entries for the library source files, with proper compiler flags including -I flags needed to compile the library source file. You may need to generate one yourself if you can’t get one from the library’s package.

I think I’ll try this, too and follow the instructions from https://github.com/clangd/vscode-clangd/issues/38#issuecomment-639986094 for this. Making the external index perfect might be much effort.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for multi-root workspaces · Issue #38 · clangd/vscode ...
Cannot navigate to definition of member in header in files that are outside of compilation database and are alternatively indexed by clangd-indexer #415....
Read more >
Headers files inside compile_commands.json (#16285) · Issues
A very common problem in these scenarios is to find out the flags from a header file since the json file will only...
Read more >
Clangd: Compilation-unit incorrectly inferred for a header file
I frequently observe cases where clangd infers an incorrect compilation-unit to use as a basis (i.e include path) for indexing a given header- ......
Read more >
extraclangtools(1) - Arch manual pages
Improved compile flags handling in clangd-indexer. The index file format changed in this release, indexes need to be rebuilt.
Read more >
Compilation database | CLion Documentation - JetBrains
A compilation database lets CLion detect project files and extract all the necessary compiler information, such as include paths and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found