Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to start a debugging session via API ? #139

Closed
ayoub-benali opened this issue Dec 12, 2021 · 4 comments
Closed

How to start a debugging session via API ? #139

ayoub-benali opened this issue Dec 12, 2021 · 4 comments

Comments

@ayoub-benali
Copy link

ayoub-benali commented Dec 12, 2021

I am trying to integrate in https://github.com/scalameta/metals-sublime with this package.
It is a helper package for Metals and already takes care of the installation. The debug workflow get started via code lenses, so as far as I understand there isn't a need to implement the adapter class.

I already have a host, port and the other debug parameters but I couldn't find a way to pass this info to the debugger and start it.
Can you point me to which API command with its argument I should use ?

@daveleroy
Copy link
Owner

daveleroy commented Dec 12, 2021

This package is not designed to be used externally like LSP there is no API for other packages to consume right now. All of the adapters live here https://github.com/daveleroy/sublime_debugger/tree/master/modules/adapters

The AdapterConfiguration is how every debug adapter is registered with the system there is no way to do what you want which is basically to give this package an already running debug adapter and a configuration to use with it.

This is the flow I think you should start with https://scalameta.org/metals/docs/editors/vscode/#via-a-launchjson-configuration

AdapterConfiguration does basically two main things

  1. It handles installing an adapter (install(...), installed_status(...), installed_version(...))
  2. Starting a debug adapter (start(...)) when a user tries to run a configuration for that adapter (specified via the type field)

For 1. You can probably check that https://github.com/scalameta/metals-sublime is installed or something.

For 2. You probably need to ask the metals LSP to start the debug adapter because I guess it is part of the LSP client and it gives you a host/port which you would return with SocketTransport. This seems to be the LSP command https://github.com/scalameta/metals-languageclient/blob/87425244724013a92ac5f442fc8e199eb89212d1/src/interfaces/ServerCommands.ts#L64

After that is working code lenses probably just give a configuration to run so something like the following command could be added

window.run_command('debugger', {
	'action': 'start',
	'configuration': {
		...
	}
})

That command would work just like if the user has that configuration in their .sublime-project

@daveleroy
Copy link
Owner

The debugger now supports starting via a configuration with both start and open_and_start commands. Still need an adapter to handle the configuration type.

window.run_command('debugger', {
	'action': 'open_and_start'/'start',
	'configuration': {
		...
	}
})```

@ayoub-benali
Copy link
Author

@daveleroy thanks for the update, I am giving this issue another try and so far I managed to:

  1. Capture the code lens click event
  2. call Metals debug-adapter-start with the code lens arguments
  3. get back an object like this {'uri': 'tcp://127.0.0.1:65264', 'name': 'example.Hello'}
  4. run window.run_command('debugger', { 'action': 'open_and_start'/'start', 'configuration': { ... } })
  5. Notice the error: Unable to find debug adapter with the type name "None"
  • As long as I create an instance of AdapterConfiguration for Metals in this repo, is this the right approach ?
  • Also what are the values that I am supposed to put in the configuration object ?

cc @prassee

@daveleroy
Copy link
Owner

@daveleroy thanks for the update, I am giving this issue another try and so far I managed to:

  1. Capture the code lens click event
  2. call Metals debug-adapter-start with the code lens arguments
  3. get back an object like this {'uri': 'tcp://127.0.0.1:65264', 'name': 'example.Hello'}
  4. run window.run_command('debugger', { 'action': 'open_and_start'/'start', 'configuration': { ... } })
  5. Notice the error: Unable to find debug adapter with the type name "None"
  • As long as I create an instance of AdapterConfiguration for Metals in this repo, is this the right approach ?
  • Also what are the values that I am supposed to put in the configuration object ?

cc @prassee

https://github.com/scalameta/metals-vscode#via-a-launchjson-configuration that is the standard way to setup debugging through a configuration and I would start by trying to get this setup and working first and then adding support for the code lens stuff.

Yes you need an AdapterConfiguration and your configuration needs a type field which is used to route it to a specific AdapterConfiguration so it can decide how to setup the transport for the debug adapter protocol. A request field of either launch or attach to specify what debug adapter request to make and a name field. Other configuration fields are adapter specific and get sent to the adapter during the launch/attach request.

They seem to just pass in a dummy configuration for vscode for code lenses here https://github.com/scalameta/metals-vscode/blob/821c44598870d9f6cabc7f28b788401eb7e3e0a7/src/debugger/scalaDebugger.ts#L136

I believe the the code lens is the configuration just missing a type, request and name so presumably you could add those to the configuration and the AdapterConfiguration starts the adapter with the lsp debug-adapter-start request so it actually supports normal configurations not just code lens stuff.

I believe you probably want something like the following on the Debugger side

class Scala(dap.AdapterConfiguration):
	type = 'scala'
	docs = 'https://github.com/scalameta/metals-vscode#readme'

	installer = util.GitInstaller(
		type='scala',
		repo='scalameta/metals-vscode'
	)

	async def start(self, log, configuration):
		# Make sure LSP and LSP-metals are installed
		util.require_package('LSP-metals')
		util.require_package('LSP')

		response = await util.lsp.request('metals', 'workspace/executeCommand', {
			'command': 'debug-adapter-start',
			'arguments': configuration,
		})

		uri: str = response['uri']
		name: str = response['name']

		address, port = uri.split('tcp://')[1].split(':')

		configuration.name = name

		return dap.SocketTransport(log, address, int(port))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants