Skip to content
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

Fix stepping while watch expressions or interactive pipeline is running #1894

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this just a big oops?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I meant to add a review comment pointing this out

BIG ol' oops there

{
return;
}
Expand Down