@@ -22,26 +22,15 @@ internal class PSReadLinePromptContext : IPromptContext
22
22
".." ,
23
23
".." ,
24
24
".." ,
25
+ #if TEST
26
+ // When using xUnit (dotnet test) the assemblies are deployed to the
27
+ // test project folder, invalidating our relative path assumption.
28
+ ".." ,
29
+ ".." ,
30
+ "module" ,
31
+ #endif
25
32
"PSReadLine" ) ;
26
33
27
- private static readonly string ReadLineInitScript = $@ "
28
- [System.Diagnostics.DebuggerHidden()]
29
- [System.Diagnostics.DebuggerStepThrough()]
30
- param()
31
- end {{
32
- $module = Get-Module -ListAvailable PSReadLine |
33
- Where-Object {{ $_.Version -ge '2.0.2' }} |
34
- Sort-Object -Descending Version |
35
- Select-Object -First 1
36
- if (-not $module) {{
37
- Import-Module '{ _psReadLineModulePath . Replace ( "'" , "''" ) } '
38
- return [Microsoft.PowerShell.PSConsoleReadLine]
39
- }}
40
-
41
- Import-Module -ModuleInfo $module
42
- return [Microsoft.PowerShell.PSConsoleReadLine]
43
- }}" ;
44
-
45
34
private static readonly Lazy < CmdletInfo > s_lazyInvokeReadLineForEditorServicesCmdletInfo = new Lazy < CmdletInfo > ( ( ) =>
46
35
{
47
36
var type = Type . GetType ( "Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineForEditorServicesCommand, Microsoft.PowerShell.EditorServices.Hosting" ) ;
@@ -97,14 +86,15 @@ internal static bool TryGetPSReadLineProxy(
97
86
using ( var pwsh = PowerShell . Create ( ) )
98
87
{
99
88
pwsh . Runspace = runspace ;
100
- var psReadLineType = pwsh
101
- . AddScript ( ReadLineInitScript , useLocalScope : true )
102
- . Invoke < Type > ( )
103
- . FirstOrDefault ( ) ;
89
+ pwsh . AddCommand ( "Microsoft.PowerShell.Core\\ Import-Module" )
90
+ . AddParameter ( "Name" , _psReadLineModulePath )
91
+ . Invoke ( ) ;
92
+
93
+ var psReadLineType = Type . GetType ( "Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2" ) ;
104
94
105
95
if ( psReadLineType == null )
106
96
{
107
- logger . LogWarning ( "PSReadLine unable to be loaded : {Reason}" , pwsh . HadErrors ? pwsh . Streams . Error [ 0 ] . ToString ( ) : "<Unknown reason>" ) ;
97
+ logger . LogWarning ( "PSConsoleReadline type not found : {Reason}" , pwsh . HadErrors ? pwsh . Streams . Error [ 0 ] . ToString ( ) : "<Unknown reason>" ) ;
108
98
return false ;
109
99
}
110
100
@@ -117,7 +107,7 @@ internal static bool TryGetPSReadLineProxy(
117
107
// The Type we got back from PowerShell doesn't have the members we expected.
118
108
// Could be an older version, a custom build, or something a newer version with
119
109
// breaking changes.
120
- logger . LogWarning ( "PSReadLine unable to be loaded : {Reason}" , e ) ;
110
+ logger . LogWarning ( "PSReadLineProxy unable to be initialized : {Reason}" , e ) ;
121
111
return false ;
122
112
}
123
113
}
0 commit comments