LSP: 'non-added document' error - missing textDocument/didOpen notification
Bug Description
When using the LSP tool with clangd, operations fail with an error indicating the document hasn't been opened/added to the language server session.
Error Message
non-added document
Root Cause Analysis
The LSP tool is sending requests (like textDocument/definition, textDocument/references, etc.) without first sending a textDocument/didOpen notification to the language server.
Per the LSP specification, before any text document operations can be performed, the client must notify the server that the document is open via textDocument/didOpen. This notification:
- Informs the server the document is now managed by the client
- Provides the document's initial content
- Enables the server to start tracking the document
Without this notification, clangd (and likely other LSP servers) will reject requests for that document.
Expected Behavior
Before performing any LSP operation on a file, Claude Code should:
- Send
textDocument/didOpenwith the file URI and content - Then proceed with the requested operation (goToDefinition, findReferences, etc.)
Environment
- OS: Windows 11
- LSP Server: clangd 21.1.8
- Claude Code: Latest version
Steps to Reproduce
- Configure clangd as an LSP server in Claude Code settings
- Use the LSP tool to perform any operation (e.g.,
goToDefinition) on a C/C++ file - Observe the "non-added document" error
Suggested Fix
In the LSP client implementation, before sending any textDocument/* request:
- Check if the document has been opened in the current session
- If not, read the file content and send
textDocument/didOpen - Track opened documents to avoid redundant notifications
- Optionally send
textDocument/didClosewhen appropriate
LSP Specification Reference
See textDocument/didOpen in the LSP 3.17 specification.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗