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.

Double backslash in compile_commands.json aren't be parsed in VSCode

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
9uanhuocommented, Oct 5, 2021

@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.

1reaction
9uanhuocommented, Sep 24, 2021

@9uanhuo could you provide some context for closing this issue – did you find a solution or workaround, or conclude the issue is invalid?

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:

  "filewatcher.commands": [
    {
      "match": "compile_commands.*",
      "isAsync": false,
      "cmd": "sed -i s#\\\\\\\\#/#g ${file}",
      "event": "onFolderChange"
    }
  ],

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 from msys or cygwin, write permission restrictions may prevent you from modifying target files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`arguments` field in `compile_commands.json` should be ...
json have argument to define a string literal from the command line such as "-DIDENT=\"Some string\"" , VS Code should parse this as...
Read more >
C/C++ extension FAQ - Visual Studio Code
In the "Advanced" section of the Configuration UI, you can supply the path to your compile_commands.json and the extension will use the compilation...
Read more >
Path with double backslash in VS-Code Task - Stack Overflow
I want to create a VS-Code task, that executs a R command to convert markdown into PDF. My current task looks like this:...
Read more >
D113268 [clangd] Fix tests with forward slash as separator on ...
Yes, but that's not what the changes does. Regardless of the preference for forward or backwards slashes, both configurations (at least when it ......
Read more >
559674 – Improve parser for compile_commands.json - Bugs
It parses the compile_commands.json file produced by cmake to extract ... This is not something extra you need to do, simply submitting your ......
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