diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs index 031a247497..41721dc122 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs @@ -34,7 +34,7 @@ static ErrorRecordExtensions() s_setWriteStreamProperty = Expression.Lambda>( Expression.Call( errorObjectParameter, - writeStreamProperty.GetSetMethod(), + writeStreamProperty.GetSetMethod(nonPublic: true), Expression.Constant(errorStreamType)), errorObjectParameter) .Compile(); diff --git a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs index 80d7ec661c..229ab11404 100644 --- a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs +++ b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs @@ -61,10 +61,13 @@ public static PSCommand AddDebugOutputCommand(this PSCommand psCommand) public static PSCommand MergePipelineResults(this PSCommand psCommand) { - // We need to do merge errors and output before rendering with an Out- cmdlet - Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1]; - lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); - lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output); + if (psCommand.Commands.Count > 0) + { + // We need to do merge errors and output before rendering with an Out- cmdlet + Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1]; + lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); + lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output); + } return psCommand; } diff --git a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs index c1ae78db8f..dd7d60ff34 100644 --- a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs +++ b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs @@ -46,14 +46,10 @@ public async Task CanExecutePSCommand() [Fact] public async Task CanHandleThrow() { - // TODO: Fix this so it doesn't throw! - _ = await Assert.ThrowsAsync(() => - { - return psesHost.ExecutePSCommandAsync( - new PSCommand().AddScript("throw"), - CancellationToken.None, - new PowerShellExecutionOptions { ThrowOnError = false }); - }).ConfigureAwait(true); + await psesHost.ExecutePSCommandAsync( + new PSCommand().AddScript("throw"), + CancellationToken.None, + new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(true); } [Trait("Category", "PsesInternalHost")]