microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.69k stars 28.69k forks source link

Support debug adapter server over WebSockets (similar to DebugAdapterServer) #176520

Open DanTup opened 1 year ago

DanTup commented 1 year ago

Currently a DebugAdapterDescriptorFactory can return one of:

DebugAdapterServer uses raw sockets, but it would be useful to be able to use WebSockets too (debuggers using WebSockets can be connected to from code running in a browser, and can also host multiple endpoints in a single port by having different URLs).

Alternatively, an implementation that allows us to just provide streams (like how the LSP client can accept a stream reader/writer) would allow a WebSocket version to be implemented fairly easily.

CGNonofr commented 1 year ago

Isn't DebugAdapterInlineImplementation what you're looking for? It is possible to implement a simple implementation using websockets:

class WebsocketDebugAdapterImplementation implements vscode.DebugAdapter {
  constructor (private websocket: WebSocket) {
    websocket.onmessage = (message) => {
      this._onDidSendMessage.fire(JSON.parse(message.data))
    }
  }

  _onDidSendMessage = new vscode.EventEmitter<vscode.DebugProtocolMessage>()
  onDidSendMessage = this._onDidSendMessage.event

  handleMessage (message: vscode.DebugProtocolMessage): void {
    this.websocket.send(JSON.stringify(message))
  }

  dispose () {
    this.websocket.close()
  }
}

new vscode.DebugAdapterInlineImplementation(new WebsocketDebugAdapterImplementation(websocket))
DanTup commented 1 year ago

@CGNonofr hmm, perhaps. But it's not clear to me how that works. The class does not have any methods define like handleMessage, it looks like this:

**
 * A debug adapter descriptor for an inline implementation.
 */
export class DebugAdapterInlineImplementation {

    /**
     * Create a descriptor for an inline implementation of a debug adapter.
     */
    constructor(implementation: DebugAdapter);
}

Is there something missing from this definition that indicates how VS Code would interact with it if I returned an instance of this?

CGNonofr commented 1 year ago

(I've edited my comment, there was some mistakes) what you need to implement is DebugAdapter and provide it to DebugAdapterInlineImplementation

DanTup commented 1 year ago

Ah thanks, that's clearer!

I still think there may be some benefit in having a built-in class for it (for the same reason that DebugAdapterExecutable and DebugAdapterServer exist, which could also be implemented that way), but I'll concede it's a fairly small benefit :-)

roblourens commented 1 year ago

That's what I would recommend for now, but it's probably reasonable for us to let you hand over a websocket url, I can keep this open.