@@ -41,7 +41,8 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
41
41
config : DebugConfiguration ,
42
42
token ?: CancellationToken ) : ProviderResult < DebugConfiguration > {
43
43
44
- const currentDocument = vscode . window . activeTextEditor . document ;
44
+ // Starting a debug session can be done when there is no document open e.g. attach to PS host process
45
+ const currentDocument = vscode . window . activeTextEditor ? vscode . window . activeTextEditor . document : undefined ;
45
46
const debugCurrentScript = ( config . script === "${file}" ) || ! config . request ;
46
47
const generateLaunchConfig = ! config . request ;
47
48
@@ -85,10 +86,18 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
85
86
if ( config . request === "launch" ) {
86
87
87
88
// For debug launch of "current script" (saved or unsaved), warn before starting the debugger if either
88
- // A) the unsaved document's language type is not PowerShell or
89
- // B) the saved document's extension is a type that PowerShell can't debug.
89
+ // A) there is not an active document
90
+ // B) the unsaved document's language type is not PowerShell
91
+ // C) the saved document's extension is a type that PowerShell can't debug.
90
92
if ( debugCurrentScript ) {
91
93
94
+ if ( currentDocument === undefined ) {
95
+ const msg = "To debug the \"Current File\", you must first open a " +
96
+ "PowerShell script file in the editor." ;
97
+ vscode . window . showErrorMessage ( msg ) ;
98
+ return ;
99
+ }
100
+
92
101
if ( currentDocument . isUntitled ) {
93
102
if ( currentDocument . languageId === "powershell" ) {
94
103
if ( ! generateLaunchConfig ) {
@@ -118,7 +127,7 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
118
127
path = currentDocument . fileName . substring ( vscode . workspace . rootPath . length + 1 ) ;
119
128
}
120
129
121
- const msg = "'" + path + "' is a file type that cannot be debugged by the PowerShell debugger .";
130
+ const msg = "PowerShell does not support debugging this file type: '" + path + "' .";
122
131
vscode . window . showErrorMessage ( msg ) ;
123
132
return ;
124
133
}
@@ -129,7 +138,7 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
129
138
}
130
139
}
131
140
132
- if ( config . cwd === "${file}" ) {
141
+ if ( ( currentDocument !== undefined ) && ( config . cwd === "${file}" ) ) {
133
142
config . cwd = currentDocument . fileName ;
134
143
}
135
144
@@ -337,7 +346,9 @@ export class PickPSHostProcessFeature implements IFeature {
337
346
} ;
338
347
339
348
return vscode . window . showQuickPick ( items , options ) . then ( ( item ) => {
340
- return item ? item . pid : "" ;
349
+ // Return undefined when user presses Esc.
350
+ // This prevents VSCode from opening launch.json in this case which happens if we return "".
351
+ return item ? `${ item . pid } ` : undefined ;
341
352
} ) ;
342
353
} ) ;
343
354
}
0 commit comments