Skip to content

Commit 4b4ee95

Browse files
committed
Fix bug where error in prompt function crashed REPL loop
Double-whammy fix by both setting `ThrowOnError` to false, and catching a more generic PowerShell `RuntimeException`. Plus a regression test!
1 parent c6b7b91 commit 4b4ee95

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Diff for: src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
3131

3232
internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRunspaceContext, IInternalPowerShellExecutionService
3333
{
34-
private const string DefaultPrompt = "> ";
34+
internal const string DefaultPrompt = "> ";
3535

3636
private static readonly PropertyInfo s_scriptDebuggerTriggerObjectProperty;
3737

@@ -865,21 +865,25 @@ private void DoOneRepl(CancellationToken cancellationToken)
865865
}
866866
}
867867

868-
private string GetPrompt(CancellationToken cancellationToken)
868+
internal string GetPrompt(CancellationToken cancellationToken)
869869
{
870870
Runspace.ThrowCancelledIfUnusable();
871871
string prompt = DefaultPrompt;
872872
try
873873
{
874874
// TODO: Should we cache PSCommands like this as static members?
875875
PSCommand command = new PSCommand().AddCommand("prompt");
876-
IReadOnlyList<string> results = InvokePSCommand<string>(command, executionOptions: null, cancellationToken);
876+
IReadOnlyList<string> results = InvokePSCommand<string>(
877+
command,
878+
executionOptions: new PowerShellExecutionOptions { ThrowOnError = false },
879+
cancellationToken);
880+
877881
if (results?.Count > 0)
878882
{
879883
prompt = results[0];
880884
}
881885
}
882-
catch (CommandNotFoundException) { } // Use default prompt
886+
catch (RuntimeException) { } // Use default prompt
883887

884888
if (CurrentRunspace.RunspaceOrigin != RunspaceOrigin.Local)
885889
{

Diff for: test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ await psesHost.ExecuteDelegateAsync(
162162
CancellationToken.None).ConfigureAwait(true);
163163
}
164164

165+
[Fact]
166+
public async Task CanHandleBrokenPrompt()
167+
{
168+
await psesHost.ExecutePSCommandAsync(
169+
new PSCommand().AddScript("function prompt { throw }"),
170+
CancellationToken.None).ConfigureAwait(true);
171+
string prompt = psesHost.GetPrompt(CancellationToken.None);
172+
Assert.Equal(PsesInternalHost.DefaultPrompt, prompt);
173+
}
174+
165175
[Fact]
166176
public async Task CanLoadPSReadLine()
167177
{

0 commit comments

Comments
 (0)