Skip to content

Revert "Consolidate InterruptCurrentForeground and `MustRunInForegr… #1780

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class ConfigurationDoneHandler : IConfigurationDoneHandler
// This API is mostly used for F5 execution so it requires the foreground.
private static readonly PowerShellExecutionOptions s_debuggerExecutionOptions = new()
{
RequiresForeground = true,
MustRunInForeground = true,
WriteInputToHost = true,
WriteOutputToHost = true,
ThrowOnError = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,17 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
.AddParameter("ArgumentList", new object[] { editorContext });

// This API is used for editor command execution so it requires the foreground.
// This API is used for editor command execution, so it needs to interrupt the
// current prompt (or other foreground task).
return ExecutionService.ExecutePSCommandAsync(
executeCommand,
cancellationToken,
new PowerShellExecutionOptions
{
RequiresForeground = true,
WriteOutputToHost = !editorCommand.SuppressOutput,
AddToHistory = !editorCommand.SuppressOutput,
ThrowOnError = false,
InterruptCurrentForeground = true
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public PsrlReadLine(
_psrlProxy.OverrideIdleHandler(onIdleAction);
}

public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(
representation: "ReadLine",
new ExecutionOptions { RequiresForeground = true },
InvokePSReadLine,
cancellationToken);
public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken);

protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => _psesHost.ReadKey(intercept: true, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ public enum ExecutionPriority
public record ExecutionOptions
{
public ExecutionPriority Priority { get; init; } = ExecutionPriority.Normal;
public bool RequiresForeground { get; init; }
public bool MustRunInForeground { get; init; }
public bool InterruptCurrentForeground { get; init; }
}

public record PowerShellExecutionOptions : ExecutionOptions
{
internal static PowerShellExecutionOptions ImmediateInteractive = new()
{
Priority = ExecutionPriority.Next,
RequiresForeground = true,
MustRunInForeground = true,
InterruptCurrentForeground = true,
};

public bool WriteOutputToHost { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ internal class EvaluateHandler : IEvaluateHandler

public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken)
{
// This API is mostly used for F8 execution so it requires the foreground.
// This API is mostly used for F8 execution, so it needs to interrupt the command prompt
// (or other foreground task).
await _executionService.ExecutePSCommandAsync(
new PSCommand().AddScript(request.Expression),
CancellationToken.None,
new PowerShellExecutionOptions
{
RequiresForeground = true,
WriteInputToHost = true,
WriteOutputToHost = true,
AddToHistory = true,
ThrowOnError = false,
InterruptCurrentForeground = true
}).ConfigureAwait(false);

// TODO: Should we return a more informative result?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -277,7 +277,7 @@ public void SetExit()
public Task<T> InvokeTaskOnPipelineThreadAsync<T>(
SynchronousTask<T> task)
{
if (task.ExecutionOptions.RequiresForeground)
if (task.ExecutionOptions.InterruptCurrentForeground)
{
// When a task must displace the current foreground command,
// we must:
Expand Down Expand Up @@ -404,14 +404,9 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp

internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
{
// TODO: Why exactly does loading profiles require the foreground?
return ExecuteDelegateAsync(
"LoadProfiles",
new PowerShellExecutionOptions
{
RequiresForeground = true,
ThrowOnError = false
},
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
cancellationToken);
}
Expand Down Expand Up @@ -855,15 +850,7 @@ private string InvokeReadLine(CancellationToken cancellationToken)
private void InvokeInput(string input, CancellationToken cancellationToken)
{
PSCommand command = new PSCommand().AddScript(input, useLocalScope: false);
InvokePSCommand(
command,
new PowerShellExecutionOptions
{
AddToHistory = true,
ThrowOnError = false,
WriteOutputToHost = true
},
cancellationToken);
InvokePSCommand(command, new PowerShellExecutionOptions { AddToHistory = true, ThrowOnError = false, WriteOutputToHost = true }, cancellationToken);
}

private void AddRunspaceEventHandlers(Runspace runspace)
Expand Down Expand Up @@ -990,7 +977,7 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
while (!cancellationScope.CancellationToken.IsCancellationRequested
&& _taskQueue.TryTake(out ISynchronousTask task))
{
if (task.ExecutionOptions.RequiresForeground)
if (task.ExecutionOptions.MustRunInForeground)
{
// If we have a task that is queued, but cannot be run under readline
// we place it back at the front of the queue, and cancel the readline task
Expand Down Expand Up @@ -1111,27 +1098,27 @@ private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStop

void OnDebuggerStoppedImpl(object sender, DebuggerStopEventArgs debuggerStopEventArgs)
{
// If the debug server is NOT active, we need to synchronize state and start it.
if (!DebugContext.IsDebugServerActive)
{
_languageServer?.SendNotification("powerShell/startDebugger");
}
// If the debug server is NOT active, we need to synchronize state and start it.
if (!DebugContext.IsDebugServerActive)
{
_languageServer?.SendNotification("powerShell/startDebugger");
}

DebugContext.SetDebuggerStopped(debuggerStopEventArgs);
DebugContext.SetDebuggerStopped(debuggerStopEventArgs);

try
{
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
PushPowerShellAndRunLoop(
CreateNestedPowerShell(CurrentRunspace),
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
}
finally
{
DebugContext.SetDebuggerResumed();
}
try
{
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
PushPowerShellAndRunLoop(
CreateNestedPowerShell(CurrentRunspace),
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
}
finally
{
DebugContext.SetDebuggerResumed();
}
}
}

Expand All @@ -1155,7 +1142,7 @@ private Task PopOrReinitializeRunspaceAsync()
// we simply run this on its thread, guaranteeing that no other action can occur
return ExecuteDelegateAsync(
nameof(PopOrReinitializeRunspaceAsync),
new ExecutionOptions { RequiresForeground = true },
new ExecutionOptions { InterruptCurrentForeground = true },
(_) =>
{
while (_psFrameStack.Count > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,7 @@ public async Task<bool> CreateFromTemplateAsync(
await _executionService.ExecutePSCommandAsync(
command,
CancellationToken.None,
new PowerShellExecutionOptions
{
RequiresForeground = true,
WriteOutputToHost = true,
ThrowOnError = false
}).ConfigureAwait(false);
new PowerShellExecutionOptions { WriteOutputToHost = true, InterruptCurrentForeground = true, ThrowOnError = false }).ConfigureAwait(false);

// If any errors were written out, creation was not successful
return true;
Expand Down