@@ -41,17 +41,28 @@ internal class PowerShellContextService : IDisposable, IHostSupportsInteractiveS
41
41
"../../Commands/PowerShellEditorServices.Commands.psd1" ) ) ;
42
42
43
43
private static readonly Action < Runspace , ApartmentState > s_runspaceApartmentStateSetter ;
44
+ private static readonly PropertyInfo s_writeStreamProperty ;
45
+ private static readonly object s_errorStreamValue ;
44
46
45
47
[ SuppressMessage ( "Performance" , "CA1810:Initialize reference type static fields inline" , Justification = "cctor needed for version specific initialization" ) ]
46
48
static PowerShellContextService ( )
47
49
{
48
- // PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection
50
+ // PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection.
49
51
if ( ! VersionUtils . IsNetCore || VersionUtils . IsPS7OrGreater )
50
52
{
51
53
MethodInfo setterInfo = typeof ( Runspace ) . GetProperty ( "ApartmentState" ) . GetSetMethod ( ) ;
52
54
Delegate setter = Delegate . CreateDelegate ( typeof ( Action < Runspace , ApartmentState > ) , firstArgument : null , method : setterInfo ) ;
53
55
s_runspaceApartmentStateSetter = ( Action < Runspace , ApartmentState > ) setter ;
54
56
}
57
+
58
+ if ( VersionUtils . IsPS7OrGreater )
59
+ {
60
+ // Used to write ErrorRecords to the Error stream. Using Public and NonPublic because the plan is to make this property
61
+ // public in 7.0.1
62
+ s_writeStreamProperty = typeof ( PSObject ) . GetProperty ( "WriteStream" , BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ) ;
63
+ Type writeStreamType = typeof ( PSObject ) . Assembly . GetType ( "System.Management.Automation.WriteStreamType" ) ;
64
+ s_errorStreamValue = Enum . Parse ( writeStreamType , "Error" ) ;
65
+ }
55
66
}
56
67
57
68
#region Fields
@@ -1924,43 +1935,22 @@ internal void WriteOutput(
1924
1935
}
1925
1936
}
1926
1937
1927
- private void WriteExceptionToHost ( Exception e )
1938
+ private void WriteExceptionToHost ( RuntimeException e )
1928
1939
{
1929
- const string ExceptionFormat =
1930
- "{0}\r \n {1}\r \n + CategoryInfo : {2}\r \n + FullyQualifiedErrorId : {3}" ;
1931
-
1932
- if ( ! ( e is IContainsErrorRecord containsErrorRecord ) ||
1933
- containsErrorRecord . ErrorRecord == null )
1934
- {
1935
- this . WriteError ( e . Message , null , 0 , 0 ) ;
1936
- return ;
1937
- }
1940
+ var psObject = PSObject . AsPSObject ( e . ErrorRecord ) ;
1938
1941
1939
- ErrorRecord errorRecord = containsErrorRecord . ErrorRecord ;
1940
- if ( errorRecord . InvocationInfo == null )
1942
+ // Used to write ErrorRecords to the Error stream so they are rendered in the console correctly.
1943
+ if ( VersionUtils . IsPS7OrGreater )
1941
1944
{
1942
- this . WriteError ( errorRecord . ToString ( ) , String . Empty , 0 , 0 ) ;
1943
- return ;
1945
+ s_writeStreamProperty . SetValue ( psObject , s_errorStreamValue ) ;
1944
1946
}
1945
-
1946
- string errorRecordString = errorRecord . ToString ( ) ;
1947
- if ( ( errorRecord . InvocationInfo . PositionMessage != null ) &&
1948
- errorRecordString . IndexOf ( errorRecord . InvocationInfo . PositionMessage , StringComparison . Ordinal ) != - 1 )
1947
+ else
1949
1948
{
1950
- this . WriteError ( errorRecordString ) ;
1951
- return ;
1949
+ var note = new PSNoteProperty ( "writeErrorStream" , true ) ;
1950
+ psObject . Properties . Add ( note ) ;
1952
1951
}
1953
1952
1954
- string message =
1955
- string . Format (
1956
- CultureInfo . InvariantCulture ,
1957
- ExceptionFormat ,
1958
- errorRecord . ToString ( ) ,
1959
- errorRecord . InvocationInfo . PositionMessage ,
1960
- errorRecord . CategoryInfo ,
1961
- errorRecord . FullyQualifiedErrorId ) ;
1962
-
1963
- this . WriteError ( message ) ;
1953
+ ExecuteCommandAsync ( new PSCommand ( ) . AddCommand ( "Microsoft.PowerShell.Core\\ Out-Default" ) . AddParameter ( "InputObject" , psObject ) ) ;
1964
1954
}
1965
1955
1966
1956
private void WriteError (
0 commit comments