Skip to content

Commit

Permalink
Merge pull request #1894 from SeeminglyScience/fix-watch-expression-e…
Browse files Browse the repository at this point in the history
…rrors

Fix stepping while watch expressions or interactive pipeline is running
  • Loading branch information
andyleejordan authored Aug 15, 2022
2 parents 87cd721 + 60e37b4 commit c25d0d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,22 @@ public async Task<VariableDetails> EvaluateExpressionAsync(
bool writeResultAsOutput)
{
PSCommand command = new PSCommand().AddScript(expressionString);
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(
command,
CancellationToken.None,
new PowerShellExecutionOptions { WriteOutputToHost = writeResultAsOutput, ThrowOnError = !writeResultAsOutput }).ConfigureAwait(false);
IReadOnlyList<PSObject> results;
try
{
results = await _executionService.ExecutePSCommandAsync<PSObject>(
command,
CancellationToken.None,
new PowerShellExecutionOptions { WriteOutputToHost = writeResultAsOutput, ThrowOnError = !writeResultAsOutput }).ConfigureAwait(false);
}
catch (Exception e)
{
// If a watch expression throws we want to show the exception.
// TODO: Show the exception as an expandable object.
return new VariableDetails(
expressionString,
$"{e.GetType().Name}: {e.Message}");
}

// Since this method should only be getting invoked in the debugger,
// we can assume that Out-String will be getting used to format results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Context;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;

Expand Down Expand Up @@ -145,15 +146,27 @@ public void SetDebugResuming(DebuggerResumeAction debuggerResumeAction)
// TODO: We need to assign cancellation tokens to each frame, because the current
// logic results in a deadlock here when we try to cancel the scopes...which
// includes ourself (hence running it in a separate thread).
Task.Run(() => _psesHost.UnwindCallStack());
_ = Task.Run(() => _psesHost.UnwindCallStack());
return;
}

// Otherwise we're continuing or stepping (i.e. resuming) so we need to cancel the
// debugger REPL.
if (_psesHost.CurrentFrame.IsRepl)
PowerShellFrameType frameType = _psesHost.CurrentFrame.FrameType;
if (frameType.HasFlag(PowerShellFrameType.Repl))
{
_psesHost.CancelIdleParentTask();
return;
}

// If the user is running something via the REPL like `while ($true) { sleep 1 }`
// and then tries to step, we want to stop that so that execution can resume.
//
// This also applies to anything we're running on debugger stop like watch variables.
if (frameType.HasFlag(PowerShellFrameType.Debug | PowerShellFrameType.Nested))
{
_psesHost.ForceSetExit();
_psesHost.CancelIdleParentTask();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ internal void MaybeAddToHistory()

private void CancelDebugExecution()
{
if (_pwsh.Runspace.RunspaceStateInfo.IsUsable())
if (!_pwsh.Runspace.RunspaceStateInfo.IsUsable())
{
return;
}
Expand Down

0 comments on commit c25d0d2

Please sign in to comment.