-
Notifications
You must be signed in to change notification settings - Fork 237
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,74 @@ 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 ($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; | ||
|
@@ -114,8 +182,10 @@ 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") | ||
.AddScript(_setPSBreakpointLegacy, useLocalScope: true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using reflection to access non-public constructors and executing dynamic PowerShell scripts could introduce security risks. Consider adding input validation or restricting the scope where this custom script can be executed. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case the code is only run on WinPS 5.1 which is mostly set in stone. PowerShell 7+ which is a rolling target uses a newer public API not available in the older version. If for whatever reason WinPS is updated and the reflection code is unable to find the ctors, the latest commit has it falling back to |
||
.AddParameter("Script", escapedScriptPath) | ||
.AddParameter("Line", breakpoint.LineNumber); | ||
|
||
|
@@ -184,7 +254,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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The embedded PowerShell script uses reflection to access internal constructors which makes the code brittle to PowerShell internal API changes. Consider adding error handling for cases where these constructors might not be available in future versions.
Copilot uses AI. Check for mistakes.