11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- using Microsoft . Extensions . Logging ;
5- using Microsoft . PowerShell . EditorServices . Hosting ;
6- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Console ;
7- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Context ;
8- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Runspace ;
9- using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
104using System ;
115using System . Collections . Generic ;
126using System . Globalization ;
7+ using System . IO ;
138using System . Management . Automation . Host ;
9+ using System . Text ;
10+ using System . Threading ;
11+ using System . Threading . Tasks ;
12+ using Microsoft . Extensions . Logging ;
13+ using Microsoft . PowerShell . EditorServices . Hosting ;
14+ using Microsoft . PowerShell . EditorServices . Services . PowerShell . Console ;
15+ using Microsoft . PowerShell . EditorServices . Services . PowerShell . Context ;
1416using Microsoft . PowerShell . EditorServices . Services . PowerShell . Debugging ;
1517using Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution ;
18+ using Microsoft . PowerShell . EditorServices . Services . PowerShell . Runspace ;
1619using Microsoft . PowerShell . EditorServices . Services . PowerShell . Utility ;
1720using Microsoft . PowerShell . EditorServices . Utility ;
18- using System . IO ;
19- using System . Reflection ;
20- using System . Text ;
21- using System . Threading ;
22- using System . Threading . Tasks ;
21+ using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
2322
2423namespace Microsoft . PowerShell . EditorServices . Services . PowerShell . Host
2524{
@@ -29,11 +28,12 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
2928 internal class PsesInternalHost : PSHost , IHostSupportsInteractiveSession , IRunspaceContext , IInternalPowerShellExecutionService
3029 {
3130 private const string DefaultPrompt = "PSIC> " ;
31+ // This is a default that can be overriden at runtime by the user or tests.
32+ private static string s_bundledModulePath = Path . GetFullPath ( Path . Combine (
33+ Path . GetDirectoryName ( typeof ( PsesInternalHost ) . Assembly . Location ) , ".." , ".." , ".." ) ) ;
3234
33- private static readonly string s_commandsModulePath = Path . GetFullPath (
34- Path . Combine (
35- Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ,
36- "../../Commands/PowerShellEditorServices.Commands.psd1" ) ) ;
35+ private static string s_commandsModulePath => Path . GetFullPath ( Path . Combine (
36+ s_bundledModulePath , "PowerShellEditorServices" , "Commands" , "PowerShellEditorServices.Commands.psd1" ) ) ;
3737
3838 private readonly ILoggerFactory _loggerFactory ;
3939
@@ -85,6 +85,13 @@ public PsesInternalHost(
8585 _languageServer = languageServer ;
8686 _hostInfo = hostInfo ;
8787
88+ // Respect a user provided bundled module path.
89+ if ( Directory . Exists ( hostInfo . BundledModulePath ) )
90+ {
91+ _logger . LogTrace ( "Using new bundled module path: {}" , hostInfo . BundledModulePath ) ;
92+ s_bundledModulePath = hostInfo . BundledModulePath ;
93+ }
94+
8895 _readLineProvider = new ReadLineProvider ( loggerFactory ) ;
8996 _taskQueue = new BlockingConcurrentDeque < ISynchronousTask > ( ) ;
9097 _psFrameStack = new Stack < PowerShellContextFrame > ( ) ;
@@ -212,7 +219,7 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio
212219 await ExecuteDelegateAsync (
213220 "LoadProfiles" ,
214221 new PowerShellExecutionOptions { MustRunInForeground = true , ThrowOnError = false } ,
215- ( pwsh , delegateCancellation ) => pwsh . LoadProfiles ( _hostInfo . ProfilePaths ) ,
222+ ( pwsh , _ ) => pwsh . LoadProfiles ( _hostInfo . ProfilePaths ) ,
216223 cancellationToken ) . ConfigureAwait ( false ) ;
217224
218225 _logger . LogInformation ( "Profiles loaded" ) ;
@@ -747,7 +754,7 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
747754
748755 pwsh . ImportModule ( s_commandsModulePath ) ;
749756
750- if ( hostStartupInfo . AdditionalModules != null && hostStartupInfo . AdditionalModules . Count > 0 )
757+ if ( hostStartupInfo . AdditionalModules ? . Count > 0 )
751758 {
752759 foreach ( string module in hostStartupInfo . AdditionalModules )
753760 {
@@ -931,7 +938,7 @@ private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsic
931938 psrlReadLine = null ;
932939 try
933940 {
934- var psrlProxy = PSReadLineProxy . LoadAndCreate ( _loggerFactory , pwsh ) ;
941+ var psrlProxy = PSReadLineProxy . LoadAndCreate ( _loggerFactory , s_bundledModulePath , pwsh ) ;
935942 psrlReadLine = new PsrlReadLine ( psrlProxy , this , engineIntrinsics , ReadKey , OnPowerShellIdle ) ;
936943 return true ;
937944 }
0 commit comments