@@ -18,6 +18,83 @@ namespace Microsoft.PowerShell.EditorServices.Services
1818{
1919 internal class BreakpointService
2020 {
21+ /// <summary>
22+ /// Code used on WinPS 5.1 to set breakpoints without Script path validation.
23+ /// It uses reflection because the APIs were not public until 7.0 but just in
24+ /// case something changes it has a fallback to Set-PSBreakpoint.
25+ /// </summary>
26+ private const string _setPSBreakpointLegacy = @"
27+ [CmdletBinding(DefaultParameterSetName = 'Line')]
28+ param (
29+ [Parameter()]
30+ [ScriptBlock]
31+ $Action,
32+
33+ [Parameter(ParameterSetName = 'Command')]
34+ [Parameter(ParameterSetName = 'Line', Mandatory = $true)]
35+ [string]
36+ $Script,
37+
38+ [Parameter(ParameterSetName = 'Line')]
39+ [int]
40+ $Line,
41+
42+ [Parameter(ParameterSetName = 'Line')]
43+ [int]
44+ $Column,
45+
46+ [Parameter(ParameterSetName = 'Command', Mandatory = $true)]
47+ [string]
48+ $Command
49+ )
50+
51+ if ($Script) {
52+ # If using Set-PSBreakpoint we need to escape any wildcard patterns.
53+ $PSBoundParameters['Script'] = [WildcardPattern]::Escape($Script)
54+ }
55+ else {
56+ # WinPS must use null for the Script if unset.
57+ $Script = [NullString]::Value
58+ }
59+
60+ if ($PSCmdlet.ParameterSetName -eq 'Command') {
61+ $cmdCtor = [System.Management.Automation.CommandBreakpoint].GetConstructor(
62+ [System.Reflection.BindingFlags]'NonPublic, Public, Instance',
63+ $null,
64+ [type[]]@([string], [System.Management.Automation.WildcardPattern], [string], [ScriptBlock]),
65+ $null)
66+
67+ if (-not $cmdCtor) {
68+ Microsoft.PowerShell.Utility\Set-PSBreakpoint @PSBoundParameters
69+ return
70+ }
71+
72+ $pattern = [System.Management.Automation.WildcardPattern]::Get(
73+ $Command,
74+ [System.Management.Automation.WildcardOptions]'Compiled, IgnoreCase')
75+ $b = $cmdCtor.Invoke(@($Script, $pattern, $Command, $Action))
76+ }
77+ else {
78+ $lineCtor = [System.Management.Automation.LineBreakpoint].GetConstructor(
79+ [System.Reflection.BindingFlags]'NonPublic, Public, Instance',
80+ $null,
81+ [type[]]@([string], [int], [int], [ScriptBlock]),
82+ $null)
83+
84+ if (-not $lineCtor) {
85+ Microsoft.PowerShell.Utility\Set-PSBreakpoint @PSBoundParameters
86+ return
87+ }
88+
89+ $b = $lineCtor.Invoke(@($Script, $Line, $Column, $Action))
90+ }
91+
92+ [Runspace]::DefaultRunspace.Debugger.SetBreakpoints(
93+ [System.Management.Automation.Breakpoint[]]@($b))
94+
95+ $b
96+ " ;
97+
2198 private readonly ILogger < BreakpointService > _logger ;
2299 private readonly IInternalPowerShellExecutionService _executionService ;
23100 private readonly PsesInternalHost _editorServicesHost ;
@@ -57,7 +134,7 @@ public async Task<IReadOnlyList<Breakpoint>> GetBreakpointsAsync()
57134 . ConfigureAwait ( false ) ;
58135 }
59136
60- public async Task < IReadOnlyList < BreakpointDetails > > SetBreakpointsAsync ( string escapedScriptPath , IReadOnlyList < BreakpointDetails > breakpoints )
137+ public async Task < IReadOnlyList < BreakpointDetails > > SetBreakpointsAsync ( IReadOnlyList < BreakpointDetails > breakpoints )
61138 {
62139 if ( BreakpointApiUtils . SupportsBreakpointApis ( _editorServicesHost . CurrentRunspace ) )
63140 {
@@ -114,9 +191,11 @@ public async Task<IReadOnlyList<BreakpointDetails>> SetBreakpointsAsync(string e
114191 psCommand . AddStatement ( ) ;
115192 }
116193
194+ // Don't use Set-PSBreakpoint as that will try and validate the Script
195+ // path which may or may not exist.
117196 psCommand
118- . AddCommand ( @"Microsoft.PowerShell.Utility\Set-PSBreakpoint" )
119- . AddParameter ( "Script" , escapedScriptPath )
197+ . AddScript ( _setPSBreakpointLegacy , useLocalScope : true )
198+ . AddParameter ( "Script" , breakpoint . Source )
120199 . AddParameter ( "Line" , breakpoint . LineNumber ) ;
121200
122201 // Check if the user has specified the column number for the breakpoint.
@@ -184,7 +263,7 @@ public async Task<IReadOnlyList<CommandBreakpointDetails>> SetCommandBreakpoints
184263 }
185264
186265 psCommand
187- . AddCommand ( @"Microsoft.PowerShell.Utility\Set-PSBreakpoint" )
266+ . AddScript ( _setPSBreakpointLegacy , useLocalScope : true )
188267 . AddParameter ( "Command" , breakpoint . Name ) ;
189268
190269 // Check if this is a "conditional" line breakpoint.
0 commit comments