LSP - 'textDocument/publishDiagnostics' to a virtual editor

Sajith 66 Reputation points
2025-11-07T18:29:35.35+00:00

I am working on a Visual Studio extension that

  • Opens Code editors using IVSUIShell.CreateDocumentWindow.
  • Implements a Langauge Server (LSP) which is associated to these code editors

'textDocument/publishDiagnostics' that are sent from Language Server to these code editors are not working. (Errors sent are not getting added to error list).

But the errors sent from Language Server for local files work fine and the errors get added to error list.

Seems there is an issue when the errors are for a virtual document.

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-07T18:29:52.7+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It seems that you are encountering an issue with the textDocument/publishDiagnostics method when working with virtual documents in your Visual Studio extension. While errors from local files are being processed correctly and added to the error list, the same does not occur for virtual documents.

    This behavior may stem from the fact that support for Language Server Protocol (LSP) in Visual Studio is designed to work primarily in open folder/file scenarios. If your virtual documents are not being treated as standard documents by the LSP implementation, this could lead to the diagnostics not being published correctly.

    To troubleshoot this issue, consider the following steps:

    1. Ensure Proper Document Handling: Verify that your virtual documents are being recognized correctly by the LSP and that they are associated with the appropriate content type.
    2. Check LSP Implementation: Review your implementation of the language server to ensure that it correctly handles diagnostics for virtual documents. You may need to adjust how diagnostics are published or how the virtual documents are registered.
    3. Debugging: Utilize debugging tools to trace the flow of diagnostics from the language server to the Visual Studio environment, checking for any discrepancies in how virtual documents are processed compared to local files.
    4. Consult Documentation: Look into the Visual Studio extensibility documentation for any specific notes regarding the handling of virtual documents with LSP.

    If the issue persists, consider reaching out to the Visual Studio community or forums for additional support, as there may be others who have encountered similar challenges with virtual documents and LSP.

    0 comments No comments

  2. Susmitha T (INFOSYS LIMITED) 1,305 Reputation points Microsoft External Staff
    2025-11-10T08:21:03.04+00:00

    Thanks for reaching out!

     When textDocument/publishDiagnostics messages are received by the Visual Studio LSP client, the diagnostics are normally routed through the Error List and editor Squiggle providers using the document URI as a key.

     

    If your editor window hosts a virtual document (for example, a file opened via IVsUIShell.createDocumentWindow with an in-memory buffer and no actual file path), Visual studio doesn't know how to associate the diagnostics with that buffer.

    Since the URI does not map to a real file on disk or to a workspace-registered text buffer, the diagnostics are silently ignored.

     To enable diagnostics:

    • Register the virtual document's text buffer with the visual studio LSP Client so it's recognized as part of the workspace, or
    • Use a temporary (shadow) file with a valid file:// path or
    • Manually forward diagnostics to the error list using visual studio "ErrorListProvider"

     Reference: Language Server Protocol Overview - Visual Studio (Windows) | Microsoft Learn

     

    Let me know if you need any further help with this. I will be happy to assist.
    If you find this helpful, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.