Skip to content

Commit a3d94ac

Browse files
committed
Clear existing breakpoints in session when debugger starts
This change ensures that the breakpoints the user has in their runspace correspond to the list of breakpoints that are enabled in the debug adapter client. Resolves PowerShell/vscode-powershell#625
1 parent bf58eb6 commit a3d94ac

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+18
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ private async Task HandleInitializeRequest(
182182
object shutdownParams,
183183
RequestContext<InitializeResponseBody> requestContext)
184184
{
185+
// Clear any existing breakpoints before proceeding
186+
await this.ClearSessionBreakpoints();
187+
185188
// Now send the Initialize response to continue setup
186189
await requestContext.SendResult(
187190
new InitializeResponseBody {
@@ -420,6 +423,9 @@ await requestContext.SendError(
420423
return;
421424
}
422425

426+
// Clear any existing breakpoints before proceeding
427+
await this.ClearSessionBreakpoints();
428+
423429
// Execute the Debug-Runspace command but don't await it because it
424430
// will block the debug adapter initialization process. The
425431
// InitializedEvent will be sent as soon as the RunspaceChanged
@@ -883,6 +889,18 @@ private void UnregisterEventHandlers()
883889
this.editorSession.PowerShellContext.DebuggerResumed -= this.powerShellContext_DebuggerResumed;
884890
}
885891

892+
private async Task ClearSessionBreakpoints()
893+
{
894+
try
895+
{
896+
await this.editorSession.DebugService.ClearAllBreakpoints();
897+
}
898+
catch (Exception e)
899+
{
900+
Logger.WriteException("Caught exception while clearing breakpoints from session", e);
901+
}
902+
}
903+
886904
#endregion
887905

888906
#region Event Handlers

src/PowerShellEditorServices/Debugging/DebugService.cs

+12
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,18 @@ public VariableScope[] GetVariableScopes(int stackFrameId)
660660
};
661661
}
662662

663+
/// <summary>
664+
/// Clears all breakpoints in the current session.
665+
/// </summary>
666+
public async Task ClearAllBreakpoints()
667+
{
668+
PSCommand psCommand = new PSCommand();
669+
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Get-PSBreakpoint");
670+
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Remove-PSBreakpoint");
671+
672+
await this.powerShellContext.ExecuteCommand<object>(psCommand);
673+
}
674+
663675
#endregion
664676

665677
#region Private Methods

0 commit comments

Comments
 (0)