Powershell: Set-PSBreakpoint allows you to set the same breakpoint again, and again, and again

Created on 11 Jul 2019  路  6Comments  路  Source: PowerShell/PowerShell

The Set-PSBreakpoint command allows you to set a breakpoint multiple times. Most users never encounter this because if they debug and use breakpoints, they do so from Visual Studio Code or PowerShell ISE. For users who use the command line and set breakpoints that way, this behavior leads to additional breakpoints being created that are not necessary and do not add any value to their debugging experience.

Steps to reproduce

Set-PSBreakpoint -Command Get-Process
Set-PSBreakpoint -Command Get-Process
Set-PSBreakpoint -Command Get-Process

Expected behavior

  ID Script                            Line Command                           Variable                          Action
  -- ------                            ---- -------                           --------                          ------
   0                                        Get-Process

Actual behavior

  ID Script                            Line Command                           Variable                          Action
  -- ------                            ---- -------                           --------                          ------
   0                                        Get-Process

  ID Script                            Line Command                           Variable                          Action
  -- ------                            ---- -------                           --------                          ------
   1                                        Get-Process

  ID Script                            Line Command                           Variable                          Action
  -- ------                            ---- -------                           --------                          ------
   2                                        Get-Process

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.1
PSEdition                      Core
GitCommitId                    6.2.1
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Desired outcome

I would like to change this behavior such that Set-PSBreakpoint does not create duplicates, by having it look at existing breakpoints when a breakpoint is set. If an exact match is found, that breakpoint is returned to the caller. Otherwise, the new breakpoint is set and returned to the caller. There is no value in allowing multiple breakpoints that are 100% identical to be set in a debugger. In fact, graphical debuggers don't allow you to do this, so the command line shouldn't either.

Issue-Question WG-Interactive-Debugging

Most helpful comment

In my example it breaks once, but shows three triggering breakpoints that only differ by their ID when it does.

Here's exactly what is shown in the console:

PS C:\Program Files\PowerShell\6> Get-Process
Entering debug mode. Use h or ? for help.

Hit Command breakpoint on 'Get-Process'
Hit Command breakpoint on 'Get-Process'
Hit Command breakpoint on 'Get-Process'

At line:1 char:1
+ Get-Process
+ ~~~~~~~~~~~

[DBG]: PS C:\Program Files\PowerShell\6>> $PSDebugContext.Breakpoints

  ID Script                      Line Command                     Variable                   Action
  -- ------                      ---- -------                     --------                   ------
   0                                  Get-Process
   1                                  Get-Process
   2                                  Get-Process

All 6 comments

@KirkMunro Don't forget about actions. Each breakpoint object can have it's own action q.e.d multiple breakpoints are necessary.

That doesn't address my point. My point was that if you're setting a breakpoint that is already set _the exact same way as an existing breakpoint_ (same line/column/command/variable/script/action), then that should just give you back the breakpoint that is already set. When working from the command line using PSReadline's Ctrl+R or just up arrow to Set-PSBreakpoint commands between sessions, it's easy to set a single breakpoint twice the exact same way. That's what I want to prevent.

Yeah, I'd expect what you're talking about to be a simple idempotent operation. Looks like the impl reflects the desire to have multiple breakpoints with different actions/commands/variable but nobody bothered to check if those actions/command/variable are actually different. BTW, in your example with Get-Process, it doesn't actually break three times, does it?

In my example it breaks once, but shows three triggering breakpoints that only differ by their ID when it does.

Here's exactly what is shown in the console:

PS C:\Program Files\PowerShell\6> Get-Process
Entering debug mode. Use h or ? for help.

Hit Command breakpoint on 'Get-Process'
Hit Command breakpoint on 'Get-Process'
Hit Command breakpoint on 'Get-Process'

At line:1 char:1
+ Get-Process
+ ~~~~~~~~~~~

[DBG]: PS C:\Program Files\PowerShell\6>> $PSDebugContext.Breakpoints

  ID Script                      Line Command                     Variable                   Action
  -- ------                      ---- -------                     --------                   ------
   0                                  Get-Process
   1                                  Get-Process
   2                                  Get-Process

@TylerLeonhardt @rjmholt Perhaps it could be important for VS Code PowerShell extension.

If a fix for this was done at the API layer, then PowerShell Editor Services could be affected.

If a fix for this is done at the cmdlet layer, then PowerShell Editor Services would not need affected.

Thanks for keeping it in mind @iSazonov!

Was this page helpful?
0 / 5 - 0 ratings