@@ -625,10 +625,14 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
625625
626626 var engineIntrinsics = ( EngineIntrinsics ) runspace . SessionStateProxy . GetVariable ( "ExecutionContext" ) ;
627627
628- if ( hostStartupInfo . ConsoleReplEnabled && ! hostStartupInfo . UsesLegacyReadLine )
628+ if ( hostStartupInfo . ConsoleReplEnabled )
629629 {
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+
632636 readLine . TryOverrideReadKey ( ReadKey ) ;
633637 readLine . TryOverrideIdleHandler ( OnPowerShellIdle ) ;
634638 readLineProvider . OverrideReadLine ( readLine ) ;
@@ -823,6 +827,22 @@ private Task PopOrReinitializeRunspaceAsync()
823827 CancellationToken . None ) ;
824828 }
825829
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+
826846 private record RunspaceFrame (
827847 Runspace Runspace ,
828848 RunspaceInfo RunspaceInfo ) ;
0 commit comments