Skip to content

fix(lsp): Improve LSP for c/c++ project.#11579

Closed
wangzw wants to merge 7 commits intoanomalyco:devfrom
wangzw:dev
Closed

fix(lsp): Improve LSP for c/c++ project.#11579
wangzw wants to merge 7 commits intoanomalyco:devfrom
wangzw:dev

Conversation

@wangzw
Copy link
Copy Markdown

@wangzw wangzw commented Feb 1, 2026

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 .clangd and compile_flags.txt automatically.

Directory of compile_commands.json should 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.json in 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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 1, 2026

Hey! Your PR title Improve LSP for c/c++ project doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 1, 2026

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.

@wangzw wangzw changed the title Improve LSP for c/c++ project fix(lsp): Improve LSP for c/c++ project Feb 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 1, 2026

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@wangzw wangzw changed the title fix(lsp): Improve LSP for c/c++ project fix(lsp): Improve LSP for c/c++ project. Fixes 11617 Feb 1, 2026
@wangzw wangzw changed the title fix(lsp): Improve LSP for c/c++ project. Fixes 11617 fix(lsp): Improve LSP for c/c++ project. Fixes #11617 Feb 1, 2026
@wangzw wangzw changed the title fix(lsp): Improve LSP for c/c++ project. Fixes #11617 fix(lsp): Improve LSP for c/c++ project. Feb 1, 2026
@wangzw wangzw marked this pull request as ready for review February 1, 2026 13:43
Copilot AI review requested due to automatic review settings February 1, 2026 13:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.json directory to clangd via --compile-commands-dir argument
  • Implements smart search for compile_commands.json prioritizing 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.

wangzw and others added 6 commits February 1, 2026 22:42
- 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>
@wangzw
Copy link
Copy Markdown
Author

wangzw commented Feb 2, 2026

@neriousy @adamdotdevin @thdxr Would you please take time to review this PR. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clangd LSP set incorrect root and missing arguments

2 participants