vscode-clangd icon indicating copy to clipboard operation
vscode-clangd copied to clipboard

Feature Request: Auto-detect compiler-specific include paths for flags like `-fsanitize=address`

Open Willaaaaaaa opened this issue 7 months ago • 7 comments

Type: Feature Request

Problem Description When compiling with sanitizer flags (-fsanitize=address), Clang automatically adds compiler-specific include paths containing essential headers like sanitizer/asan_interface.h. These paths (e.g., /usr/lib/llvm-18/lib/clang/18/include/) are not part of standard system include directories.

However, clangd currently doesn't replicate this compiler behavior. Even when:

  1. The compilation database (compile_commands.json) contains sanitizer flags
  2. The project builds successfully with Clang

clangd fails to locate these internal headers, showing false-positive errors:

'sanitizer/asan_interface.h' file not found clang(pp_file_not_found)

Current Method Users must manually specify these paths in configuration files:

# .clangd
CompileFlags:
  Add: [-isystem, /usr/lib/llvm-18/lib/clang/18/include]

Proposed Solution clangd should automatically:

  1. Detect sanitizer flags (-fsanitize=*) in compilation commands
  2. Resolve the compiler's installation path from the command
  3. Add the corresponding internal include directory to its search path:
    <compiler-path>/../lib/clang/<version>/include
    

Given this compilation command:

{
  "command": "/usr/bin/clang++-18 -DENABLE_ASAN ... -fsanitize=address ... -std=gnu++20 -c /path/to/file.cpp",
  "file": "/scr/file.cpp",
  "directory": "/build/dir"
}

clangd should automatically add:

/usr/bin/clang++-18 → /usr/lib/llvm-18/lib/clang/18/include

This would remove the need for users to manually patch .clangd or CMakeLists.txt, and bring clangd's behavior more in line with compilers.

Extension version: 0.1.33 VS Code version: Code 1.99.2 (4949701c880d4bdb949e3c0e6b400288da7f474b, 2025-04-10T01:21:25.295Z) OS version: Windows_NT x64 10.0.26100 Modes: Remote OS version: Linux x64 5.15.167.4-microsoft-standard-WSL2

Willaaaaaaa avatar Apr 12 '25 09:04 Willaaaaaaa

Btw, the same problem also occurred when I used gcc/g++ toolchains. Path: /usr/lib/gcc/x86_64-linux-gnu/13/include/

Willaaaaaaa avatar Apr 12 '25 09:04 Willaaaaaaa

I think this should already work.

Could you share clangd logs from a session where you encounter this unresolved include error?

HighCommander4 avatar Apr 12 '25 18:04 HighCommander4

Apologies for not including the logs earlier - I'm still learning how to properly collect and filter them, so I've attached the full output below.

I didn't add the --log=verbose parameter as it generated an extremely long output, but please let me know if you'd like me to provide the verbose logs as well. Really appreciate your quick response and guidance!

I[17:18:31.521] <-- textDocument/didOpen
I[17:18:31.521] <-- textDocument/documentSymbol(19)
I[17:18:31.521] <-- textDocument/semanticTokens/full(20)
I[17:18:31.521] <-- textDocument/codeAction(21)
I[17:18:31.522] <-- textDocument/documentLink(22)
I[17:18:31.535] ASTWorker building file /mnt/d/Workspace/DataBase_CourseResources/miniob/src/common/mm/mem_pool.h version 1 with command inferred from /mnt/d/Workspace/DataBase_CourseResources/miniob/src/common/mm/mem_pool.cpp
[/mnt/d/Workspace/DataBase_CourseResources/miniob/build/src/common]
/usr/lib/llvm-18/bin/clang++-18 --driver-mode=g++ -DENABLE_ASAN -DLINUX -DUNIX -I/mnt/d/Workspace/DataBase_CourseResources/miniob/. -I/mnt/d/Workspace/DataBase_CourseResources/miniob/deps -I/mnt/d/Workspace/DataBase_CourseResources/miniob/src -I/mnt/d/Workspace/DataBase_CourseResources/miniob/src/common -Wall -Werror -Wno-error=sign-compare -DDEBUG -g -O0 -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope -g -c -x c++-header -std=gnu++20 -resource-dir=/home/willaaaaaaa/.vscode-server/data/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/19.1.2/clangd_19.1.2/lib/clang/19 -- /mnt/d/Workspace/DataBase_CourseResources/miniob/src/common/mm/mem_pool.h
I[17:18:31.547] --> textDocument/clangd.fileStatus
I[17:18:31.612] <-- $/cancelRequest
I[17:18:31.612] <-- textDocument/documentSymbol(23)
I[17:18:31.658] <-- textDocument/documentHighlight(24)
I[17:18:31.688] <-- textDocument/foldingRange(25)
I[17:18:31.689] --> reply:textDocument/foldingRange(25) 1 ms
I[17:18:31.733] <-- $/cancelRequest
I[17:18:31.733] <-- textDocument/codeAction(26)
I[17:18:31.786] <-- textDocument/documentSymbol(27)
I[17:18:32.078] <-- textDocument/inlayHint(28)
I[17:18:32.640] Built preamble of size 10180380 for file /mnt/d/Workspace/DataBase_CourseResources/miniob/src/common/mm/mem_pool.h version 1 in 1.08 seconds
I[17:18:32.640] --> textDocument/clangd.fileStatus
I[17:18:32.640] --> workspace/semanticTokens/refresh(2)
I[17:18:32.641] <-- reply(2)
E[17:18:32.735] IncludeCleaner: Failed to get an entry for resolved path : No such file or directory
I[17:18:32.754] --> textDocument/publishDiagnostics
I[17:18:32.754] --> textDocument/inactiveRegions
I[17:18:32.755] --> reply:textDocument/documentSymbol(19) 1234 ms, error: Task was cancelled.
[Error - 5:18:32 PM] Request textDocument/documentSymbol failed.
[object Object]
I[17:18:32.756] --> reply:textDocument/semanticTokens/full(20) 1234 ms
I[17:18:32.756] --> reply:textDocument/codeAction(21) 1234 ms, error: Task was cancelled.
I[17:18:32.756] --> reply:textDocument/documentLink(22) 1233 ms
[Error - 5:18:32 PM] Request textDocument/codeAction failed.
[object Object]
I[17:18:32.758] --> reply:textDocument/documentSymbol(23) 1145 ms
I[17:18:32.758] --> reply:textDocument/documentHighlight(24) 1100 ms
I[17:18:32.758] --> reply:textDocument/codeAction(26) 1025 ms
I[17:18:32.759] --> reply:textDocument/documentSymbol(27) 972 ms
I[17:18:32.760] --> reply:textDocument/inlayHint(28) 681 ms
I[17:18:32.760] --> textDocument/clangd.fileStatus
I[17:18:32.992] <-- textDocument/codeAction(29)
I[17:18:32.992] --> reply:textDocument/codeAction(29) 0 ms
I[17:18:32.992] --> textDocument/clangd.fileStatus
I[17:18:34.048] <-- textDocument/semanticTokens/full/delta(30)
I[17:18:34.048] --> reply:textDocument/semanticTokens/full/delta(30) 0 ms
I[17:18:34.048] --> textDocument/clangd.fileStatus

Willaaaaaaa avatar Apr 13 '25 09:04 Willaaaaaaa

Thanks.

So, the idea behind headers like sanitizer/asan_interface.h, is that they are part of a compiler's "built-in headers", which are closely tied to the compiler, and therefore clangd ships and uses its own.

The path clangd uses for its built-in headers is printed as the value of the -resource-dir added to the command. It's usually at a path related to the location of the clangd binary (and then /include is added to it to search for built-in headers).

In my case, I installed clangd 19 as a package from https://apt.llvm.org/, and my -resource-dir is /usr/lib/llvm-19/lib/clang/19, and indeed /usr/lib/llvm-19/lib/clang/19/include contains sanitizer/asan_interface.h.

In your log, I can see your -resource-dir is /home/willaaaaaaa/.vscode-server/data/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/19.1.2/clangd_19.1.2/lib/clang/19, so I would expect that /home/willaaaaaaa/.vscode-server/data/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/19.1.2/clangd_19.1.2/lib/clang/19/include contains sanitizer/asan_interface.h.

I take it, since you're reporting this issue, this file is not there?

HighCommander4 avatar Apr 14 '25 00:04 HighCommander4

Thank you for the detailed explanation!

You're absolutely right - I've confirmed that the file sanitizer/asan_interface.h does not exist in the expected location: /home/willaaaaaaa/.vscode-server/.../clangd_19.1.2/lib/clang/19/include/ Image

From my system search, these sanitizer headers only appear in GCC and older Clang versions:

$ find /usr -name "asan_interface.h"

/usr/lib/gcc/x86_64-linux-gnu/13/include/sanitizer/asan_interface.h
/usr/lib/llvm-18/lib/clang/18/include/sanitizer/asan_interface.h
/usr/lib/llvm-14/lib/clang/14.0.6/include/sanitizer/asan_interface.h

As a student still learning things, now I suspect this might be due to my configuration issues. So, I plan to reinstall the LLVM/Clang toolchain during my upcoming free time and then test if the headers are properly included.

Since this is my first time setting up the complete LLVM toolchain (I only use GCC previosly), the process might take me some time to get right. I'll report back after reinstalling.

btw, thanks for your link. But aftering searching, I noticed the nightly packages you recommended might auto-update too frequently for my needs (as a beginner) and could potentially impact system resources. So I think I'll install a newer stable version instead. I think I will follow the instruction in https://clangd.llvm.org/installation.

Really appreciate your response. Your analysis of the logs is a really great learning opportunity for me. From now on, I will try to learn your way of thinking to analyze the log!

Willaaaaaaa avatar Apr 14 '25 03:04 Willaaaaaaa

I've confirmed that the file sanitizer/asan_interface.h does not exist in the expected location: /home/willaaaaaaa/.vscode-server/.../clangd_19.1.2/lib/clang/19/include/

Thanks. That directory looks like the location where vscode-clangd's auto-install feature installs clangd. That feature downloads a clangd package from https://github.com/clangd/clangd/releases, which are packages built by our CI.

I've confirmed that these packages are missing the sanitizer subdirectory in the built-in include directory. I believe this is a bug, and filed https://github.com/clangd/clangd/issues/2365 about it.

In the meantime, my suggestion would be to install clangd another way; in particular, whatever package gave you /usr/bin/clang++-18 should have a corresponding clangd package that will give you a /usr/bin/clangd which will find /usr/lib/llvm-18/lib/clang/18/include/sanitizer/....

HighCommander4 avatar Apr 14 '25 03:04 HighCommander4

Problem solved by setting the clangd.path in vscode to /usr/bin/clangd, and everything works perfectly now! The missing headers are correctly resolved, and the diagnostics are gone.

Image

I really appreciate your support and the time you took to help me understand what's going on. This was a great learning opportunity for me!

I also hope the issue you filed gets resolved soon. Fixing it would definitely save newcomers like me a lot of confusion. Thank you and the clangd team for your continued effort and awesome work!

Willaaaaaaa avatar Apr 14 '25 05:04 Willaaaaaaa