Skip to content

Commit 96a8207

Browse files
Fix BundledModulePath and PSReadLine loading (redux) (#1638)
This redoes prior work that was lost during the rewrite. Specifically this actually respects the user configuration of `BundledModulePath` (also used by unit tests to provide compatibililty with xUnit), and forces the use of only our bundled PSReadLine dependency. Essentially this redoes #1514 and #1522.
1 parent 6986cf4 commit 96a8207

File tree

2 files changed

+29
-45
lines changed

2 files changed

+29
-45
lines changed

src/PowerShellEditorServices/Services/PowerShell/Console/PSReadLineProxy.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,14 @@ internal class PSReadLineProxy
4444

4545
private static readonly Type[] s_addToHistoryTypes = { typeof(string) };
4646

47-
private static readonly string _psReadLineModulePath = Path.GetFullPath(
48-
Path.Combine(
49-
Path.GetDirectoryName(typeof(PSReadLineProxy).Assembly.Location),
50-
"..",
51-
"..",
52-
"..",
53-
"PSReadLine"));
54-
55-
private static readonly string ReadLineInitScript = $@"
56-
[System.Diagnostics.DebuggerHidden()]
57-
[System.Diagnostics.DebuggerStepThrough()]
58-
param()
59-
end {{
60-
$module = Get-Module -ListAvailable PSReadLine |
61-
Where-Object {{ $_.Version -ge '2.2.1' }} |
62-
Sort-Object -Descending Version |
63-
Select-Object -First 1
64-
if (-not $module) {{
65-
Import-Module '{_psReadLineModulePath.Replace("'", "''")}'
66-
return [Microsoft.PowerShell.PSConsoleReadLine]
67-
}}
68-
69-
Import-Module -ModuleInfo $module
70-
return [Microsoft.PowerShell.PSConsoleReadLine]
71-
}}";
72-
7347
public static PSReadLineProxy LoadAndCreate(
7448
ILoggerFactory loggerFactory,
49+
string bundledModulePath,
7550
SMA.PowerShell pwsh)
7651
{
77-
Type psConsoleReadLineType = pwsh.AddScript(ReadLineInitScript).InvokeAndClear<Type>().FirstOrDefault();
52+
pwsh.ImportModule(Path.Combine(bundledModulePath, "PSReadLine"));
53+
Type psConsoleReadLineType = pwsh.AddScript("return [Microsoft.PowerShell.PSConsoleReadLine]")
54+
.InvokeAndClear<Type>().FirstOrDefault();
7855

7956
RuntimeHelpers.RunClassConstructor(psConsoleReadLineType.TypeHandle);
8057

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
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;
104
using System;
115
using System.Collections.Generic;
126
using System.Globalization;
7+
using System.IO;
138
using 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;
1416
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging;
1517
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
18+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace;
1619
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
1720
using 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

2423
namespace 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

Comments
 (0)