@@ -41,7 +41,8 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
4141 config : DebugConfiguration ,
4242 token ?: CancellationToken ) : ProviderResult < DebugConfiguration > {
4343
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 ;
4546 const debugCurrentScript = ( config . script === "${file}" ) || ! config . request ;
4647 const generateLaunchConfig = ! config . request ;
4748
@@ -85,10 +86,18 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
8586 if ( config . request === "launch" ) {
8687
8788 // 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.
9092 if ( debugCurrentScript ) {
9193
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+
92101 if ( currentDocument . isUntitled ) {
93102 if ( currentDocument . languageId === "powershell" ) {
94103 if ( ! generateLaunchConfig ) {
@@ -118,7 +127,7 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
118127 path = currentDocument . fileName . substring ( vscode . workspace . rootPath . length + 1 ) ;
119128 }
120129
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 + "' .";
122131 vscode . window . showErrorMessage ( msg ) ;
123132 return ;
124133 }
@@ -129,7 +138,7 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
129138 }
130139 }
131140
132- if ( config . cwd === "${file}" ) {
141+ if ( ( currentDocument !== undefined ) && ( config . cwd === "${file}" ) ) {
133142 config . cwd = currentDocument . fileName ;
134143 }
135144
@@ -337,7 +346,9 @@ export class PickPSHostProcessFeature implements IFeature {
337346 } ;
338347
339348 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 ;
341352 } ) ;
342353 } ) ;
343354 }
0 commit comments