Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 262
Support breakpoints in untitled files in WinPS#2248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Andy Jordan (andyleejordan)
merged 4 commits into
PowerShell:main
from
jborean93:set-breakpoint-5.1Aug 5, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
87 changes: 83 additions & 4 deletions
87 src/PowerShellEditorServices/Services/DebugAdapter/BreakpointService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,6 +18,83 @@ namespace Microsoft.PowerShell.EditorServices.Services | ||
| { | ||
| internal class BreakpointService | ||
| { | ||
| /// <summary> | ||
| /// Code used on WinPS 5.1 to set breakpoints without Script path validation. | ||
| /// It uses reflection because the APIs were not public until 7.0 but just in | ||
| /// case something changes it has a fallback to Set-PSBreakpoint. | ||
| /// </summary> | ||
| private const string _setPSBreakpointLegacy = @" | ||
| [CmdletBinding(DefaultParameterSetName = 'Line')] | ||
| param ( | ||
| [Parameter()] | ||
| [ScriptBlock] | ||
| $Action, | ||
| [Parameter(ParameterSetName = 'Command')] | ||
| [Parameter(ParameterSetName = 'Line', Mandatory = $true)] | ||
| [string] | ||
| $Script, | ||
| [Parameter(ParameterSetName = 'Line')] | ||
| [int] | ||
| $Line, | ||
| [Parameter(ParameterSetName = 'Line')] | ||
| [int] | ||
| $Column, | ||
| [Parameter(ParameterSetName = 'Command', Mandatory = $true)] | ||
| [string] | ||
| $Command | ||
| ) | ||
| if ($Script) { | ||
| # If using Set-PSBreakpoint we need to escape any wildcard patterns. | ||
| $PSBoundParameters['Script'] = [WildcardPattern]::Escape($Script) | ||
| } | ||
| else { | ||
| # WinPS must use null for the Script if unset. | ||
| $Script = [NullString]::Value | ||
| } | ||
| if ($PSCmdlet.ParameterSetName -eq 'Command') { | ||
| $cmdCtor = [System.Management.Automation.CommandBreakpoint].GetConstructor( | ||
| [System.Reflection.BindingFlags]'NonPublic, Public, Instance', | ||
| $null, | ||
| [type[]]@([string], [System.Management.Automation.WildcardPattern], [string], [ScriptBlock]), | ||
| $null) | ||
| if (-not $cmdCtor) { | ||
| Microsoft.PowerShell.Utility\Set-PSBreakpoint @PSBoundParameters | ||
| return | ||
| } | ||
| $pattern = [System.Management.Automation.WildcardPattern]::Get( | ||
| $Command, | ||
| [System.Management.Automation.WildcardOptions]'Compiled, IgnoreCase') | ||
| $b = $cmdCtor.Invoke(@($Script, $pattern, $Command, $Action)) | ||
| } | ||
| else { | ||
| $lineCtor = [System.Management.Automation.LineBreakpoint].GetConstructor( | ||
| [System.Reflection.BindingFlags]'NonPublic, Public, Instance', | ||
| $null, | ||
| [type[]]@([string], [int], [int], [ScriptBlock]), | ||
| $null) | ||
| if (-not $lineCtor) { | ||
| Microsoft.PowerShell.Utility\Set-PSBreakpoint @PSBoundParameters | ||
| return | ||
| } | ||
| $b = $lineCtor.Invoke(@($Script, $Line, $Column, $Action)) | ||
| } | ||
| [Runspace]::DefaultRunspace.Debugger.SetBreakpoints( | ||
| [System.Management.Automation.Breakpoint[]]@($b)) | ||
| $b | ||
| "; | ||
| private readonly ILogger<BreakpointService> _logger; | ||
| private readonly IInternalPowerShellExecutionService _executionService; | ||
| private readonly PsesInternalHost _editorServicesHost; | ||
| @@ -57,7 +134,7 @@ public async Task<IReadOnlyList<Breakpoint>> GetBreakpointsAsync() | ||
| .ConfigureAwait(false); | ||
| } | ||
| public async Task<IReadOnlyList<BreakpointDetails>> SetBreakpointsAsync(string escapedScriptPath, IReadOnlyList<BreakpointDetails> breakpoints) | ||
| public async Task<IReadOnlyList<BreakpointDetails>> SetBreakpointsAsync(IReadOnlyList<BreakpointDetails> breakpoints) | ||
| { | ||
| if (BreakpointApiUtils.SupportsBreakpointApis(_editorServicesHost.CurrentRunspace)) | ||
| { | ||
| @@ -114,9 +191,11 @@ public async Task<IReadOnlyList<BreakpointDetails>> SetBreakpointsAsync(string e | ||
| psCommand.AddStatement(); | ||
| } | ||
| // Don't use Set-PSBreakpoint as that will try and validate the Script | ||
| // path which may or may not exist. | ||
| psCommand | ||
| .AddCommand(@"Microsoft.PowerShell.Utility\Set-PSBreakpoint") | ||
| .AddParameter("Script", escapedScriptPath) | ||
| .AddScript(_setPSBreakpointLegacy, useLocalScope: true) | ||
JustinGrote marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .AddParameter("Script", breakpoint.Source) | ||
| .AddParameter("Line", breakpoint.LineNumber); | ||
| // Check if the user has specified the column number for the breakpoint. | ||
| @@ -184,7 +263,7 @@ public async Task<IReadOnlyList<CommandBreakpointDetails>> SetCommandBreakpoints | ||
| } | ||
| psCommand | ||
| .AddCommand(@"Microsoft.PowerShell.Utility\Set-PSBreakpoint") | ||
| .AddScript(_setPSBreakpointLegacy, useLocalScope: true) | ||
| .AddParameter("Command", breakpoint.Name); | ||
| // Check if this is a "conditional" line breakpoint. | ||
2 changes: 1 addition & 1 deletion
2 src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11 src/PowerShellEditorServices/Services/DebugAdapter/Handlers/BreakpointHandlers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 3 additions & 10 deletions
13 src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ConfigurationDoneHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 18 additions & 8 deletions
26 test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.