@@ -89,21 +89,37 @@ internal static bool TryGetPSReadLineProxy(
89
89
{
90
90
readLineProxy = null ;
91
91
logger . LogTrace ( "Attempting to load PSReadLine" ) ;
92
- Console . WriteLine ( $ "Module path is { _psReadLineTestModulePath } ") ;
93
92
using ( var pwsh = PowerShell . Create ( ) )
94
93
{
95
94
pwsh . Runspace = runspace ;
96
95
pwsh . AddCommand ( "Microsoft.PowerShell.Core\\ Import-Module" )
97
96
. AddParameter ( "Name" , testing ? _psReadLineTestModulePath : _psReadLineModulePath )
98
97
. Invoke ( ) ;
99
98
99
+ if ( pwsh . HadErrors )
100
+ {
101
+ logger . LogWarning ( "PSConsoleReadline type not found: {Reason}" , pwsh . Streams . Error [ 0 ] . ToString ( ) ) ;
102
+ return false ;
103
+ }
104
+
100
105
var psReadLineType = Type . GetType ( "Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2" ) ;
101
106
102
107
if ( psReadLineType == null )
103
108
{
104
- logger . LogWarning ( "PSConsoleReadline type not found: {Reason}" , pwsh . HadErrors ? pwsh . Streams . Error [ 0 ] . ToString ( ) : "<Unknown reason>" ) ;
105
- Console . WriteLine ( "Failed to GetType but no PowerShell error" ) ;
106
- return false ;
109
+ // NOTE: For some reason `Type.GetType(...)` can fail to find the type,
110
+ // and in that case, this search through the `AppDomain` for some reason will succeed.
111
+ // It's slower, but only happens when needed.
112
+ logger . LogTrace ( "PSConsoleReadline type not found using Type.GetType(), searching all loaded assemblies..." ) ;
113
+ psReadLineType = AppDomain . CurrentDomain
114
+ . GetAssemblies ( )
115
+ . FirstOrDefault ( asm => asm . GetName ( ) . Name . Equals ( "Microsoft.PowerShell.PSReadLine2" ) )
116
+ ? . ExportedTypes
117
+ ? . FirstOrDefault ( type => type . FullName . Equals ( "Microsoft.PowerShell.PSConsoleReadLine" ) ) ;
118
+ if ( psReadLineType == null )
119
+ {
120
+ logger . LogWarning ( "PSConsoleReadLine type not found anywhere!" ) ;
121
+ return false ;
122
+ }
107
123
}
108
124
109
125
try
0 commit comments