Double backslash in compile_commands.json aren't be parsed in VSCode
See original GitHub issueI use the clangd extension in vscode and try to use clangd to process a cmake project. After I generated the compile_commands.json file and open a .c file, I find the clangd crashed. A part of the compile_commands.json is
{
"directory": "D:/Code/Cpp/DataStructures/build/CourseBook/0301_SqStack",
"command": "D:\\msys64\\clang64\\bin\\clang.exe @CMakeFiles/SqStack.dir/includes_C.rsp -O3 -DNDEBUG -std=gnu11 -o CMakeFiles\\SqStack.dir\\SqStack.c.obj -c D:\\Code\\Cpp\\DataStructures\\CourseBook\\0301_SqStack\\SqStack.c",
"file": "D:/Code/Cpp/DataStructures/CourseBook/0301_SqStack/SqStack.c"
},
Then I checked the JSON Compilation Database Format Specification and the command part says:
command: The compile command executed. After JSON unescaping, this must be a valid command to rerun the exact compilation step for the translation unit in the environment the build system uses. Parameters use shell quoting and shell escaping of quotes, with ‘"’ and ‘\’ being the only special characters. Shell expansion is not supported.
clangd works well after I replace all the \\
with /
. The logs part shows that the clangd don’t parse the \\
as a separator. To be honest, I am not sure if this problem is caused by vscode not pasting the command properly, or the clangd extension break it. The following part of logs may be useful:
I[23:16:08.487] <-- initialized
I[23:16:08.488] <-- textDocument/didOpen
I[23:16:08.495] Loaded compilation database from d:\Code/Cpp/DataStructures/build/compile_commands.json
I[23:16:08.498] ASTWorker building file d:\Code\Cpp\DataStructures\CourseBook\0201_SqList\SqList.c version 1 with command
[D:/Code/Cpp/DataStructures/build/CourseBook/0201_SqList]
D:\msys64\clang64\bin/msys64clang64binclang.exe -ID:/Code/Cpp/DataStructures/Status -O3 -DNDEBUG -std=gnu11 -o CMakeFilesSqList.dirSqList.c.obj -c D:CodeCppDataStructuresCourseBook0201_SqListSqList.c -fsyntax-only -resource-dir=D:/msys64/clang64/lib/clang/12.0.0
All the toolchains were installed in clang64 environment of msys2.
Logs
I[23:16:08.473] clangd version 12.0.0
I[23:16:08.474] PID: 15440
I[23:16:08.474] Working directory: d:\Code\Cpp\DataStructures
I[23:16:08.474] argv[0]: D:\msys64\clang64\bin\clangd.EXE
I[23:16:08.480] Starting LSP over stdin/stdout
I[23:16:08.480] <-- initialize(0)
I[23:16:08.480] Client supports legacy semanticHighlights notification and standard semanticTokens request, choosing the latter (no notifications).
I[23:16:08.483] --> reply:initialize(0) 3 ms
I[23:16:08.487] <-- initialized
I[23:16:08.488] <-- textDocument/didOpen
I[23:16:08.495] Loaded compilation database from d:\Code/Cpp/DataStructures/build/compile_commands.json
I[23:16:08.498] ASTWorker building file d:\Code\Cpp\DataStructures\CourseBook\0201_SqList\SqList.c version 1 with command
[D:/Code/Cpp/DataStructures/build/CourseBook/0201_SqList]
D:\msys64\clang64\bin/msys64clang64binclang.exe -ID:/Code/Cpp/DataStructures/Status -O3 -DNDEBUG -std=gnu11 -o CMakeFilesSqList.dirSqList.c.obj -c D:CodeCppDataStructuresCourseBook0201_SqListSqList.c -fsyntax-only -resource-dir=D:/msys64/clang64/lib/clang/12.0.0
I[23:16:08.499] <-- textDocument/documentSymbol(1)
I[23:16:08.499] <-- textDocument/codeAction(2)
I[23:16:08.500] <-- textDocument/documentLink(3)
I[23:16:08.501] --> textDocument/clangd.fileStatus
I[23:16:08.516] Loaded compilation database from D:\Code\Cpp\DataStructures/build/compile_commands.json
I[23:16:08.806] <-- textDocument/semanticTokens/full(4)
#0 0x00007ff918d92b1a clang::FileManager::getVirtualFileRef(llvm::StringRef, long long, long long) (D:\msys64\clang64\bin\libclang-cpp.dll+0x32b1a)
#1 0x00007ff918d92d76 clang::FileManager::getVirtualFile(llvm::StringRef, long long, long long) (D:\msys64\clang64\bin\libclang-cpp.dll+0x32d76)
#2 0x00007ff91a663165 clang::CompilerInstance::createPreprocessor(clang::TranslationUnitKind) (D:\msys64\clang64\bin\libclang-cpp.dll+0x1903165)
#3 0x00007ff91a6d558c clang::FrontendAction::BeginSourceFile(clang::CompilerInstance&, clang::FrontendInputFile const&) (D:\msys64\clang64\bin\libclang-cpp.dll+0x197558c)
#4 0x00007ff6e52e062d clang::clangd::PreamblePatch::create(llvm::StringRef, clang::clangd::ParseInputs const&, clang::clangd::PreambleData const&) (D:\msys64\clang64\bin\clangd.EXE+0x17062d)
#5 0x00007ff6e52df273 clang::clangd::PreamblePatch::create(llvm::StringRef, clang::clangd::ParseInputs const&, clang::clangd::PreambleData const&) (D:\msys64\clang64\bin\clangd.EXE+0x16f273)
#6 0x00007ff6e52d4b39 clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::__1::unique_ptr<clang::CompilerInvocation, std::__1::default_delete<clang::CompilerInvocation> >, llvm::ArrayRef<clang::clangd::Diag>, std::__1::shared_ptr<clang::clangd::PreambleData const>) (D:\msys64\clang64\bin\clangd.EXE+0x164b39)
#7 0x00007ff6e533d391 clang::clangd::TUScheduler::profile(clang::clangd::MemoryTree&) const (D:\msys64\clang64\bin\clangd.EXE+0x1cd391)
#8 0x00007ff6e5337da7 clang::clangd::TUScheduler::profile(clang::clangd::MemoryTree&) const (D:\msys64\clang64\bin\clangd.EXE+0x1c7da7)
#9 0x00007ff6e53a2b22 clang::clangd::PeriodicThrottler::operator()() (D:\msys64\clang64\bin\clangd.EXE+0x232b22)
#10 0x00007ff913b457a1 llvm::llvm_execute_on_thread_async(llvm::unique_function<void ()>, llvm::Optional<unsigned int>) (D:\msys64\clang64\bin\libLLVM.dll+0x1557a1)
#11 0x00007ff9af431bb2 (C:\WINDOWS\System32\ucrtbase.dll+0x21bb2)
#12 0x00007ff9af977034 (C:\WINDOWS\System32\KERNEL32.DLL+0x17034)
#13 0x00007ff9b174[Error - 下午11:16:09] Connection to server got closed. Server will not be restarted.
[Error - 下午11:16:09] Request textDocument/documentSymbol failed.
[object Object]
[Error - 下午11:16:09] Request textDocument/codeAction failed.
[object Object]
[Error - 下午11:16:09] Request textDocument/documentLink failed.
[object Object]
[Error - 下午11:16:09] Request textDocument/semanticTokens/full failed.
[object Object]
System information
Output of clangd --version
: clangd version 12.0.0
Editor/LSP plugin: VSCode 1.58.1 with CMake, CMake Tools, Clangd extensions
Operating system: Windows 10 19043.1110
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top GitHub Comments
@Locietta Thanks for your informing! I’ve tested it just now, which works well. But, since the PR is still a draft, the pre-compile binary may be not that stable. Hope this PR can be merged as soon as possible.
I do not find a normal way to deal with this annoying crash issue. Now I am using a little tricky way which may not be so elegant. A VS Code extension called File Watcher can run some commands after detecting the change of specific file, so I use it to automatically replace all the
\\
with/
. Here is the setting config:Please notice that the usage of
sed
may be different, depending on which package you get it from. Also, you can use other tools which can handle this job. If you try to use the toolkit frommsys
orcygwin
, write permission restrictions may prevent you from modifying target files.