vscode-clangd: "clangd: restart language server" results in `Error: command 'clangd.applyFix' already exists` if clangd isn't started yet

When I restart clangd I get the following error as two notifications and also as log entry.

Logs Please attach the clangd log from the “Output” window if you can.

clangd extension logs from just the restart
I[11:41:39.338] clangd version 16.0.2 (https://github.com/llvm/llvm-project 18ddebe1a1a9bde349441631365f0472e9693520)
I[11:41:39.338] Features: windows+grpc
I[11:41:39.338] PID: 12944
I[11:41:39.338] Working directory: [OMITTED BY ISSUE REPORTER]
I[11:41:39.338] argv[0]: c:\Users\deboeto\AppData\Roaming\Code\User\globalStorage\llvm-vs-code-extensions.vscode-clangd\install\16.0.2\clangd_16.0.2\bin\clangd.exe
I[11:41:39.338] argv[1]: --query-driver=C:\DevApps\build-support\gcc-arm-none-eabi-11\bin\arm-none-eabi-c++.exe
I[11:41:39.345] Starting LSP over stdin/stdout
I[11:41:39.346] <-- initialize(0)
I[11:41:39.374] --> reply:initialize(0) 28 ms
[Error - 11:41:39 AM] Server initialization failed.
Error: command 'clangd.applyFix' already exists
	at e.$vL.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:99:62663)
	at Object.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:117:20174)
	at x1.register (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71984)
	at x1.initialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71547)
	at ff.initializeFeatures (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:19266)
	at ff.doInitialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:7616)
	at async ff.start (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:4636)
[Error - 11:41:39 AM] Clang Language Server client: couldn't create connection to server.
Error: command 'clangd.applyFix' already exists
	at e.$vL.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:99:62663)
	at Object.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:117:20174)
	at x1.register (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71984)
	at x1.initialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71547)
	at ff.initializeFeatures (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:19266)
	at ff.doInitialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:7616)
	at async ff.start (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:4636)
[Error - 11:41:39 AM] Sending notification textDocument/didOpen failed.
Error: command 'clangd.applyFix' already exists
	at e.$vL.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:99:62663)
	at Object.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:117:20174)
	at x1.register (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71984)
	at x1.initialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71547)
	at ff.initializeFeatures (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:19266)
	at ff.doInitialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:7616)
	at async ff.start (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:4636)
[Error - 11:41:39 AM] Sending document notification textDocument/didOpen failed
Error: command 'clangd.applyFix' already exists
	at e.$vL.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:99:62663)
	at Object.registerCommand (c:\Users\deboeto\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:117:20174)
	at x1.register (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71984)
	at x1.initialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:37:71547)
	at ff.initializeFeatures (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:19266)
	at ff.doInitialize (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:7616)
	at async ff.start (c:\Users\deboeto\.vscode\extensions\llvm-vs-code-extensions.vscode-clangd-0.1.24\out\bundle.js:39:4636)
I[11:41:39.375] <-- initialized
I[11:41:39.378] <-- shutdown(1)
I[11:41:39.379] --> reply:shutdown(1) 0 ms
I[11:41:39.379] <-- exit
I[11:41:39.379] LSP finished, exiting with status 0
I[11:41:39.380] <-- textDocument/documentSymbol(22)
I[11:41:39.382] --> reply:textDocument/documentSymbol(22) 2 ms
I[11:41:39.382] --> textDocument/clangd.fileStatus
I[11:41:39.383] <-- textDocument/codeAction(23)
I[11:41:39.383] --> reply:textDocument/codeAction(23) 0 ms
I[11:41:39.383] --> textDocument/clangd.fileStatus
I[11:41:39.391] <-- textDocument/codeAction(24)
I[11:41:39.391] --> reply:textDocument/codeAction(24) 0 ms
I[11:41:39.391] --> textDocument/clangd.fileStatus
I[11:41:39.393] <-- textDocument/documentSymbol(25)
I[11:41:39.393] <-- textDocument/documentSymbol(26)
I[11:41:39.393] --> reply:textDocument/documentSymbol(25) 0 ms
I[11:41:39.393] --> reply:textDocument/documentSymbol(26) 0 ms
I[11:41:39.394] --> textDocument/clangd.fileStatus
I[11:41:39.421] <-- textDocument/documentLink(27)
I[11:41:39.422] --> reply:textDocument/documentLink(27) 0 ms
I[11:41:39.422] --> textDocument/clangd.fileStatus
I[11:42:11.318] <-- textDocument/didClose
I[11:42:11.318] --> textDocument/publishDiagnostics

System information Clangd version (from the log, or clangd --version): clangd version 16.0.2 clangd extension version: clangd v0.1.24 Operating system: Windows 10

I write this issue in relation to https://github.com/clangd/vscode-clangd/issues/115#issuecomment-1500748876

edit: This only happens when clangd is not started (no source code file is open) and the server is restarted. A better error message would be helpful, like “Cannot restart server, it is not started yet (Open a source file to start it)”

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 21 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Does it make sense to start it, when there is no source file open?

I would say yes. There are operations the language server supports that don’t require an open file.

An example would be workspace/symbol. A user might want to open their first file based on the name of a class or function it contains (looked up using workspace/symbol), rather than using the file name.

This is an aside, but mentioning for completeness: the workflow I described here involving workspace/symbol doesn’t actually work today, because clangd doesn’t load the project’s index until a source file is opened. (See https://github.com/clangd/clangd/discussions/1341.)

However, I view that as a limitation to be fixed in clangd (at least in some configurations), and overall I still think the less surprising behaviour is for “clangd: restart language server” to consistently result in a state where the language server is running.

Thanks everyone for helping.

I’ve debugged a bit what’s going on here.

The first thing to note is that the handler for the clangd.restart command is registered when the extension is activated (normally triggered by opening a C/C++ source file). So, how is it even possible to invoke the command when the extension is not yet activated?

The answer seems to be that, since the command is declared in the extension’s package.json, vscode knows about it, and internally registers it (e.g. such that it shows up in the Command Palette) even without the extension being activated. When it’s invoked, if the extension hasn’t been activated, then vscode first activates the extension (allowing it to register the handler for the command), and then executes the handler.

So, in principle, calling clangd.restart when the extension is not yet activated should result in the extension being activated (which also starts the server), then running the handler (which stops the just-started server and starts it again).

However, it seems that vscode runs the handler “too soon” after activating the extension for the above sequence to actually succeed. At the time the handler is invoked, the LanguageClient is still in the Starting state, and our attempt to stop it triggers an exception (Error: Client is not running and can't be stopped. It's current state is: starting), and things go downhill from there (the rest of the cleanup code doesn’t run, and then trying to start things without having cleaned up properly we end up trying to do things like register commands twice).

Regarding build configurations, I would suggest taking that discussion to a dedicated issue for visibility.

any update on this? facing similar issues…