Skip to content

Commit 652f7ec

Browse files
BACKPORT: Implement #1611 - provide dynamic debug config (#2084) (#2195)
* Address PR comments, change to single promptClean up debug configuration snippet names & descriptions. Remove Launch Pester Tests debug config snippet. The Launch Scriptsnippet gives an example of invoking Pester plus we have code lens to debug Pester tests. * Remove w/Args prompt debug config snippet * Switch to int id check in provideDebugConfig * Address PR feedback, remove path module as it wasn't being used
1 parent 7e12816 commit 652f7ec

File tree

2 files changed

+104
-106
lines changed

2 files changed

+104
-106
lines changed

package.json

+17-102
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"main": "./out/src/main",
2929
"activationEvents": [
30+
"onDebugInitialConfigurations",
3031
"onDebugResolve:powershell",
3132
"onLanguage:powershell",
3233
"onCommand:PowerShell.NewProjectFromTemplate",
@@ -70,6 +71,11 @@
7071
"test": "node ./node_modules/vscode/bin/test"
7172
},
7273
"contributes": {
74+
"breakpoints": [
75+
{
76+
"language": "powershell"
77+
}
78+
],
7379
"viewsContainers": {
7480
"activitybar": [
7581
{
@@ -315,11 +321,6 @@
315321
{
316322
"type": "PowerShell",
317323
"label": "PowerShell",
318-
"enableBreakpointsFor": {
319-
"languageIds": [
320-
"powershell"
321-
]
322-
},
323324
"program": "./out/src/debugAdapter.js",
324325
"runtime": "node",
325326
"variables": {
@@ -333,87 +334,46 @@
333334
"configurationSnippets": [
334335
{
335336
"label": "PowerShell: Launch Current File",
336-
"description": "Launch current file (in active editor window) under debugger",
337+
"description": "Launch and debug the file in the currently active editor window",
337338
"body": {
338339
"name": "PowerShell Launch Current File",
339340
"type": "PowerShell",
340341
"request": "launch",
341342
"script": "^\"\\${file}\"",
342-
"args": [],
343-
"cwd": "^\"\\${file}\""
344-
}
345-
},
346-
{
347-
"label": "PowerShell: Launch Current File in Temporary Console",
348-
"description": "Launch current file (in active editor window) under debugger in a temporary Integrated Console.",
349-
"body": {
350-
"name": "PowerShell Launch Current File in Temporary Console",
351-
"type": "PowerShell",
352-
"request": "launch",
353-
"script": "^\"\\${file}\"",
354-
"args": [],
355-
"cwd": "^\"\\${file}\"",
356-
"createTemporaryIntegratedConsole": true
357-
}
358-
},
359-
{
360-
"label": "PowerShell: Launch Current File w/Args Prompt",
361-
"description": "Launch current file (in active editor window) under debugger, prompting first for script arguments",
362-
"body": {
363-
"name": "PowerShell Launch Current File w/Args Prompt",
364-
"type": "PowerShell",
365-
"request": "launch",
366-
"script": "^\"\\${file}\"",
367-
"args": [
368-
"^\"\\${command:SpecifyScriptArgs}\""
369-
],
370343
"cwd": "^\"\\${file}\""
371344
}
372345
},
373346
{
374347
"label": "PowerShell: Launch Script",
375-
"description": "Launch specified script or path to script under debugger",
348+
"description": "Launch and debug the specified file or command",
376349
"body": {
377-
"name": "PowerShell Launch ${Script}",
350+
"name": "PowerShell Launch Script",
378351
"type": "PowerShell",
379352
"request": "launch",
380-
"script": "^\"\\${workspaceFolder}/${Script}\"",
381-
"args": [],
353+
"script": "^\"enter path or command to execute e.g.: \\${workspaceFolder}/src/foo.ps1 or Invoke-Pester\"",
382354
"cwd": "^\"\\${workspaceFolder}\""
383355
}
384356
},
385357
{
386-
"label": "PowerShell: Pester Tests",
387-
"description": "Invokes Pester tests under debugger",
358+
"label": "PowerShell: Interactive Session",
359+
"description": "Debug commands executed from the Integrated Console",
388360
"body": {
389-
"name": "PowerShell Pester Tests",
361+
"name": "PowerShell Interactive Session",
390362
"type": "PowerShell",
391363
"request": "launch",
392-
"script": "Invoke-Pester",
393-
"args": [],
394-
"cwd": "^\"\\${workspaceFolder}\""
364+
"cwd": ""
395365
}
396366
},
397367
{
398368
"label": "PowerShell: Attach to PowerShell Host Process",
399-
"description": "Open host process picker to select process to attach debugger to",
369+
"description": "Attach the debugger to a running PowerShell Host Process",
400370
"body": {
401371
"name": "PowerShell Attach to Host Process",
402372
"type": "PowerShell",
403373
"request": "attach",
404374
"runspaceId": 1
405375
}
406376
},
407-
{
408-
"label": "PowerShell: Interactive Session",
409-
"description": "Start interactive session (Debug Console) under debugger",
410-
"body": {
411-
"name": "PowerShell Interactive Session",
412-
"type": "PowerShell",
413-
"request": "launch",
414-
"cwd": ""
415-
}
416-
},
417377
{
418378
"label": "PowerShell: Attach Interactive Session Runspace",
419379
"description": "Open runspace picker to select runspace to attach debugger to",
@@ -434,7 +394,7 @@
434394
},
435395
"args": {
436396
"type": "array",
437-
"description": "Command line arguments to pass to the PowerShell script.",
397+
"description": "Command line arguments to pass to the PowerShell script. Specify \"${command:SpecifyScriptArgs}\" if you want to be prompted for the args.",
438398
"items": {
439399
"type": "string"
440400
},
@@ -484,52 +444,7 @@
484444
}
485445
}
486446
},
487-
"initialConfigurations": [
488-
{
489-
"name": "PowerShell Launch Current File",
490-
"type": "PowerShell",
491-
"request": "launch",
492-
"script": "${file}",
493-
"args": [],
494-
"cwd": "${file}"
495-
},
496-
{
497-
"name": "PowerShell Launch Current File in Temporary Console",
498-
"type": "PowerShell",
499-
"request": "launch",
500-
"script": "${file}",
501-
"args": [],
502-
"cwd": "${file}",
503-
"createTemporaryIntegratedConsole": true
504-
},
505-
{
506-
"name": "PowerShell Launch Current File w/Args Prompt",
507-
"type": "PowerShell",
508-
"request": "launch",
509-
"script": "${file}",
510-
"args": [
511-
"${command:SpecifyScriptArgs}"
512-
],
513-
"cwd": "${file}"
514-
},
515-
{
516-
"name": "PowerShell Attach to Host Process",
517-
"type": "PowerShell",
518-
"request": "attach"
519-
},
520-
{
521-
"name": "PowerShell Interactive Session",
522-
"type": "PowerShell",
523-
"request": "launch",
524-
"cwd": ""
525-
},
526-
{
527-
"name": "PowerShell Attach Interactive Session Runspace",
528-
"type": "PowerShell",
529-
"request": "attach",
530-
"processId": "current"
531-
}
532-
]
447+
"initialConfigurations": []
533448
}
534449
],
535450
"configuration": {

src/features/DebugSession.ts

+87-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import vscode = require("vscode");
66
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider,
7-
ExtensionContext, ProviderResult, WorkspaceFolder } from "vscode";
7+
ExtensionContext, WorkspaceFolder } from "vscode";
88
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
99
import { IFeature } from "../feature";
1010
import { getPlatformDetails, OperatingSystem } from "../platform";
@@ -42,6 +42,89 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
4242
}));
4343
}
4444

45+
public async provideDebugConfigurations(
46+
folder: WorkspaceFolder | undefined,
47+
token?: CancellationToken): Promise<DebugConfiguration[]> {
48+
49+
const launchCurrentFileId = 0;
50+
const launchScriptId = 1;
51+
const interactiveSessionId = 2;
52+
const attachHostProcessId = 3;
53+
54+
const debugConfigPickItems = [
55+
{
56+
id: launchCurrentFileId,
57+
label: "Launch Current File",
58+
description: "Launch and debug the file in the currently active editor window",
59+
},
60+
{
61+
id: launchScriptId,
62+
label: "Launch Script",
63+
description: "Launch and debug the specified file or command",
64+
},
65+
{
66+
id: interactiveSessionId,
67+
label: "Interactive Session",
68+
description: "Debug commands executed from the Integrated Console",
69+
},
70+
{
71+
id: attachHostProcessId,
72+
label: "Attach",
73+
description: "Attach the debugger to a running PowerShell Host Process",
74+
},
75+
];
76+
77+
const launchSelection =
78+
await vscode.window.showQuickPick(
79+
debugConfigPickItems,
80+
{ placeHolder: "Select a PowerShell debug configuration" });
81+
82+
if (launchSelection.id === launchCurrentFileId) {
83+
return [
84+
{
85+
name: "PowerShell: Launch Current File",
86+
type: "PowerShell",
87+
request: "launch",
88+
script: "${file}",
89+
cwd: "${file}",
90+
},
91+
];
92+
}
93+
94+
if (launchSelection.id === launchScriptId) {
95+
return [
96+
{
97+
name: "PowerShell: Launch Script",
98+
type: "PowerShell",
99+
request: "launch",
100+
script: "enter path or command to execute e.g.: ${workspaceFolder}/src/foo.ps1 or Invoke-Pester",
101+
cwd: "${workspaceFolder}",
102+
},
103+
];
104+
}
105+
106+
if (launchSelection.id === interactiveSessionId) {
107+
return [
108+
{
109+
name: "PowerShell: Interactive Session",
110+
type: "PowerShell",
111+
request: "launch",
112+
cwd: "",
113+
},
114+
];
115+
}
116+
117+
// Last remaining possibility is attach to host process
118+
return [
119+
{
120+
name: "PowerShell: Attach to PowerShell Host Process",
121+
type: "PowerShell",
122+
request: "attach",
123+
runspaceId: 1,
124+
},
125+
];
126+
}
127+
45128
// DebugConfigurationProvider method
46129
public async resolveDebugConfiguration(
47130
folder: WorkspaceFolder | undefined,
@@ -161,13 +244,13 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
161244
}
162245

163246
if ((currentDocument.languageId !== "powershell") || !isValidExtension) {
164-
let path = currentDocument.fileName;
247+
let docPath = currentDocument.fileName;
165248
const workspaceRootPath = vscode.workspace.rootPath;
166249
if (currentDocument.fileName.startsWith(workspaceRootPath)) {
167-
path = currentDocument.fileName.substring(vscode.workspace.rootPath.length + 1);
250+
docPath = currentDocument.fileName.substring(vscode.workspace.rootPath.length + 1);
168251
}
169252

170-
const msg = "PowerShell does not support debugging this file type: '" + path + "'.";
253+
const msg = "PowerShell does not support debugging this file type: '" + docPath + "'.";
171254
vscode.window.showErrorMessage(msg);
172255
return;
173256
}

0 commit comments

Comments
 (0)