fix(lsp): Improve LSP for c/c++ project.#11579
Conversation
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Found a potentially related PR: #7409 - fix(lsp): add better clangd compile_commands detection This PR is directly related to your current PR #11579. Both address clangd LSP configuration issues, specifically dealing with compile_commands detection and directory handling for C/C++ projects. Check if #7409 is already merged or if it addresses the same problems you're fixing. |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
There was a problem hiding this comment.
Pull request overview
This PR improves the clangd LSP server configuration by fixing incorrect root directory detection and adding support for passing compile commands directory as an argument. Previously, clangd's root was detected by searching for marker files like CMakeLists.txt or Makefile, which often resulted in subdirectories rather than the project root. The new implementation uses the git repository root (Instance.worktree) when available, allowing clangd to correctly find configuration files like .clangd and compile_flags.txt.
Changes:
- Changes clangd root detection to use
Instance.worktree(git repo root) instead of nearest marker file - Adds logic to find and pass
compile_commands.jsondirectory to clangd via--compile-commands-dirargument - Implements smart search for
compile_commands.jsonprioritizing CMake build directories, then root directory, then first-level subdirectories
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| packages/opencode/src/lsp/clangd.ts | New file implementing helper functions to find CMake build directories and compile_commands.json |
| packages/opencode/src/lsp/server.ts | Updated Clangd configuration to use new root detection and compile commands search logic |
| packages/opencode/test/lsp/clangd.test.ts | Comprehensive test suite covering root detection and compile commands discovery scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Create src/lsp/clangd.ts with findCMakeBuildDirs and findCompileCommandsDir - Import in server.ts for Clangd.spawn - Tests import from clangd module directly
- Use worktree as LSP root for git projects to find .clangd at repo root - Search compile_commands.json in directory first, then worktree if needed - Add tests for non-git, git repo, and monorepo scenarios
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@neriousy @adamdotdevin @thdxr Would you please take time to review this PR. Thanks. |
What does this PR do?
Fixes #11617 #9521
LSP server of clangd was started with incorrect working directory and arguments.
clangd should run in root project directory and it will find
.clangdandcompile_flags.txtautomatically.Directory of
compile_commands.jsonshould be passed to clangd as argument in case that it is not in the up directories of active file. It is quite normal when project is built out of source tree.Todo:
In this patch, we try to find
compile_commands.jsonin the first level sub directories of root project and root project itself. In case of opencode is running on the subdirectory of git repository, it first search current working directory and then top directory of git repository.If multiply build directories exist, or it is not in above location, it will fail to find or choose one which is not expected. It would be better to find a way to detect working directory more AI way. #7780
If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!
How did you verify your code works?
Test cases added to cover above chagnes.