Skip to content

Add Debug Runspace command #1782

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

Merged
merged 6 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"onCommand:PowerShell.NewProjectFromTemplate",
"onCommand:PowerShell.OpenExamplesFolder",
"onCommand:PowerShell.PickPSHostProcess",
"onCommand:PowerShell.PickRunspace",
"onCommand:PowerShell.SpecifyScriptArgs",
"onCommand:PowerShell.ShowSessionConsole",
"onCommand:PowerShell.ShowSessionMenu",
Expand Down Expand Up @@ -318,6 +319,7 @@
"runtime": "node",
"variables": {
"PickPSHostProcess": "PowerShell.PickPSHostProcess",
"PickRunspace": "PowerShell.PickRunspace",
"SpecifyScriptArgs": "PowerShell.SpecifyScriptArgs"
},
"languages": [
Expand Down Expand Up @@ -406,6 +408,17 @@
"request": "launch",
"cwd": ""
}
},
{
"label": "PowerShell: Attach Interactive Session Runspace",
"description": "Open runspace picker to select runspace to attach debugger to",
"body": {
"name": "PowerShell Attach Interactive Session Runspace",
"type": "PowerShell",
"request": "attach",
"processId": "current",
"runspaceId": "^\"\\${command:PickRunspace}\""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}
],
"configurationAttributes": {
Expand Down Expand Up @@ -447,9 +460,9 @@
"default": null
},
"runspaceId": {
"type": "number",
"type": "string",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wish it were possible to do like string | int for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually... it looks like you can do:

"type":["string","number"]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamdriscoll I think you maybe missed this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. In there now.

"description": "Optional: The ID of the runspace to debug in the attached process. Defaults to 1. Works only on PowerShell 5 and above.",
"default": 1
"default": "1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the things I did in #1775 was made the default for processId to be null and then prompt for it when resolving the debug config:

if (!config.customPipeName && !config.processId) {
config.processId = await vscode.commands.executeCommand("PowerShell.PickPSHostProcess");
}

@rkeithhill @rjmholt if we want to make prompting for PickRunspace to be the default, it should probably be implemented in a similar fashion.

},
"customPipeName": {
"type": "string",
Expand Down Expand Up @@ -491,13 +504,20 @@
"name": "PowerShell Attach to Host Process",
"type": "PowerShell",
"request": "attach",
"runspaceId": 1
"runspaceId": "1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should go ahead and make this ${command:PickRunspace} now?

thoughts @rkeithhill @rjmholt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would the workflow be 1) query PSES for host processes 2) user picks host process, 3) query PSES for runspaces in the selected host process and 4) user picks runspace? If so, that sounds pretty good to me. Although I have a feeling the vast majority of time the user will pick runspace 1. If they get tired of the prompt, I guess then could then hard-code 1 into the debug config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! Ok. Lets do that. @adamdriscoll can you follow #1775 and implement it in a similar fashion?

Ideally, the user should not have to put anything for runspaceId and it will prompt them for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After starting to implement this I realized I needed to update the request to pass in a process ID for the request runspace command. I kinda got that working but am seeing weird stuff where its complaining about null values and stating the "Attach to Host Process" debugger is already running when I cancel out of the pickers.

I will revisit tomorrow but it's not as simple as I thought at first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if there's anything you need from us :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this working. Updated this PR and the PSES PR to support process ID and to pop up the picker in the same place as the process picker.

},
{
"name": "PowerShell Interactive Session",
"type": "PowerShell",
"request": "launch",
"cwd": ""
},
{
"name": "PowerShell Attach Interactive Session Runspace",
"type": "PowerShell",
"request": "attach",
"processId": "current",
"runspaceId": "${command:PickRunspace}"
}
]
}
Expand Down
127 changes: 127 additions & 0 deletions src/features/DebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,130 @@ export class PickPSHostProcessFeature implements IFeature {
}
}
}

interface IRunspaceItem extends vscode.QuickPickItem {
id: string; // payload for the QuickPick UI
}

interface IRunspace {
id: string;
name: string;
availability: string;
}

export const GetRunspaceRequestType =
new RequestType<any, IGetRunspaceResponseBody, string, void>("powerShell/getRunspace");

interface IGetRunspaceResponseBody {
runspaces: IRunspace[];
}

export class PickRunspaceFeature implements IFeature {

private command: vscode.Disposable;
private languageClient: LanguageClient;
private waitingForClientToken: vscode.CancellationTokenSource;
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void;

constructor() {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can probably get rid of this newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

this.command =
vscode.commands.registerCommand("PowerShell.PickRunspace", () => {
return this.getLanguageClient()
.then((_) => this.pickRunspace(), (_) => undefined);
});
}

public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;

if (this.waitingForClientToken) {
this.getLanguageClientResolve(this.languageClient);
this.clearWaitingToken();
}
}

public dispose() {
this.command.dispose();
}

private getLanguageClient(): Thenable<LanguageClient> {
if (this.languageClient) {
return Promise.resolve(this.languageClient);
} else {
// If PowerShell isn't finished loading yet, show a loading message
// until the LanguageClient is passed on to us
this.waitingForClientToken = new vscode.CancellationTokenSource();

return new Promise<LanguageClient>(
(resolve, reject) => {
this.getLanguageClientResolve = resolve;

vscode.window
.showQuickPick(
["Cancel"],
{ placeHolder: "Attach to PowerShell host process: Please wait, starting PowerShell..." },
this.waitingForClientToken.token)
.then((response) => {
if (response === "Cancel") {
this.clearWaitingToken();
reject();
}
});

// Cancel the loading prompt after 60 seconds
setTimeout(() => {
if (this.waitingForClientToken) {
this.clearWaitingToken();
reject();

vscode.window.showErrorMessage(
"Attach to PowerShell host process: PowerShell session took too long to start.");
}
}, 60000);
},
);
}
}

private pickRunspace(): Thenable<string> {
return this.languageClient.sendRequest(GetRunspaceRequestType, null).then((runspaces) => {
const items: IRunspaceItem[] = [];

for (const p in runspaces) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably switch this to a for...of loop then you don't have to index, you can just do:

for (const runspace of runspaces)

and runspace is an IRunspaceItem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest commit fixes this. There was actually a mismatch in the PSES response type and the VS Code response type.

if (runspaces.hasOwnProperty(p)) {

// Skip default runspace
if (runspaces[p].id === 1) {
continue;
}

items.push({
label: runspaces[p].name,
description: `ID: ${runspaces[p].id} - ${runspaces[p].availability}`,
id: runspaces[p].id,
});
}
}

const options: vscode.QuickPickOptions = {
placeHolder: "Select PowerShell runspace to debug",
matchOnDescription: true,
matchOnDetail: true,
};

return vscode.window.showQuickPick(items, options).then((item) => {
// Return undefined when user presses Esc.
// This prevents VSCode from opening launch.json in this case which happens if we return "".
return item ? `${item.id}` : undefined;
});
});
}

private clearWaitingToken() {
if (this.waitingForClientToken) {
this.waitingForClientToken.dispose();
this.waitingForClientToken = undefined;
}
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ConsoleFeature } from "./features/Console";
import { CustomViewsFeature } from "./features/CustomViews";
import { DebugSessionFeature } from "./features/DebugSession";
import { PickPSHostProcessFeature } from "./features/DebugSession";
import { PickRunspaceFeature } from "./features/DebugSession";
import { SpecifyScriptArgsFeature } from "./features/DebugSession";
import { DocumentFormatterFeature } from "./features/DocumentFormatter";
import { ExamplesFeature } from "./features/Examples";
Expand Down Expand Up @@ -142,6 +143,7 @@ export function activate(context: vscode.ExtensionContext): void {
new SpecifyScriptArgsFeature(context),
new HelpCompletionFeature(logger),
new CustomViewsFeature(),
new PickRunspaceFeature(),
];

sessionManager.setExtensionFeatures(extensionFeatures);
Expand Down