eclipse-lsp4j / lsp4j

A Java implementation of the language server protocol intended to be consumed by tools and language servers implemented in Java.
https://eclipse.org/lsp4j
Other
581 stars 141 forks source link

Unable to identify the input message #793

Closed federicocarboni closed 5 months ago

federicocarboni commented 5 months ago

I'm trying to make a DAP server over TCP but it crashes as soon as it receives an initialize request from VSCode.

Here's the code I'm using to start the server:

try (ServerSocket serverSocket = new ServerSocket(port)) {
    Socket socket = serverSocket.accept();
    Launcher<IDebugProtocolClient> launcher = Launcher.createLauncher(server, IDebugProtocolClient.class, socket.getInputStream(), socket.getOutputStream());
    launcher.startListening().get();
} catch (Exception ex) {
    LOGGER.error(ex.toString());
}

And the stack trace of the error:

Feb 07, 2024 5:05:11 PM org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer fireError
SEVERE: Unable to identify the input message.
com.google.gson.JsonParseException: Unable to identify the input message.
    at org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.createMessage(MessageTypeAdapter.java:403)
    at org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.read(MessageTypeAdapter.java:139)
    at org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.read(MessageTypeAdapter.java:56)
    at com.google.gson.Gson.fromJson(Gson.java:1227)
    at com.google.gson.Gson.fromJson(Gson.java:1186)
    at org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler.parseMessage(MessageJsonHandler.java:119)
    at org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler.parseMessage(MessageJsonHandler.java:114)
    at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:193)
    at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
    at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:840)

And here's the raw JSONRPC message sent by VSCode:

Content-Length: 505

{"command":"initialize","arguments":{"clientID":"vscode","clientName":"VSCodium","adapterID":"scarpet","pathFormat":"path","linesStartAt1":true,"columnsStartAt1":true,"supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"locale":"en","supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryReferences":true,"supportsArgsCanBeInterpretedByShell":true,"supportsMemoryEvent":true,"supportsStartDebuggingRequest":true},"type":"request","seq":1}

Maybe I'm doing something completely wrong, can someone explain why this is happening?

jonahgraham commented 5 months ago
`Launcher<IDebugProtocolClient> launcher = Launcher.createLauncher(server, IDebugProtocolClient.class, socket.getInputStream(), socket.getOutputStream());`

The issue is using Launcher which is the non-debug specific version and handles JSON-RPC as defined for LSP version of the protocol. You want to use the DebugLauncher to talk the DAP version of the JSON-RPC protocol.

But you may want the higher level DSPLauncher which handles the types too.

If it helps to understand further, the message DAP sends looks like this:

{"command":"initialize","arguments": [...] "type":"request","seq":1}

But LSP (the one Launcher understands) looks like this:

{"jsonrpc": "2.0", "id": 42, "method": "askServer", "params": [...]}

The DAP is not actually a JSON-RPC format, but it is quite similar.


Please feel free to raise additional issues if you have further questions.

federicocarboni commented 5 months ago

Thanks for the quick response, it works now :+1: