@@ -625,10 +625,14 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
625
625
626
626
var engineIntrinsics = ( EngineIntrinsics ) runspace . SessionStateProxy . GetVariable ( "ExecutionContext" ) ;
627
627
628
- if ( hostStartupInfo . ConsoleReplEnabled && ! hostStartupInfo . UsesLegacyReadLine )
628
+ if ( hostStartupInfo . ConsoleReplEnabled )
629
629
{
630
- var psrlProxy = PSReadLineProxy . LoadAndCreate ( _loggerFactory , pwsh ) ;
631
- var readLine = new PsrlReadLine ( psrlProxy , this , engineIntrinsics ) ;
630
+ // If we've been configured to use it, or if we can't load PSReadLine, use the legacy readline
631
+ if ( hostStartupInfo . UsesLegacyReadLine || ! TryLoadPSReadLine ( pwsh , engineIntrinsics , out IReadLine readLine ) )
632
+ {
633
+ readLine = new LegacyReadLine ( this ) ;
634
+ }
635
+
632
636
readLine . TryOverrideReadKey ( ReadKey ) ;
633
637
readLine . TryOverrideIdleHandler ( OnPowerShellIdle ) ;
634
638
readLineProvider . OverrideReadLine ( readLine ) ;
@@ -823,6 +827,22 @@ private Task PopOrReinitializeRunspaceAsync()
823
827
CancellationToken . None ) ;
824
828
}
825
829
830
+ private bool TryLoadPSReadLine ( PowerShell pwsh , EngineIntrinsics engineIntrinsics , out IReadLine psrlReadLine )
831
+ {
832
+ psrlReadLine = null ;
833
+ try
834
+ {
835
+ var psrlProxy = PSReadLineProxy . LoadAndCreate ( _loggerFactory , pwsh ) ;
836
+ psrlReadLine = new PsrlReadLine ( psrlProxy , this , engineIntrinsics ) ;
837
+ return true ;
838
+ }
839
+ catch ( Exception e )
840
+ {
841
+ _logger . LogError ( e , "Unable to load PSReadLine. Will fall back to legacy readline implementation." ) ;
842
+ return false ;
843
+ }
844
+ }
845
+
826
846
private record RunspaceFrame (
827
847
Runspace Runspace ,
828
848
RunspaceInfo RunspaceInfo ) ;
0 commit comments