Skip to content

Fix bug where error in prompt function crashed REPL #1866

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 1 commit into from
Aug 3, 2022
Merged
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 @@ -31,7 +31,7 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host

internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRunspaceContext, IInternalPowerShellExecutionService
{
private const string DefaultPrompt = "> ";
internal const string DefaultPrompt = "> ";

private static readonly PropertyInfo s_scriptDebuggerTriggerObjectProperty;

Expand Down Expand Up @@ -865,21 +865,25 @@ private void DoOneRepl(CancellationToken cancellationToken)
}
}

private string GetPrompt(CancellationToken cancellationToken)
internal string GetPrompt(CancellationToken cancellationToken)
{
Runspace.ThrowCancelledIfUnusable();
string prompt = DefaultPrompt;
try
{
// TODO: Should we cache PSCommands like this as static members?
PSCommand command = new PSCommand().AddCommand("prompt");
IReadOnlyList<string> results = InvokePSCommand<string>(command, executionOptions: null, cancellationToken);
IReadOnlyList<string> results = InvokePSCommand<string>(
command,
executionOptions: new PowerShellExecutionOptions { ThrowOnError = false },
cancellationToken);

if (results?.Count > 0)
{
prompt = results[0];
}
}
catch (CommandNotFoundException) { } // Use default prompt
catch (RuntimeException) { } // Use default prompt

if (CurrentRunspace.RunspaceOrigin != RunspaceOrigin.Local)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,30 @@ await psesHost.ExecuteDelegateAsync(
CancellationToken.None).ConfigureAwait(true);
}

// NOTE: Tests where we call functions that use PowerShell runspaces are slightly more
// complicated than one would expect because we explicitly need the methods to run on the
// pipeline thread, otherwise Windows complains about the the thread's apartment state not
// matching. Hence we use a delegate where it looks like we could just call the method.

[Fact]
public async Task CanHandleBrokenPrompt()
{
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("function prompt { throw }"),
CancellationToken.None).ConfigureAwait(true);

string prompt = await psesHost.ExecuteDelegateAsync(
nameof(psesHost.GetPrompt),
executionOptions: null,
(_, _) => psesHost.GetPrompt(CancellationToken.None),
CancellationToken.None).ConfigureAwait(true);

Assert.Equal(PsesInternalHost.DefaultPrompt, prompt);
}

[Fact]
public async Task CanLoadPSReadLine()
{
// NOTE: This is slightly more complicated than one would expect because we explicitly
// need it to run on the pipeline thread otherwise Windows complains about the the
// thread's apartment state not matching.
Assert.True(await psesHost.ExecuteDelegateAsync(
nameof(psesHost.TryLoadPSReadLine),
executionOptions: null,
Expand Down