Skip to content

Commit dc078e5

Browse files
committed
Send stopDebugger notification when appropriate
1 parent 53b3952 commit dc078e5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+25
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,18 @@ public RunspaceDetails CurrentRunspace
162162
/// </summary>
163163
public string InitialWorkingDirectory { get; private set; }
164164

165+
/// <summary>
166+
/// Tracks the state of the LSP debug server (not the PowerShell debugger).
167+
/// </summary>
165168
internal bool IsDebugServerActive { get; set; }
166169

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+
167177
internal DebuggerStopEventArgs CurrentDebuggerStopEventArgs { get; private set; }
168178

169179
#endregion
@@ -777,6 +787,16 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
777787
await this.sessionStateLock.ReleaseForExecuteCommand().ConfigureAwait(false);
778788
}
779789

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+
780800
if (shell.HadErrors)
781801
{
782802
var strBld = new StringBuilder(1024);
@@ -2422,9 +2442,14 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
24222442
// when the DebugServer is fully started.
24232443
CurrentDebuggerStopEventArgs = e;
24242444

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.
24252449
if (!IsDebugServerActive)
24262450
{
24272451
_languageServer?.SendNotification("powerShell/startDebugger");
2452+
OwnsDebugServerState = true;
24282453
}
24292454

24302455
// We've hit a breakpoint so go to a new line so that the prompt can be rendered.

0 commit comments

Comments
 (0)