@@ -36,6 +36,8 @@ internal class EditorServicesConsolePSHost : PSHost, IHostSupportsInteractiveSes
3636
3737 private readonly ReadLineProvider _readLineProvider ;
3838
39+ private readonly Stack < KeyValuePair < Runspace , RunspaceInfo > > _runspacesInUse ;
40+
3941 private string _localComputerName ;
4042
4143 private int _hostStarted = 0 ;
@@ -48,20 +50,22 @@ public EditorServicesConsolePSHost(
4850 _logger = loggerFactory . CreateLogger < EditorServicesConsolePSHost > ( ) ;
4951 _psFrameStack = new Stack < PowerShellContextFrame > ( ) ;
5052 _psFactory = new PowerShellFactory ( loggerFactory , this ) ;
53+ _runspacesInUse = new Stack < KeyValuePair < Runspace , RunspaceInfo > > ( ) ;
5154 _hostInfo = hostInfo ;
5255 Name = hostInfo . Name ;
5356 Version = hostInfo . Version ;
5457
5558 _readLineProvider = new ReadLineProvider ( loggerFactory ) ;
5659 _pipelineExecutor = new PipelineThreadExecutor ( loggerFactory , hostInfo , this , _readLineProvider ) ;
5760 ExecutionService = new PowerShellExecutionService ( loggerFactory , this , _pipelineExecutor ) ;
58- DebugContext = new PowerShellDebugContext ( loggerFactory , languageServer , this , _consoleReplRunner ) ;
5961 UI = new EditorServicesConsolePSHostUserInterface ( loggerFactory , _readLineProvider , hostInfo . PSHost . UI ) ;
6062
6163 if ( hostInfo . ConsoleReplEnabled )
6264 {
6365 _consoleReplRunner = new ConsoleReplRunner ( loggerFactory , this , _readLineProvider , ExecutionService ) ;
6466 }
67+
68+ DebugContext = new PowerShellDebugContext ( loggerFactory , languageServer , this , _consoleReplRunner ) ;
6569 }
6670
6771 public override CultureInfo CurrentCulture => _hostInfo . PSHost . CurrentCulture ;
@@ -217,9 +221,28 @@ private void SetExit()
217221
218222 private void PushPowerShellAndRunLoop ( SMA . PowerShell pwsh , PowerShellFrameType frameType )
219223 {
224+ RunspaceInfo runspaceInfo = null ;
225+ if ( _runspacesInUse . Count > 0 )
226+ {
227+ // This is more than just an optimization.
228+ // When debugging, we cannot execute PowerShell directly to get this information;
229+ // trying to do so will block on the command that called us, deadlocking execution.
230+ // Instead, since we are reusing the runspace, we reuse that runspace's info as well.
231+ KeyValuePair < Runspace , RunspaceInfo > currentRunspace = _runspacesInUse . Peek ( ) ;
232+ if ( currentRunspace . Key == pwsh . Runspace )
233+ {
234+ runspaceInfo = currentRunspace . Value ;
235+ }
236+ }
237+
238+ if ( runspaceInfo is null )
239+ {
240+ RunspaceOrigin runspaceOrigin = pwsh . Runspace . RunspaceIsRemote ? RunspaceOrigin . EnteredProcess : RunspaceOrigin . Local ;
241+ runspaceInfo = RunspaceInfo . CreateFromPowerShell ( _logger , pwsh , runspaceOrigin , _localComputerName ) ;
242+ _runspacesInUse . Push ( new KeyValuePair < Runspace , RunspaceInfo > ( pwsh . Runspace , runspaceInfo ) ) ;
243+ }
244+
220245 // TODO: Improve runspace origin detection here
221- RunspaceOrigin runspaceOrigin = pwsh . Runspace . RunspaceIsRemote ? RunspaceOrigin . EnteredProcess : RunspaceOrigin . Local ;
222- var runspaceInfo = RunspaceInfo . CreateFromPowerShell ( _logger , pwsh , runspaceOrigin , _localComputerName ) ;
223246 PushPowerShellAndRunLoop ( new PowerShellContextFrame ( pwsh , runspaceInfo , frameType ) ) ;
224247 }
225248
0 commit comments