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.

Start `clangd` when a file of any type is opened if `compile_commands.json` is in a parent directory

See original GitHub issue

I’ve built a database for my project, which has .c, .h. and .S (assembly) files. When I open VSCode and navigate to a .c file, clangd is started and I can use commands like “Go to Symbol in Workspace” just fine, even if I navigate to other files like a .S file.

However, if I close VSCode while in a .S file (or any file that’s not a.c or .h file, like a Makefile), then reopen VSCode, I start in the last file I opened, and because it’s not a .c or .h file clangd isn’t started, and I can’t use any clangd features until I open a .c or .h file, which is inconvenient.

Can we add an option to start clangd when any file is opened, as long as there’s a compile_commands.json in some parent directory?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
njames93commented, Mar 22, 2022

That sounds like a solid plan, but I’d extend it to also look for the fallback CompileFlags.txt

0reactions
gsingh93commented, Mar 23, 2022

I’d find this behavior useful with or without strong workspace mode.

It seems like clangd needs to receive at least one textDocument/didOpen before it loads the index (see https://github.com/microsoft/language-server-protocol/issues/861 and https://discourse.llvm.org/t/clangd-explicit-and-implicit-doc-open/205).

While ideally I think this could be improved in clangd, I think it would be worth it to implement a small workaround in vscode-clangd to get the desired behavior. One idea is that if we detect a compile_commands.json, we can just send that to clangd in a textDocument/didOpen request. clangd will load the index regardless of whether the file we sent is a valid C++ file. I fully support putting this behind a setting that’s off by default.

I did a quick test of this by adding {scheme: 'file', language: 'json'} in clangdDocumentSelector in clangd-context.ts. After opening the compile_commands.json, I was able to find symbols in other C files in the index. This was just a test of course, we shouldn’t be modifying clangdDocumentSelector but instead locating the compile_commands.json and sending it directly.

@sam-mccall Does this solution seem acceptable or too much of a hack?

Read more comments on GitHub >

github_iconTop Results From Across the Web

parent directory (..) in compile_commands.json might prevent ...
in compile_commands.json might prevent background indexing #1317 ... editor (Emacs) starts one clangd process for each project for which I have a file...
Read more >
Getting started with clangd - LLVM releases
json file provides compile commands for all source files in the project. This file is usually generated by the build system, or tools...
Read more >
Configuration - What is clangd? - LLVM
Configuration is stored in YAML files. These are either: project configuration: a file named .clangd in the source tree. (clangd searches in all...
Read more >
Getting started - What is clangd? - LLVM
This file provides compile commands for every source file in a project. It is usually generated by tools. clangd will look in the...
Read more >
JSON Compilation Database Format Specification - Clang
Figuring out whether things have changed is often an IO bound process; this makes it ... A compilation database is a JSON file,...
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