Skip to content

Fix execution of debug prompt commands #1803

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

Merged
merged 2 commits into from
May 17, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public SynchronousPowerShellTask(

public override ExecutionOptions ExecutionOptions => PowerShellExecutionOptions;

// These are PowerShell's intrinsic debugger commands that must be run via
// `ProcessDebugCommand`.
private static readonly string[] DebuggerCommands = { "continue", "c", "k", "h", "?", "list", "l", "stepInto", "s", "stepOut", "o", "stepOver", "v", "quit", "q", "detach", "d" };

public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)
{
_psesHost.Runspace.ThrowCancelledIfUnusable();
Expand All @@ -65,8 +61,14 @@ public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)
_psesHost.WriteWithPrompt(_psCommand, cancellationToken);
}

// If we're in a breakpoint it means we're executing either interactive commands in
// a debug prompt, or our own special commands to query the PowerShell debugger for
// state that we sync with the LSP debugger. The former commands we want to send
// through PowerShell's `Debugger.ProcessCommand` so that they work as expected, but
// 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
&& (IsDebuggerCommand(_psCommand) || _pwsh.Runspace.RunspaceIsRemote)
&& (PowerShellExecutionOptions.AddToHistory || IsPromptCommand(_psCommand) || _pwsh.Runspace.RunspaceIsRemote)
? ExecuteInDebugger(cancellationToken)
: ExecuteNormally(cancellationToken);
}
Expand All @@ -78,7 +80,7 @@ public override IReadOnlyList<TResult> Run(CancellationToken cancellationToken)

public override string ToString() => _psCommand.GetInvocationText();

private static bool IsDebuggerCommand(PSCommand command)
private static bool IsPromptCommand(PSCommand command)
{
if (command.Commands.Count is not 1
|| command.Commands[0] is { IsScript: false } or { Parameters.Count: > 0 })
Expand All @@ -87,15 +89,7 @@ private static bool IsDebuggerCommand(PSCommand command)
}

string commandText = command.Commands[0].CommandText;
foreach (string knownCommand in DebuggerCommands)
{
if (commandText.Equals(knownCommand, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
return commandText.Equals("prompt", StringComparison.OrdinalIgnoreCase);
}

private IReadOnlyList<TResult> ExecuteNormally(CancellationToken cancellationToken)
Expand Down Expand Up @@ -124,7 +118,7 @@ private IReadOnlyList<TResult> ExecuteNormally(CancellationToken cancellationTok
result = _pwsh.InvokeCommand<TResult>(_psCommand, invocationSettings);
cancellationToken.ThrowIfCancellationRequested();
}
// Allow terminate exceptions to propogate for flow control.
// Allow terminate exceptions to propagate for flow control.
catch (TerminateException)
{
throw;
Expand Down Expand Up @@ -222,12 +216,12 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
//
// Unfortunately, this API does not allow us to pass in the InvocationSettings,
// which means (for instance) that we cannot instruct it to avoid adding our
// debugger implmentation's commands to the history. So instead we now only call
// debugger implementation's commands to the history. So instead we now only call
// `ExecuteInDebugger` for PowerShell's own intrinsic debugger commands.
debuggerResult = _pwsh.Runspace.Debugger.ProcessCommand(_psCommand, outputCollection);
cancellationToken.ThrowIfCancellationRequested();
}
// Allow terminate exceptions to propogate for flow control.
// Allow terminate exceptions to propagate for flow control.
catch (TerminateException)
{
throw;
Expand Down Expand Up @@ -281,7 +275,7 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT

_psesHost.DebugContext.ProcessDebuggerResult(debuggerResult);

// Optimisation to save wasted computation if we're going to throw the output away anyway
// Optimization to save wasted computation if we're going to throw the output away anyway
if (PowerShellExecutionOptions.WriteOutputToHost)
{
return Array.Empty<TResult>();
Expand Down