Visual Studio (not VSCode) Language Server Protocol Failing Immediately with ConnectionLostException (using Clangd as server)


Has anybody had any success with getting Visual Studio (NOT VSCode) working with clangd?
I have been trying to follow the guide at https://learn.microsoft.com/en-us/visualstudio/extensibility/adding-an-lsp-extension?view=vs-2022 to create an extension for Visual Studio to allow it to work with clangd.

It builds and launches okay, and whenever I drag+drop a file of this custom content type into visual studio, I can see the ActivateAsync() function fires and returns successfully, but then I IMMEDIATELY (no timeout) get a popup saying a “ConnectionLostException” occurred in my extension. The log it opens up says

StreamJsonRpc.ConnectionLostException: The JSON-RPC connection with the remote party was lost before the request could complete. ---> System.OperationCanceledException: The operation was canceled.

I have tried printing out the contents of the input and output streams, and this is what they show:

server intput stream

{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "requestServiceChannel",
    "params": [
        {
            "Name": "vs-intellicode-models64"
        },
        {
            "ActivationArguments": {
                "__servicehub__ServiceHubRemoteServiceBrokerPipeName": "3125c421d85e4d4393f24401ddcb05cb"
            },
            "ClientCulture": "en-US",
            "ClientUICulture": "en-US"
        }
    ]
}

server output stream:

{
    "traceparent": "00-6686150fa02fb943b3516c607425492d-10f269feee44fc48-01",
    "jsonrpc": "2.0",
    "id": 2,
    "method": "initialize",
    "params": {
        "processId": 10216,
        "rootUri": null,
        "capabilities": {
            "_vs_supportsVisualStudioExtensions": true,
            "_vs_supportedSnippetVersion": 1,
            "_vs_supportsIconExtensions": true,
            "_vs_supportsDiagnosticRequests": true,
            "workspace": {
                "applyEdit": true,
                "workspaceEdit": {
                    "documentChanges": true,
                    "resourceOperations": [
                    ],
                    "normalizesLineEndings": true
                },
                "didChangeConfiguration": {
                },
                "didChangeWatchedFiles": {
                },
                "symbol": {
                },
                "executeCommand": {
                    "_vs_supportedCommands": [
                        "_ms_setClipboard",
                        "_ms_openUrl"
                    ]
                },
                "semanticTokens": {
                    "refreshSupport": true
                },
                "diagnostics": {
                    "refreshSupport": true
                },
                "inlayHint": {
                    "refreshSupport": true
                }
            },
            "textDocument": {
                "_vs_onAutoInsert": {
                    "dynamicRegistration": true
                },
                "synchronization": {
                    "didSave": true,
                    "dynamicRegistration": true
                },
                "completion": {
                    "_vs_completionList": {
                        "_vs_data": true,
                        "_vs_commitCharacters": true
                    },
                    "completionItem": {
                    },
                    "completionItemKind": {
                        "valueSet": [
                            0,
                            1,
                            2,
                            3,
                            4,
                            5,
                            6,
                            7,
                            8,
                            9,
                            10,
                            11,
                            12,
                            13,
                            14,
                            15,
                            16,
                            17,
                            18,
                            19,
                            20,
                            21,
                            22,
                            23,
                            24,
                            25,
                            118115,
                            118116,
                            118117,
                            118118,
                            118119,
                            118120,
                            118121,
                            118122,
                            118123,
                            118124,
                            118125,
                            118126
                        ]
                    },
                    "insertTextMode": 1,
                    "completionList": {
                        "itemDefaults": [
                            "commitCharacters",
                            "editRange",
                            "insertTextFormat"
                        ]
                    },
                    "dynamicRegistration": true
                },
                "hover": {
                    "contentFormat": [
                        "plaintext"
                    ],
                    "dynamicRegistration": true
                },
                "signatureHelp": {
                    "signatureInformation": {
                        "documentationFormat": [
                            "plaintext"
                        ],
                        "parameterInformation": {
                            "labelOffsetSupport": true
                        }
                    },
                    "contextSupport": true,
                    "dynamicRegistration": true
                },
                "definition": {
                    "dynamicRegistration": true
                },
                "typeDefinition": {
                    "dynamicRegistration": true
                },
                "implementation": {
                    "dynamicRegistration": true
                },
                "references": {
                    "dynamicRegistration": true
                },
                "documentHighlight": {
                    "dynamicRegistration": true
                },
                "documentSymbol": {
                    "dynamicRegistration": true
                },
                "codeAction": {
                    "codeActionLiteralSupport": {
                        "_vs_codeActionGroup": {
                            "_vs_valueSet": [
                                "quickfix",
                                "refactor",
                                "refactor.extract",
                                "refactor.inline",
                                "refactor.rewrite",
                                "source",
                                "source.organizeImports"
                            ]
                        },
                        "codeActionKind": {
                            "valueSet": [
                                "quickfix",
                                "refactor",
                                "refactor.extract",
                                "refactor.inline",
                                "refactor.rewrite",
                                "source",
                                "source.organizeImports"
                            ]
                        }
                    },
                    "resolveSupport": {
                        "properties": [
                            "additionalTextEdits",
                            "command",
                            "commitCharacters",
                            "description",
                            "detail",
                            "documentation",
                            "insertText",
                            "insertTextFormat",
                            "label"
                        ]
                    },
                    "dataSupport": true,
                    "dynamicRegistration": true
                },
                "codeLens": {
                    "dynamicRegistration": true
                },
                "documentLink": {
                    "dynamicRegistration": true
                },
                "formatting": {
                    "dynamicRegistration": true
                },
                "rangeFormatting": {
                    "dynamicRegistration": true
                },
                "onTypeFormatting": {
                    "dynamicRegistration": true
                },
                "rename": {
                    "prepareSupport": true,
                    "prepareSupportDefaultBehavior": 1,
                    "dynamicRegistration": true
                },
                "publishDiagnostics": {
                    "tagSupport": {
                        "valueSet": [
                            1,
                            2,
                            2147483646,
                            2147483645
                        ]
                    }
                },
                "foldingRange": {
                    "_vs_refreshSupport": true,
                    "foldingRange": {
                        "collapsedText": true
                    },
                    "dynamicRegistration": true
                },
                "linkedEditingRange": {
                    "dynamicRegistration": true
                },
                "semanticTokens": {
                    "requests": {
                        "range": true,
                        "full": {
                            "delta": true
                        }
                    },
                    "tokenTypes": [
                        "namespace",
                        "type",
                        "class",
                        "enum",
                        "interface",
                        "struct",
                        "typeParameter",
                        "parameter",
                        "variable",
                        "property",
                        "enumMember",
                        "event",
                        "function",
                        "method",
                        "macro",
                        "keyword",
                        "modifier",
                        "comment",
                        "string",
                        "number",
                        "regexp",
                        "operator",
                        "cppMacro",
                        "cppEnumerator",
                        "cppGlobalVariable",
                        "cppLocalVariable",
                        "cppParameter",
                        "cppType",
                        "cppRefType",
                        "cppValueType",
                        "cppFunction",
                        "cppMemberFunction",
                        "cppMemberField",
                        "cppStaticMemberFunction",
                        "cppStaticMemberField",
                        "cppProperty",
                        "cppEvent",
                        "cppClassTemplate",
                        "cppGenericType",
                        "cppFunctionTemplate",
                        "cppNamespace",
                        "cppLabel",
                        "cppUserDefinedLiteralRaw",
                        "cppUserDefinedLiteralNumber",
                        "cppUserDefinedLiteralString",
                        "cppOperator",
                        "cppMemberOperator",
                        "cppNewDelete"
                    ],
                    "tokenModifiers": [
                        "declaration",
                        "definition",
                        "readonly",
                        "static",
                        "deprecated",
                        "abstract",
                        "async",
                        "modification",
                        "documentation",
                        "defaultLibrary"
                    ],
                    "formats": [
                        "relative"
                    ],
                    "dynamicRegistration": true
                },
                "diagnostic": {
                    "dynamicRegistration": true
                },
                "inlayHint": {
                    "dynamicRegistration": true
                }
            }
        }
    }
}

…but nothing in them really sticks out to me. These are the only two inputs/outputs — it doesn’t fire off any more before the exception occurs.

I haven’t been able to find much of use on Google about this. I found an unresolved StackOverflow post dealing with what sounds like the exact same issue, but with a different language.
Why my language server loses the JSON-RPC connection with the client in Visual Studio?

Are there any extra diagnostics I can enable in Visual studio to shed some light on this? I’m getting close to the end of my rope on this.

Leave a Reply

Your email address will not be published. Required fields are marked *