Skip to content

Ensure that errors are written to the console when debugging #1230

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 2 commits into from
Mar 12, 2020
Merged
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 @@ -645,6 +645,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
executionOptions)).ConfigureAwait(false);
}

Task writeErrorsToConsoleTask = null;
try
{
// Instruct PowerShell to send output and errors to the host
Expand Down Expand Up @@ -836,7 +837,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
if (executionOptions.WriteErrorsToHost)
{
// Write the error to the host
this.WriteExceptionToHost(e);
// We must await this after the runspace handle has been released or we will deadlock
writeErrorsToConsoleTask = this.WriteExceptionToHostAsync(e);
}
}
catch (Exception)
Expand Down Expand Up @@ -896,6 +898,10 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
if (runspaceHandle != null)
{
runspaceHandle.Dispose();
if (writeErrorsToConsoleTask != null)
{
await writeErrorsToConsoleTask.ConfigureAwait(false);
}
}

this.OnExecutionStatusChanged(
Expand Down Expand Up @@ -1948,7 +1954,7 @@ internal void WriteOutput(
}
}

private void WriteExceptionToHost(RuntimeException e)
private Task WriteExceptionToHostAsync(RuntimeException e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
var psObject = PSObject.AsPSObject(e.ErrorRecord);

Expand All @@ -1963,7 +1969,7 @@ private void WriteExceptionToHost(RuntimeException e)
psObject.Properties.Add(note);
}

ExecuteCommandAsync(new PSCommand().AddCommand("Microsoft.PowerShell.Core\\Out-Default").AddParameter("InputObject", psObject));
return ExecuteCommandAsync(new PSCommand().AddCommand("Microsoft.PowerShell.Core\\Out-Default").AddParameter("InputObject", psObject));
}

private void WriteError(
Expand Down