clangd / clangd

clangd language server
https://clangd.llvm.org
Apache License 2.0
1.51k stars 63 forks source link

clangd ignores `--compile-commands-dir=<string>` for relative paths in compile_commands.json #864

Closed Febbe closed 2 years ago

Febbe commented 3 years ago

Please describe the problem:

When executing clangd in a directory distinct to the compile-commands-dir nearly all relative paths in the compile_commands.json can't be resolved, because clangd tries to resolve them relative to the clangd directory, not the directory of the file, the paths are from.

Logs No crash logs System information No special system informations

Output of clangd --version: clangd version 12.0.1 (https://github.com/llvm/llvm-project fed41342a82f5a3a9201819a82bf7a48313e296b)

Editor/LSP plugin: ms visual studio code - clangd plugin

Operating system: Windows 10

HighCommander4 commented 3 years ago

nearly all relative paths in the compile_commands.json can't be resolved

Which paths in your compile_commands.json are relative?

My reading is of the compilation database spec is that paths in command and file are allowed to be relative, and if so they are interpreted relative to directory.

The spec does not say anything about directory itself being relative; I would consider that an unsupported configuration.

kadircet commented 3 years ago

Handling of compile_commands.json is done at a higher level than clangd and shared amongst all clang-based tools. That layer assumes that the "directory" field will always be absolute. Even though it is not explicit in the spec as it could be, it says that the directory field is The working directory of the compilation. also it goes on like All paths specified in the command or file fields must be either absolute or relative to this directory.. I don't think it is possible for a relative directory to be relative to a non-absolute one, or working directory itself to be relative.

Hence when you have a relative directory in your compilation database, most tools will interpret it as relative to whatever the current working directory in the environment is.

That being said, I am not sure there are viable solutions here. Surely we can try to interpret everything relative to the whatever directory was specified in compile-commands-dir option, but:


Can you tell us more about your use case so we can try to figure out if it's symptom of a bigger problem? Otherwise as I mentioned I don't think there's much we can do.

Febbe commented 2 years ago

Hadn't much time, but here is what I found out so far:

HighCommander4 commented 2 years ago

I don't see any relative paths in that entry... so whatever the problem is, it's not related to relative paths.

HighCommander4 commented 2 years ago

A good next step to investigate your issue would be to share some clangd logs.

Febbe commented 2 years ago

There were some previously, but I updated cmake ninja and msvc. So this obviously means, that the problem is somewhere else.

Febbe commented 2 years ago

Ok seems like, there was a name clash between 2 targets, and clang handled them wrongly(?):

Febbe commented 2 years ago

I'll close this issue, since the bug (if there is any) is somewhere else. Also, it is easy to use the workaround (just don't use the same filename, even if those never appear in the same include path).