user19522235
user19522235

Reputation: 19

How to send correct initialize response to VS Code for an LSP

I am building a Language Server Protocol (LSP) in C# and I'm currently setting up the response to the initialize request. My LSP receives JSON-RPC messages correctly, but I'm encountering an issue when sending the response back to Visual Studio Code.

The relevant part of my code is as follows:

case "initialize":
    var request = new InitializeRequest();

    try
    {
        request = JsonSerializer.Deserialize<InitializeRequest>(content);

        if (request != null)
        {
            logger.Log("Deserialization successful!");
            logger.Log($"ClientInfo Name: {request.Params.ClientInfo.Name}");
            logger.Log($"ClientInfo Version: {request.Params.ClientInfo.Version}");
            InitializeResponse msg = new InitializeResponse("2.0", request.ID);
            var reply = Rpc.EncodeMessage(msg);
            logger.Log("Replying With: " + reply.ToString());
            Console.WriteLine(reply);
            logger.Log("Replied!");
        }
    }
    catch (Exception e)
    {
        logger.Log("Error: " + e.Message);
    }
    break;

The EncodeMessage method is:

public static string EncodeMessage(object message)
{
    try
    {
        string content = JsonSerializer.Serialize(message);
        string header = $"Content-Length: {Encoding.UTF8.GetByteCount(content)}\r\n\r\n";
        string encodedMessage = header + content;
        Program.logger.Log("Serialized Content: " + content);
        Program.logger.Log("Header: " + header);
        Program.logger.Log("Encoded Message: " + encodedMessage);
        return encodedMessage;
    }
    catch (Exception e)
    {
        Program.logger.Log("Error in EncodeMessage: " + e.Message);
        return null;
    }
}

Here is the log output:

2024-06-13 16:36:57 [ScarbroScriptLSP] Content Before Encode: {"result":{"capabilities":{},"serverInfo":{"name":"ScarbroScriptLSP","version":"0.0.1"}},"jsonrpc":"2.0","id":0}
2024-06-13 16:36:57 [ScarbroScriptLSP] Header: Content-Length: 112

2024-06-13 16:36:57 [ScarbroScriptLSP] Encoded Message: Content-Length: 112

{"result":{"capabilities":{},"serverInfo":{"name":"ScarbroScriptLSP","version":"0.0.1"}},"jsonrpc":"2.0","id":0}
2024-06-13 16:36:57 [ScarbroScriptLSP] Replying With: Content-Length: 112

{"result":{"capabilities":{},"serverInfo":{"name":"ScarbroScriptLSP","version":"0.0.1"}},"jsonrpc":"2.0","id":0}
2024-06-13 16:36:57 [ScarbroScriptLSP] Replied!

However, Visual Studio Code throws an error:

[Error - 5:00:31 PM] Client Scarbro Script LSP: connection to server is erroring.
Message header must separate key and value using ':' initialize
Shutting down server.
[Error - 5:00:31 PM] Stopping server failed

I noticed that serverinfo was supposed to be serverInfo but aside from that I don't see any issues with the generated JSON... My only theory was that this part "jsonrpc":"2.0","id":0 must come first?

So it would be

{
    "jsonrpc": "2.0", 
    "id": 0,
    "result" : {
         "capabilities": {},
         "serverInfo": {
              "name": "ScarbroScriptLSP",
              "version": "0.0.1"
         }
    }
}

But surely that's not right? I mean VS Code does do serialization correctly so it shouldn't matter the exact order?

Upvotes: 0

Views: 195

Answers (0)

Related Questions