@@ -33,9 +33,9 @@ export class DebugSessionFeature extends LanguageClientConsumer
3333 implements DebugConfigurationProvider , vscode . DebugAdapterDescriptorFactory {
3434
3535 private sessionCount : number = 1 ;
36- private tempDebugProcess : PowerShellProcess ;
37- private tempSessionDetails : IEditorServicesSessionDetails ;
38- private handlers : vscode . Disposable [ ] ;
36+ private tempDebugProcess : PowerShellProcess | undefined ;
37+ private tempSessionDetails : IEditorServicesSessionDetails | undefined ;
38+ private handlers : vscode . Disposable [ ] = [ ] ;
3939 private configs : Record < DebugConfig , DebugConfiguration > = {
4040 [ DebugConfig . LaunchCurrentFile ] : {
4141 name : "PowerShell: Launch Current File" ,
@@ -68,7 +68,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
6868 super ( ) ;
6969 // Register a debug configuration provider
7070 context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( "PowerShell" , this ) ) ;
71- context . subscriptions . push ( vscode . debug . registerDebugAdapterDescriptorFactory ( "PowerShell" , this ) )
71+ context . subscriptions . push ( vscode . debug . registerDebugAdapterDescriptorFactory ( "PowerShell" , this ) ) ;
7272 }
7373
7474 createDebugAdapterDescriptor (
@@ -79,6 +79,11 @@ export class DebugSessionFeature extends LanguageClientConsumer
7979 ? this . tempSessionDetails
8080 : this . sessionManager . getSessionDetails ( ) ;
8181
82+ if ( sessionDetails === undefined ) {
83+ this . logger . writeAndShowError ( `No session details available for ${ session . name } ` ) ;
84+ return ;
85+ }
86+
8287 this . logger . writeVerbose ( `Connecting to pipe: ${ sessionDetails . debugServicePipeName } ` ) ;
8388 this . logger . writeVerbose ( `Debug configuration: ${ JSON . stringify ( session . configuration ) } ` ) ;
8489
@@ -234,6 +239,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
234239 return undefined ;
235240 }
236241 }
242+
237243 // Check the temporary console setting for untitled documents only, and
238244 // check the document extension for everything else.
239245 if ( config . untitled_document ) {
@@ -248,17 +254,24 @@ export class DebugSessionFeature extends LanguageClientConsumer
248254 return undefined ;
249255 }
250256 }
257+
251258 return config ;
252259 }
253260
254261 private async resolveAttachDebugConfiguration ( config : DebugConfiguration ) : Promise < DebugConfiguration | undefined | null > {
255262 const platformDetails = getPlatformDetails ( ) ;
256263 const versionDetails = this . sessionManager . getPowerShellVersionDetails ( ) ;
264+ if ( versionDetails === undefined ) {
265+ vscode . window . showErrorMessage ( `Session version details were not found for ${ config . name } ` )
266+ return null ;
267+ }
268+
257269 // Cross-platform attach to process was added in 6.2.0-preview.4.
258270 if ( versionDetails . version < "7.0.0" && platformDetails . operatingSystem !== OperatingSystem . Windows ) {
259271 vscode . window . showErrorMessage ( `Attaching to a PowerShell Host Process on ${ OperatingSystem [ platformDetails . operatingSystem ] } requires PowerShell 7.0 or higher.` ) ;
260272 return undefined ;
261273 }
274+
262275 // If nothing is set, prompt for the processId.
263276 if ( ! config . customPipeName && ! config . processId ) {
264277 config . processId = await vscode . commands . executeCommand ( "PowerShell.PickPSHostProcess" ) ;
@@ -267,13 +280,15 @@ export class DebugSessionFeature extends LanguageClientConsumer
267280 return null ;
268281 }
269282 }
283+
270284 if ( ! config . runspaceId && ! config . runspaceName ) {
271285 config . runspaceId = await vscode . commands . executeCommand ( "PowerShell.PickRunspace" , config . processId ) ;
272286 // No runspace selected. Cancel attach.
273287 if ( ! config . runspaceId ) {
274288 return null ;
275289 }
276290 }
291+
277292 return config ;
278293 }
279294}
@@ -341,7 +356,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
341356
342357 private command : vscode . Disposable ;
343358 private waitingForClientToken ?: vscode . CancellationTokenSource ;
344- private getLanguageClientResolve : ( value : LanguageClient ) => void ;
359+ private getLanguageClientResolve ? : ( value : LanguageClient ) => void ;
345360
346361 constructor ( ) {
347362 super ( ) ;
@@ -356,7 +371,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
356371 public setLanguageClient ( languageClient : LanguageClient ) {
357372 this . languageClient = languageClient ;
358373
359- if ( this . waitingForClientToken ) {
374+ if ( this . waitingForClientToken && this . getLanguageClientResolve ) {
360375 this . getLanguageClientResolve ( this . languageClient ) ;
361376 this . clearWaitingToken ( ) ;
362377 }
@@ -367,7 +382,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
367382 }
368383
369384 private getLanguageClient ( ) : Promise < LanguageClient > {
370- if ( this . languageClient ) {
385+ if ( this . languageClient !== undefined ) {
371386 return Promise . resolve ( this . languageClient ) ;
372387 } else {
373388 // If PowerShell isn't finished loading yet, show a loading message
@@ -406,13 +421,14 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
406421 }
407422
408423 private async pickPSHostProcess ( ) : Promise < string | undefined > {
409- const hostProcesses = await this . languageClient . sendRequest ( GetPSHostProcessesRequestType , { } ) ;
410424 // Start with the current PowerShell process in the list.
411425 const items : IProcessItem [ ] = [ {
412426 label : "Current" ,
413427 description : "The current PowerShell Extension process." ,
414428 pid : "current" ,
415429 } ] ;
430+
431+ const hostProcesses = await this . languageClient ?. sendRequest ( GetPSHostProcessesRequestType , { } ) ;
416432 for ( const p in hostProcesses ) {
417433 if ( hostProcesses . hasOwnProperty ( p ) ) {
418434 let windowTitle = "" ;
@@ -427,15 +443,18 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
427443 } ) ;
428444 }
429445 }
446+
430447 if ( items . length === 0 ) {
431448 return Promise . reject ( "There are no PowerShell host processes to attach to." ) ;
432449 }
450+
433451 const options : vscode . QuickPickOptions = {
434452 placeHolder : "Select a PowerShell host process to attach to" ,
435453 matchOnDescription : true ,
436454 matchOnDetail : true ,
437455 } ;
438456 const item = await vscode . window . showQuickPick ( items , options ) ;
457+
439458 return item ? `${ item . pid } ` : undefined ;
440459 }
441460
@@ -462,7 +481,7 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
462481
463482 private command : vscode . Disposable ;
464483 private waitingForClientToken ?: vscode . CancellationTokenSource ;
465- private getLanguageClientResolve : ( value : LanguageClient ) => void ;
484+ private getLanguageClientResolve ? : ( value : LanguageClient ) => void ;
466485
467486 constructor ( ) {
468487 super ( ) ;
@@ -476,7 +495,7 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
476495 public setLanguageClient ( languageClient : LanguageClient ) {
477496 this . languageClient = languageClient ;
478497
479- if ( this . waitingForClientToken ) {
498+ if ( this . waitingForClientToken && this . getLanguageClientResolve ) {
480499 this . getLanguageClientResolve ( this . languageClient ) ;
481500 this . clearWaitingToken ( ) ;
482501 }
@@ -526,9 +545,9 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
526545 }
527546
528547 private async pickRunspace ( processId : string ) : Promise < string | undefined > {
529- const response = await this . languageClient . sendRequest ( GetRunspaceRequestType , { processId } ) ;
548+ const response = await this . languageClient ? .sendRequest ( GetRunspaceRequestType , { processId } ) ;
530549 const items : IRunspaceItem [ ] = [ ] ;
531- for ( const runspace of response ) {
550+ for ( const runspace of response ?? [ ] ) {
532551 // Skip default runspace
533552 if ( ( runspace . id === 1 || runspace . name === "PSAttachRunspace" )
534553 && processId === "current" ) {
@@ -541,12 +560,14 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
541560 id : runspace . id . toString ( ) ,
542561 } ) ;
543562 }
563+
544564 const options : vscode . QuickPickOptions = {
545565 placeHolder : "Select PowerShell runspace to debug" ,
546566 matchOnDescription : true ,
547567 matchOnDetail : true ,
548568 } ;
549569 const item = await vscode . window . showQuickPick ( items , options ) ;
570+
550571 return item ? `${ item . id } ` : undefined ;
551572 }
552573
0 commit comments