Skip to content

Make sure ErrorRecords go to Error stream #1201

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
Show file tree
Hide file tree
Changes from 3 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 @@ -41,17 +41,28 @@ public class PowerShellContextService : IDisposable, IHostSupportsInteractiveSes
"../../Commands/PowerShellEditorServices.Commands.psd1"));

private static readonly Action<Runspace, ApartmentState> s_runspaceApartmentStateSetter;
private static readonly PropertyInfo s_writeStreamProperty;
private static readonly object s_errorStreamValue;

[SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline", Justification = "cctor needed for version specific initialization")]
static PowerShellContextService()
{
// PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection
// PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection.
if (!VersionUtils.IsNetCore || VersionUtils.IsPS7OrGreater)
{
MethodInfo setterInfo = typeof(Runspace).GetProperty("ApartmentState").GetSetMethod();
Delegate setter = Delegate.CreateDelegate(typeof(Action<Runspace, ApartmentState>), firstArgument: null, method: setterInfo);
s_runspaceApartmentStateSetter = (Action<Runspace, ApartmentState>)setter;
}

if (VersionUtils.IsPS7OrGreater)
{
// Used to write ErrorRecords to the Error stream. Using Public and NonPublic because the plan is to make this property
// public in 7.0.1
s_writeStreamProperty = typeof(PSObject).GetProperty("WriteStream", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
Type writeStreamType = typeof(PSObject).Assembly.GetType("System.Management.Automation.WriteStreamType");
s_errorStreamValue = Enum.Parse(writeStreamType, "Error");
}
}

#region Fields
Expand Down Expand Up @@ -1924,43 +1935,22 @@ internal void WriteOutput(
}
}

private void WriteExceptionToHost(Exception e)
private void WriteExceptionToHost(RuntimeException e)
{
const string ExceptionFormat =
"{0}\r\n{1}\r\n + CategoryInfo : {2}\r\n + FullyQualifiedErrorId : {3}";

if (!(e is IContainsErrorRecord containsErrorRecord) ||
containsErrorRecord.ErrorRecord == null)
{
this.WriteError(e.Message, null, 0, 0);
return;
}
var psObject = PSObject.AsPSObject(e.ErrorRecord);

ErrorRecord errorRecord = containsErrorRecord.ErrorRecord;
if (errorRecord.InvocationInfo == null)
// Used to write ErrorRecords to the Error stream so they are rendered in the console correctly.
if (VersionUtils.IsPS7OrGreater)
{
this.WriteError(errorRecord.ToString(), String.Empty, 0, 0);
return;
s_writeStreamProperty.SetValue(psObject, s_errorStreamValue);
}

string errorRecordString = errorRecord.ToString();
if ((errorRecord.InvocationInfo.PositionMessage != null) &&
errorRecordString.IndexOf(errorRecord.InvocationInfo.PositionMessage, StringComparison.Ordinal) != -1)
else
{
this.WriteError(errorRecordString);
return;
var note = new PSNoteProperty("writeErrorStream", true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var note = new PSNoteProperty("writeErrorStream", true);
var note = new PSNoteProperty("WriteErrorStream", true);

Copy link
Member Author

@TylerLeonhardt TylerLeonhardt Feb 20, 2020

Choose a reason for hiding this comment

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

I got that from PowerShell but since it seems to be case insensitive I can make this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

May as well use what PowerShell uses. Strange they did it that way

psObject.Properties.Add(note);
}

string message =
string.Format(
CultureInfo.InvariantCulture,
ExceptionFormat,
errorRecord.ToString(),
errorRecord.InvocationInfo.PositionMessage,
errorRecord.CategoryInfo,
errorRecord.FullyQualifiedErrorId);

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

private void WriteError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,13 @@ public override void WriteWarningLine(string message)
/// <param name="value"></param>
public override void WriteErrorLine(string value)
{
// PowerShell's ConsoleHost also skips over empty lines:
// https://github.com/PowerShell/PowerShell/blob/8e683972284a5a7f773ea6d027d9aac14d7e7524/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs#L1334-L1337
if (string.IsNullOrEmpty(value))
{
return;
}

this.WriteOutput(
value,
true,
Expand Down