Skip to content

Commit 38fd616

Browse files
authored
Fix crash on paste into Get-Credential (#1173)
* Fix crash on paste * Reuse same execution options
1 parent e051a89 commit 38fd616

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
1414
{
15+
using System.Collections.Generic;
1516
using System.Management.Automation;
1617
using Microsoft.Extensions.Logging;
1718

@@ -44,6 +45,15 @@ internal class PSReadLinePromptContext : IPromptContext
4445
return [Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]
4546
}";
4647

48+
private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions
49+
{
50+
WriteErrorsToHost = false,
51+
WriteOutputToHost = false,
52+
InterruptCommandPrompt = false,
53+
AddToHistory = false,
54+
IsReadLine = true,
55+
};
56+
4757
private readonly PowerShellContextService _powerShellContext;
4858

4959
private readonly PromptNest _promptNest;
@@ -68,7 +78,6 @@ internal PSReadLinePromptContext(
6878
_consoleReadLine = new ConsoleReadLine(powerShellContext);
6979
_readLineProxy = readLineProxy;
7080

71-
7281
_readLineProxy.OverrideReadKey(
7382
intercept => ConsoleProxy.SafeReadKey(
7483
intercept,
@@ -81,6 +90,7 @@ internal static bool TryGetPSReadLineProxy(
8190
out PSReadLineProxy readLineProxy)
8291
{
8392
readLineProxy = null;
93+
logger.LogTrace("Attempting to load PSReadLine");
8494
using (var pwsh = PowerShell.Create())
8595
{
8696
pwsh.Runspace = runspace;
@@ -91,18 +101,20 @@ internal static bool TryGetPSReadLineProxy(
91101

92102
if (psReadLineType == null)
93103
{
104+
logger.LogWarning("PSReadLine unable to be loaded: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : "<Unknown reason>");
94105
return false;
95106
}
96107

97108
try
98109
{
99110
readLineProxy = new PSReadLineProxy(psReadLineType, logger);
100111
}
101-
catch (InvalidOperationException)
112+
catch (InvalidOperationException e)
102113
{
103114
// The Type we got back from PowerShell doesn't have the members we expected.
104115
// Could be an older version, a custom build, or something a newer version with
105116
// breaking changes.
117+
logger.LogWarning("PSReadLine unable to be loaded: {Reason}", e);
106118
return false;
107119
}
108120
}
@@ -119,38 +131,27 @@ public async Task<string> InvokeReadLineAsync(bool isCommandLine, CancellationTo
119131
throw new TaskCanceledException();
120132
}
121133

122-
try
134+
if (!isCommandLine)
123135
{
124-
if (!isCommandLine)
125-
{
126-
return await _consoleReadLine.InvokeLegacyReadLineAsync(
127-
isCommandLine: false,
128-
_readLineCancellationSource.Token).ConfigureAwait(false);
129-
}
136+
return await _consoleReadLine.InvokeLegacyReadLineAsync(
137+
isCommandLine: false,
138+
_readLineCancellationSource.Token).ConfigureAwait(false);
139+
}
130140

131-
var result = (await _powerShellContext.ExecuteCommandAsync<string>(
132-
new PSCommand()
133-
.AddScript(ReadLineScript)
134-
.AddArgument(_readLineCancellationSource.Token),
135-
errorMessages: null,
136-
new ExecutionOptions()
137-
{
138-
WriteErrorsToHost = false,
139-
WriteOutputToHost = false,
140-
InterruptCommandPrompt = false,
141-
AddToHistory = false,
142-
IsReadLine = isCommandLine
143-
}).ConfigureAwait(false))
144-
.FirstOrDefault();
141+
var readLineCommand = new PSCommand()
142+
.AddScript(ReadLineScript)
143+
.AddArgument(_readLineCancellationSource.Token);
145144

146-
return cancellationToken.IsCancellationRequested
147-
? string.Empty
148-
: result;
149-
}
150-
finally
151-
{
152-
_readLineCancellationSource = null;
153-
}
145+
IEnumerable<string> readLineResults = await _powerShellContext.ExecuteCommandAsync<string>(
146+
readLineCommand,
147+
errorMessages: null,
148+
s_psrlExecutionOptions).ConfigureAwait(false);
149+
150+
string line = readLineResults.FirstOrDefault();
151+
152+
return cancellationToken.IsCancellationRequested
153+
? string.Empty
154+
: line;
154155
}
155156

156157
public void AbortReadLine()

0 commit comments

Comments
 (0)