| Issue |
239478
|
| Summary |
[clangd] A compile command containing `--version` makes the driver print to stdout, corrupting the LSP stream
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
pengi
|
When an entry in `compile_commands.json` contains `--version`, clangd hands it to the clang driver. This happens even when the entry isn't a compiler command; some build tools list every build step, not only compilations. The driver prints its version banner to stdout. In clangd, stdout is the LSP channel, so the banner lands between protocol messages and the editor's client loses the connection or reports a protocol error.
The bad entry doesn't have to be the file being edited. Opening any correct file in the project is enough, because background indexing processes every entry in the compdb. Indexing the bad entry fails, so the failure isn't cached and happens again every time clangd starts. A single bad entry therefore breaks clangd for the whole project.
### Reproducer
```sh
cat > compile_commands.json <<EOF
[
{"directory": "$PWD", "file": "main.c", "command": "cc -c main.c -o main.o"},
{"directory": "$PWD", "file": "notes.txt", "command": "cp notes.txt notes.out --version"}
]
EOF
echo 'int main(void) { return 0; }' > main.c
touch notes.txt
clangd --check=$PWD/notes.txt 2>/dev/null
```
Output on stdout:
```
Ubuntu clang version 20.1.2 (0ubuntu1~24.04.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```
`--check=$PWD/main.c` doesn't show this, because check mode doesn't run background indexing.
In an editor, opening only `main.c` is enough. Background indexing processes `notes.txt`, and the banner appears on stdout right after the JSON body of the `initialize` response, outside any `Content-Length` frame:
```
...,"serverInfo":{"name":"clangd","version":"Ubuntu clangd version 20.1.2 ..."}}}Ubuntu clang version 20.1.2 (0ubuntu1~24.04.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Content-Length: 234
```
clangd output shows (through vscode):
```
E[06:26:16.982] Indexing /home/pengi/test/notes.txt failed: Couldn't build compiler invocation
I[06:26:16.982] --> $/progress
[Error - 6:26:16 AM] Client Clang Language Server: connection to server is erroring.
Header must provide a Content-Length property.
{"ubuntu clang version 20.1.2 (0ubuntu1~24.04.3)\ntarget":"x86_64-pc-linux-gnu\nThread model: posix\nInstalledDir: /usr/bin\nContent-Length: 143"}
Shutting down server.
I[06:26:16.982] --> textDocument/clangd.fileStatus
```
Expected: nothing on stdout except LSP messages. The entry should fail to build (or be ignored), and the failure should be logged to stderr.
Reproduced with Linux Mint (ubuntu) using clangd 20.1.2.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs