From b126eda7b4402f927bc851d9aab923a32984a8b8 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Fri, 8 Sep 2023 12:57:25 -0700 Subject: [PATCH] Fix debugging script blocks that aren't in files The code relied on running `list 1 ` but that wasn't being run in the debug context because we allow-list commands to prevent pollution of the history, and missed it. Like the `prompt` and interactive commands (which `list` could be but is not when we run it) we need to check for this exact `list` command and run it under the debugger. Moreover, we also weren't locking the `debugInfoHandle`, nor were we correctly checking if `scriptListingLines` was empty (it was never null), and our shortcut to skip allocation was broken. Actually we can't skip allocation, but we can at least skip superfluous conversions. --- .../Services/DebugAdapter/DebugService.cs | 21 ++++++++++++++----- .../Execution/SynchronousPowerShellTask.cs | 14 ++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs b/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs index 0439f44844..9328e54408 100644 --- a/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs +++ b/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs @@ -930,13 +930,24 @@ internal async void OnDebuggerStopAsync(object sender, DebuggerStopEventArgs e) if (_remoteFileManager is not null && string.IsNullOrEmpty(localScriptPath)) { // Get the current script listing and create the buffer - PSCommand command = new PSCommand().AddScript($"list 1 {int.MaxValue}"); + IReadOnlyList scriptListingLines; + await debugInfoHandle.WaitAsync().ConfigureAwait(false); + try + { + // This command must be run through `ExecuteInDebugger`! + PSCommand psCommand = new PSCommand().AddScript($"list 1 {int.MaxValue}"); - IReadOnlyList scriptListingLines = - await _executionService.ExecutePSCommandAsync( - command, CancellationToken.None).ConfigureAwait(false); + scriptListingLines = + await _executionService.ExecutePSCommandAsync( + psCommand, + CancellationToken.None).ConfigureAwait(false); + } + finally + { + debugInfoHandle.Release(); + } - if (scriptListingLines is not null) + if (scriptListingLines.Count > 0) { int linePrefixLength = 0; diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs index c7a983bd82..c72aca4f6d 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs @@ -75,7 +75,7 @@ public override IReadOnlyList Run(CancellationToken cancellationToken) // the latter we must not send through it else they pollute the history as this // PowerShell API does not let us exclude them from it. return _pwsh.Runspace.Debugger.InBreakpoint - && (PowerShellExecutionOptions.AddToHistory || IsPromptCommand(_psCommand) || _pwsh.Runspace.RunspaceIsRemote) + && (PowerShellExecutionOptions.AddToHistory || IsPromptOrListCommand(_psCommand) || _pwsh.Runspace.RunspaceIsRemote) ? ExecuteInDebugger(cancellationToken) : ExecuteNormally(cancellationToken); } @@ -87,7 +87,7 @@ public override IReadOnlyList Run(CancellationToken cancellationToken) public override string ToString() => _psCommand.GetInvocationText(); - private static bool IsPromptCommand(PSCommand command) + private static bool IsPromptOrListCommand(PSCommand command) { if (command.Commands.Count is not 1 || command.Commands[0] is { IsScript: false } or { Parameters.Count: > 0 }) @@ -96,7 +96,8 @@ private static bool IsPromptCommand(PSCommand command) } string commandText = command.Commands[0].CommandText; - return commandText.Equals("prompt", StringComparison.OrdinalIgnoreCase); + return commandText.Equals("prompt", StringComparison.OrdinalIgnoreCase) + || commandText.Equals($"list 1 {int.MaxValue}", StringComparison.OrdinalIgnoreCase); } private IReadOnlyList ExecuteNormally(CancellationToken cancellationToken) @@ -301,11 +302,10 @@ private IReadOnlyList ExecuteInDebugger(CancellationToken cancellationT return Array.Empty(); } - // If we've been asked for a PSObject, no need to allocate a new collection - if (typeof(TResult) == typeof(PSObject) - && outputCollection is IReadOnlyList resultCollection) + // If we've been asked for a PSObject, no need to convert + if (typeof(TResult) == typeof(PSObject)) { - return resultCollection; + return new List(outputCollection) as IReadOnlyList; } // Otherwise, convert things over