diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs index 99ea3b40a..6028baecd 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs @@ -12,6 +12,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext { + using System.Collections.Generic; using System.Management.Automation; using Microsoft.Extensions.Logging; @@ -44,6 +45,15 @@ internal class PSReadLinePromptContext : IPromptContext return [Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null] }"; + private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions + { + WriteErrorsToHost = false, + WriteOutputToHost = false, + InterruptCommandPrompt = false, + AddToHistory = false, + IsReadLine = true, + }; + private readonly PowerShellContextService _powerShellContext; private readonly PromptNest _promptNest; @@ -68,7 +78,6 @@ internal PSReadLinePromptContext( _consoleReadLine = new ConsoleReadLine(powerShellContext); _readLineProxy = readLineProxy; - _readLineProxy.OverrideReadKey( intercept => ConsoleProxy.SafeReadKey( intercept, @@ -81,6 +90,7 @@ internal static bool TryGetPSReadLineProxy( out PSReadLineProxy readLineProxy) { readLineProxy = null; + logger.LogTrace("Attempting to load PSReadLine"); using (var pwsh = PowerShell.Create()) { pwsh.Runspace = runspace; @@ -91,6 +101,7 @@ internal static bool TryGetPSReadLineProxy( if (psReadLineType == null) { + logger.LogWarning("PSReadLine unable to be loaded: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : ""); return false; } @@ -98,11 +109,12 @@ internal static bool TryGetPSReadLineProxy( { readLineProxy = new PSReadLineProxy(psReadLineType, logger); } - catch (InvalidOperationException) + catch (InvalidOperationException e) { // The Type we got back from PowerShell doesn't have the members we expected. // Could be an older version, a custom build, or something a newer version with // breaking changes. + logger.LogWarning("PSReadLine unable to be loaded: {Reason}", e); return false; } } @@ -119,38 +131,27 @@ public async Task InvokeReadLineAsync(bool isCommandLine, CancellationTo throw new TaskCanceledException(); } - try + if (!isCommandLine) { - if (!isCommandLine) - { - return await _consoleReadLine.InvokeLegacyReadLineAsync( - isCommandLine: false, - _readLineCancellationSource.Token).ConfigureAwait(false); - } + return await _consoleReadLine.InvokeLegacyReadLineAsync( + isCommandLine: false, + _readLineCancellationSource.Token).ConfigureAwait(false); + } - var result = (await _powerShellContext.ExecuteCommandAsync( - new PSCommand() - .AddScript(ReadLineScript) - .AddArgument(_readLineCancellationSource.Token), - errorMessages: null, - new ExecutionOptions() - { - WriteErrorsToHost = false, - WriteOutputToHost = false, - InterruptCommandPrompt = false, - AddToHistory = false, - IsReadLine = isCommandLine - }).ConfigureAwait(false)) - .FirstOrDefault(); + var readLineCommand = new PSCommand() + .AddScript(ReadLineScript) + .AddArgument(_readLineCancellationSource.Token); - return cancellationToken.IsCancellationRequested - ? string.Empty - : result; - } - finally - { - _readLineCancellationSource = null; - } + IEnumerable readLineResults = await _powerShellContext.ExecuteCommandAsync( + readLineCommand, + errorMessages: null, + s_psrlExecutionOptions).ConfigureAwait(false); + + string line = readLineResults.FirstOrDefault(); + + return cancellationToken.IsCancellationRequested + ? string.Empty + : line; } public void AbortReadLine()