Skip to content

Clangd LSP set incorrect root and missing arguments #11617

Closed

Description

@wangzw

Description

Currently clangd root directory is detected by NearestRoot(["compile_commands.json", "compile_flags.txt", ".clangd", "CMakeLists.txt", "Makefile"]), which is usually result in the up directory which contains "CMakeLists.txt" or "Makefile" of active file, and is not the root directory of project. Instead, clangd should run in the root directory.

clangd will search from active file directory up to root directory to find .clangd or compile_flags.txt. We do not need to do the search again in LSPServer.

compile_commands.json is important to clangd, clangd will look in the parent directories of active file looking for it, and also in subdirectories named "build/". But it usually fails if one build project out of source tree. Argument of clangd (--compile-commands-dir) can be used to pass the location.

So all we need to do:

  1. set root directory of the project correctly.
  2. pass compile_commands.json using argument.

Plugins

No response

OpenCode version

No response

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. github-actions commented on Feb 1, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

  2. BLumia commented on Apr 2, 2026

    @BLumia

    Not sure if you are using Windows, if so, you might need to ensure opencode is using the clangd that you expect it to use. For me OpenCode by default will attempt to download and use its own clangd but I need to make it use MSYS2's clangd, thus custom configuration needed. See #7405 (comment) for example configuration.

  3. wangzw commented on Apr 7, 2026

    @wangzw
    Author

    Not sure if you are using Windows, if so, you might need to ensure opencode is using the clangd that you expect it to use. For me OpenCode by default will attempt to download and use its own clangd but I need to make it use MSYS2's clangd, thus custom configuration needed. See #7405 (comment) for example configuration.

    I'm working MacOS and Linux. I have already submitted a patch (#11579), but the PR was ignored. So I gave up.

  4. nonbanana commented on Apr 8, 2026

    @nonbanana
    Contributor

    I agree with this issue. In most Make and CMake projects, Makefile and CMakeLists.txt do not indicate the project root — they exist in countless subdirectories throughout the tree.

    llvm-project (https://github.com/llvm/llvm-project) is good example. llvm-project has CMakeLists.txt in every single subdirectory like clang/lib/Sema/, clang/lib/AST/, clang/lib/CodeGen/, etc. Opening any file there causes clangd to start in the wrong directory.
    These markers should be removed from the clangd root detection. Only compile_commands.json, compile_flags.txt, and .clangd reliably indicate where clangd should run.

  5. AadityaRavindran commented on Apr 23, 2026

    @AadityaRavindran

    For ROS c++ projects, the build directory is not in the src directory. The folder structure is

    ros_ws/
     |- build/compile_commands.json
     |- src/package/CMakeLists.txt
    

    If I'm not mistaken, NearestRoot is excluding parent folders, so even if the compile_commands.json file was in ros_ws/compile_commands.json, it wouldn't find it. It would be nice if we could configure the build directory explicitly. It would also be helpful to add a note to https://opencode.ai/docs/lsp#how-it-works which talks about the location of compile commands, or how to configure the build directory for clangd.

    I believe this is how we do it now, but making this explicit in the documentation would help.

    {
      "$schema": "https://opencode.ai/config.json",
      "permission": {
        "lsp": "allow"
      },
      "lsp": {
        "clangd": {
          "command": [
            "clangd",
            "--compile-commands-dir=<your build directory with the compile_commands.json file>"
          ]
        }
      }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions