Skip to content

clangd spawns in wrong directory in CMake/Make projects #21446

@nonbanana

Description

@nonbanana

Description

In any CMake or Make project, opencode spawns a separate clangd instance per subdirectory that has a CMakeLists.txt or Makefile. Each instance independently indexes the full project, wasting memory and CPU.

The root is currently detected by:

NearestRoot(["compile_commands.json", "compile_flags.txt", ".clangd", "CMakeLists.txt", "Makefile"])

clangd does not read CMakeLists.txt or Makefile — they are completely irrelevant to it. On top of that, both appear in every subdirectory by design (add_subdirectory() in CMake, recursive make in Make), so NearestRoot returns a different root for each subdirectory, spawning a new clangd every time.

Tested with abseil-cpp (22 CMakeLists.txt across modules). Opening files from 3 different modules in a single session spawns 3 clangd instances, each indexing the full project:

absl/strings: 337MB
absl/base:    318MB
absl/time:    343MB
total:        998MB  ← same project indexed 3 times

With the fix (markers removed), one clangd covers the whole project at ~330MB.

redis has the same issue with nested Makefiles (src/Makefile, src/modules/Makefile).

CMakeLists.txt and Makefile should be removed. This was previously reported in #11617 and a fix was attempted in #11579, but the PR was closed without review. Submitting a simpler fix.

Steps to reproduce

git clone --depth=1 https://github.com/abseil/abseil-cpp /tmp/abseil-cpp
cd /tmp/abseil-cpp
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DABSL_BUILD_TESTING=OFF
ln -s build/compile_commands.json .
opencode

In the opencode session, read files from different modules:

> read absl/strings/str_split.cc
> read absl/base/log_severity.cc
> read absl/time/clock.cc

In a separate terminal:

ps aux | grep clangd | grep -v grep
# 3 separate clangd instances, each ~330MB

Each file from a new subdirectory spawns another clangd instance.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions