{"record":{"id":"0a528f26daef68e6","repo":"BoundaryML/baml","slug":"tried-to-close-document-that-didn-t-exist-at","errorCode":null,"errorMessage":"tried to close document that didn't exist at {}","messagePattern":"tried to close document that didn't exist at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_server/src/session/index.rs","lineNumber":83,"sourceCode":"\n    pub fn make_document_ref(&self, document_key: DocumentKey) -> Option<DocumentQuery> {\n        // let url = self.url_for_key(&key)?.clone();\n        let controller = self.documents.get(&document_key)?;\n        Some(controller.make_ref(document_key))\n    }\n\n    pub fn open_text_document(&mut self, document_key: DocumentKey, document: TextDocument) {\n        self.documents\n            .insert(document_key, DocumentController::new_text(document));\n    }\n\n    pub fn close_document(&mut self, document_key: &DocumentKey) -> anyhow::Result<()> {\n        // let Some(url) = self.url_for_key(key).cloned() else {\n        //     anyhow::bail!(\"Tried to close unavailable document `{key}`\");\n        // };\n\n        let Some(_) = self.documents.remove(document_key) else {\n            anyhow::bail!(\n                \"tried to close document that didn't exist at {}\",\n                document_key\n            )\n        };\n        Ok(())\n    }\n\n    pub fn document_controller_for_key(\n        &mut self,\n        document_key: &DocumentKey,\n    ) -> anyhow::Result<&mut DocumentController> {\n        let Some(controller) = self.documents.get_mut(document_key) else {\n            anyhow::bail!(\"Document controller not available at `{}`\", document_key);\n        };\n        Ok(controller)\n    }\n\n    // fn url_for_key<'a>(&'a self, key: &'a DocumentKey) -> Option<&'a Url> {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_server/src/session/index.rs#L65-L101","documentation":"Raised by Index::close_document when a textDocument/didClose arrives for a document key that is not present in the index. The server treats closing an unknown document as an error rather than silently ignoring it.","triggerScenarios":"didClose for a URI that was never didOpen'd; duplicate didClose notifications; didClose after a server restart that cleared the index.","commonSituations":"Editor plugins firing didClose on virtual/untitled buffers never opened; race where client thinks a doc is open after server reload; double-close from client retry logic.","solutions":["Only send textDocument/didClose for URIs previously confirmed opened via didOpen","Handle/deduplicate close notifications on the client side","If this appears after a server crash/restart, reopen the document (didOpen) before closing","Log and ignore benign duplicate closes in the client integration"],"exampleFix":"// before\nclient.close(uri);\nclient.close(uri); // duplicate\n// after\nif (openDocs.has(uri)) { client.close(uri); openDocs.delete(uri); }","handlingStrategy":"try-catch","validationCode":"if (!openDocuments.has(uri)) return; // skip close for unknown docs","typeGuard":"function isOpen(uri: string): boolean { return openDocuments.has(uri); }","tryCatchPattern":"try { await sendDidClose(uri); } catch (e) { if (String(e).includes(\"didn't exist\")) { openDocuments.delete(uri); } else throw e; }","preventionTips":["Track open documents client-side and never double-close","Ignore stale close notifications after restarts","Deduplicate didClose events","Only close URIs previously opened"],"tags":["lsp","document","rust"],"backgroundTag":"resource-not-found","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}