@@ -162,8 +162,18 @@ public RunspaceDetails CurrentRunspace
162
162
/// </summary>
163
163
public string InitialWorkingDirectory { get ; private set ; }
164
164
165
+ /// <summary>
166
+ /// Tracks the state of the LSP debug server (not the PowerShell debugger).
167
+ /// </summary>
165
168
internal bool IsDebugServerActive { get ; set ; }
166
169
170
+ /// <summary>
171
+ /// Tracks if the PowerShell session started the debug server itself (true), or if it was
172
+ /// started by an LSP notification (false). Essentially, this marks if we're responsible for
173
+ /// stopping the debug server (and thus need to send a notification to do so).
174
+ /// </summary>
175
+ internal bool OwnsDebugServerState { get ; set ; }
176
+
167
177
internal DebuggerStopEventArgs CurrentDebuggerStopEventArgs { get ; private set ; }
168
178
169
179
#endregion
@@ -777,6 +787,16 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
777
787
await this . sessionStateLock . ReleaseForExecuteCommand ( ) . ConfigureAwait ( false ) ;
778
788
}
779
789
790
+ // This is the edge case where the debug server is running because it was
791
+ // started by PowerShell (and not by an LSP event), and we're no longer in the
792
+ // debugger within PowerShell, so since we own the state we need to stop the
793
+ // debug server too.
794
+ if ( IsDebugServerActive && OwnsDebugServerState && ! shell . IsNested )
795
+ {
796
+ logger . LogDebug ( "Stopping LSP debugger because PowerShell debugger stopped!" ) ;
797
+ _languageServer ? . SendNotification ( "powerShell/stopDebugger" ) ;
798
+ }
799
+
780
800
if ( shell . HadErrors )
781
801
{
782
802
var strBld = new StringBuilder ( 1024 ) ;
@@ -2422,9 +2442,14 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
2422
2442
// when the DebugServer is fully started.
2423
2443
CurrentDebuggerStopEventArgs = e ;
2424
2444
2445
+ // If this event has fired but the LSP debug server is not active, it means that the
2446
+ // PowerShell debugger has started some other way (most likely an existing PSBreakPoint
2447
+ // was executed). So not only do we have to start the server, but later we will be
2448
+ // responsible for stopping it too.
2425
2449
if ( ! IsDebugServerActive )
2426
2450
{
2427
2451
_languageServer ? . SendNotification ( "powerShell/startDebugger" ) ;
2452
+ OwnsDebugServerState = true ;
2428
2453
}
2429
2454
2430
2455
// We've hit a breakpoint so go to a new line so that the prompt can be rendered.
0 commit comments