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

[Feature] Support restarting the configuration if it is already running. #1

Closed
DJ4ddi opened this issue Jan 18, 2021 · 10 comments
Closed

Comments

@DJ4ddi
Copy link

DJ4ddi commented Jan 18, 2021

It would be great to allow this extension to restart an active debugging session if the configuration is already running when the command is invoked. See microsoft/vscode#114467 for a possible use case.

This could be done in a setting, e.g.

"launches.stopRunningInstance": "no" // Show an error (current behavior, default)
"launches.stopRunningInstance": "yes" // Stop and start the configuration
"launches.stopRunningInstance": "restart" // Restart the configuration (not the same as stop and start for some debuggers)
@DJ4ddi
Copy link
Author

DJ4ddi commented Jan 18, 2021

Turns out, it's not that simple.

First, getting all active debug sessions. The API only exposes the currently active one. So the list would have to be maintained locally using onDidStartDebugSession and onDidTerminateDebugSession.

But even then, it can't easily be restarted. Neither the capabilities (which indicates if a session can be restarted) nor the restart function are exposed.

@ArturoDent
Copy link
Owner

Yes, I've come to the same conclusion with respect to restart. Will probably have to ask for an api for restartDebuggingSession(string|DebugSession) to be added.

vscode.commands.executeCommand('workbench.action.debug.restart') doesn't take arguments either.

Keeping track of the DebugSessions is certainly workable and is how I envisioned implementing stop/start since stopDebugging(session?: DebugSession) only takes a DebugSession.

@ArturoDent
Copy link
Owner

I did discover a dirty hack that seems to work and the commands could be used in a macro extension command.::

await vscode.commands.executeCommand('workbench.debug.action.focusCallStackView');
await vscode.commands.executeCommand('list.collapseAll');
await vscode.commands.executeCommand('list.focusDown');
await vscode.commands.executeCommand('list.select');

await vscode.commands.executeCommand('workbench.action.debug.restart');

Focusing into the call stack and selecting the next debug session changes the activeDebugSession as reflected in the debug widget dropdown. Thus a restart operates on that subsequent debugSession as desired. Obviously, this only works when you have two debug sessions and the one you want to restart is not the currently active session.

Clearly, a proper API is far more desirable but in the meantime...

@ArturoDent
Copy link
Owner

@DJ4ddi Please test the latest version v0.6.5 of this extension for your use case. I haven't updated the README yet.

There is a new setting with three possible values:

"launch.stopRunningInstance": "stop"
"launch.stopRunningInstance": "stop/start"
"launch.stopRunningInstance": "restart"

So, if you have some keybinding to start a debug launch configuration, say:

{
  "key": "alt+g",
  "command": "launches.someFile"
},

If that debug session is still running and you trigger the same keybinding again, the setting will be checked and that session will either be stopped (the default), stopped and started again, or restarted. It seems to be working but if don't have a good way of testing the restart option.

[BTW, how is anyone supposed to figure out the arguments of workbench.action.debug.restart from this link ??].

Also it is weird to be using

vscode.debug.stopDebugging(session)
vscode.debug.startDebugging(someWorkSpaceFolder, someConfigName)

and then

vscode.commands.executeCommand('workbench.action.debug.restart', '', { sessionId: session.id } )

for three such related functions ...

@DJ4ddi
Copy link
Author

DJ4ddi commented Jan 20, 2021

I agree that the VSCode functionality for this is rather... obscure?

The 0.6.5 pulled from the store still shows the There is already a debug configuration "testConfig" running. error. Is that the correct one?

Settings:

"launches": {
    "test-debug": "testConfig (ProjectTemplate)"
},
"launch.stopRunningInstance": "restart",

Called by task:

{
    "label": "watch: reload",
    "type": "shell",
    "command": "${command:launches.test-debug}"
}

@ArturoDent
Copy link
Owner

The arguments passed to a command, like launches.test-debug are very different when that command is started via a task than via a simple keybinding, like:

{
  "key": "alt+g",
  "command": "launches.test-debug",
  "args": "restart"
},

so I had to change a couple of things for that. Please test this now with v0.6.6.

BTW, as you can see above I added the ability to add an arg right to the keybinding, this will override the system-wide setting:

"launch.stopRunningInstance" [which needs to be renamed since it'll do more that stop].

I have started to investigate how to get the args from a task as in :

{
  "label": "watch: reload",
  "type": "shell",
  "command": "${command:launches.test-debug}",
  "args": ["restart"]
},

which would also override the workspace-level setting. But although it does properly trigger the command, it only supplies the command name and workspaceFolder as args - so that is going on my TODO list.

@DJ4ddi
Copy link
Author

DJ4ddi commented Jan 22, 2021

Apologies, it turns out that my folder name was not ProjectTemplate, but Project Template (with a space). I debugged into your code and got it working by replacing the folder regex (/.*\((\w*)\)/) with /.*\(([\w\s]*)\)/ in handleDebugSession.isMatchingDebugSession and handleDebugSession.getMatchingDebugSession. [\w ] should also be enough since tabs and line breaks aren't valid in folder names.

Overriding the restart behavior per configuration is definitely useful, nice job. Arguments for the task command syntax are always a hassle, not sure how to make that work (I usually circumvent it by defining more specific commands).

@ArturoDent
Copy link
Owner

ArturoDent commented Jan 22, 2021

So I assume it works as expected now with your fix? Thanks, I always use TitleCase project names so I missed accounting for spaces.

And did the folder name error ProjectTemplate in

"launches": {
    "test-debug": "testConfig (ProjectTemplate)"
},

arise via intellisense completion or did you manually enter that?

@DJ4ddi
Copy link
Author

DJ4ddi commented Jan 22, 2021

The auto completion worked fine, it was just a mistake in my comment. And yes, working as expected with the spacebar patch.

@ArturoDent
Copy link
Owner

ArturoDent commented Jan 25, 2021

Added the functionality to stop or stop/start or restart a running debug session via a new setting. Added support for a keybinding argument to do the same.

Thanks again for all your help on this.

In v0.6.7.

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