-
Notifications
You must be signed in to change notification settings - Fork 237
Honor BundledModulesPath when loading modules in PowerShellContextService #1516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
26818b3
2888045
d0bc52d
e590fad
52b16ca
d8738f0
ee154b0
b91382b
77186f0
48d1a96
7037115
a0fa3c5
1443baa
bda11be
bda40ea
01957e6
9a045d9
e1e70cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,18 @@ namespace Microsoft.PowerShell.EditorServices.Services | |
/// </summary> | ||
internal class PowerShellContextService : IHostSupportsInteractiveSession | ||
{ | ||
private static readonly string s_commandsModulePath = Path.GetFullPath( | ||
private static string s_bundledModulesPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."); | ||
|
||
private static string s_commandsModulePath => Path.GetFullPath( | ||
Path.Combine( | ||
s_bundledModulesPath, | ||
"PowerShellEditorServices", | ||
"Commands", | ||
"PowerShellEditorServices.Commands.psd1")); | ||
private static string _psReadLineModulePath => Path.GetFullPath( | ||
Path.Combine( | ||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), | ||
"../../Commands/PowerShellEditorServices.Commands.psd1")); | ||
s_bundledModulesPath, | ||
"PSReadLine")); | ||
|
||
private static readonly Action<Runspace, ApartmentState> s_runspaceApartmentStateSetter; | ||
private static readonly PropertyInfo s_writeStreamProperty; | ||
|
@@ -190,7 +198,10 @@ public static PowerShellContextService Create( | |
HostStartupInfo hostStartupInfo) | ||
{ | ||
Validate.IsNotNull(nameof(hostStartupInfo), hostStartupInfo); | ||
|
||
s_bundledModulesPath = !string.IsNullOrEmpty(hostStartupInfo.BundledModulePath) && Directory.Exists(hostStartupInfo.BundledModulePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here if a valid path was specified, we use it, otherwise we fallback to the previous logic. |
||
? hostStartupInfo.BundledModulePath | ||
: s_bundledModulesPath; | ||
|
||
var logger = factory.CreateLogger<PowerShellContextService>(); | ||
|
||
bool shouldUsePSReadLine = hostStartupInfo.ConsoleReplEnabled | ||
|
@@ -215,12 +226,18 @@ public static PowerShellContextService Create( | |
|
||
logger.LogTrace("Creating initial PowerShell runspace"); | ||
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.LanguageMode); | ||
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface); | ||
powerShellContext.Initialize(hostStartupInfo, initialRunspace, true, hostUserInterface); | ||
powerShellContext.ImportCommandsModuleAsync(); | ||
|
||
// TODO: This can be moved to the point after the $psEditor object | ||
// gets initialized when that is done earlier than LanguageServer.Initialize | ||
foreach (string module in hostStartupInfo.AdditionalModules) | ||
var modulesToImport = new List<string>(); | ||
if(hostStartupInfo.ConsoleReplEnabled) | ||
{ | ||
modulesToImport.Add(_psReadLineModulePath); | ||
} | ||
modulesToImport.AddRange(hostStartupInfo.AdditionalModules); | ||
foreach (string module in modulesToImport) | ||
{ | ||
var command = | ||
new PSCommand() | ||
|
@@ -305,7 +322,7 @@ public static Runspace CreateRunspace(PSHost psHost, PSLanguageMode languageMode | |
/// <param name="ownsInitialRunspace">If true, the PowerShellContext owns this runspace.</param> | ||
/// <param name="consoleHost">An IHostOutput implementation. Optional.</param> | ||
public void Initialize( | ||
ProfilePathInfo profilePaths, | ||
HostStartupInfo hostStartupInfo, | ||
Runspace initialRunspace, | ||
bool ownsInitialRunspace, | ||
IHostOutput consoleHost) | ||
|
@@ -359,7 +376,7 @@ public void Initialize( | |
this.ConfigureRunspaceCapabilities(this.CurrentRunspace); | ||
|
||
// Set the $profile variable in the runspace | ||
this.profilePaths = profilePaths; | ||
this.profilePaths = hostStartupInfo.ProfilePaths; | ||
if (profilePaths != null) | ||
{ | ||
this.SetProfileVariableInCurrentRunspace(profilePaths); | ||
|
@@ -396,7 +413,7 @@ public void Initialize( | |
|
||
if (powerShellVersion.Major >= 5 && | ||
this.isPSReadLineEnabled && | ||
PSReadLinePromptContext.TryGetPSReadLineProxy(logger, initialRunspace, out PSReadLineProxy proxy)) | ||
PSReadLinePromptContext.TryGetPSReadLineProxy(logger, initialRunspace, hostStartupInfo.BundledModulePath, out PSReadLineProxy proxy)) | ||
{ | ||
this.PromptContext = new PSReadLinePromptContext( | ||
this, | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||||||||||||
using System.IO; | ||||||||||||||||||||||||||||||||
using System.Linq; | ||||||||||||||||||||||||||||||||
using System.Management.Automation; | ||||||||||||||||||||||||||||||||
using System.Reflection; | ||||||||||||||||||||||||||||||||
using System.Runtime.InteropServices; | ||||||||||||||||||||||||||||||||
using System.Threading.Tasks; | ||||||||||||||||||||||||||||||||
using Microsoft.Extensions.Logging.Abstractions; | ||||||||||||||||||||||||||||||||
|
@@ -152,10 +153,19 @@ await this.powerShellContext.ExecuteCommandAsync<string>( | |||||||||||||||||||||||||||||||
public async Task CanGetPSReadLineProxy() | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
Skip.If(IsWindows, "This test doesn't work on Windows for some reason."); | ||||||||||||||||||||||||||||||||
string s_bundledModulesPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), | ||||||||||||||||||||||||||||||||
"..", | ||||||||||||||||||||||||||||||||
"..", | ||||||||||||||||||||||||||||||||
"..", | ||||||||||||||||||||||||||||||||
"..", | ||||||||||||||||||||||||||||||||
"module" | ||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||
System.Console.WriteLine($"Attempting to import {s_bundledModulesPath}"); | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI I am debugging this test on Windows right now, really want to know why it worked everywhere but there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah, me too. I think this last change fixed it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this PR should be the first to go in. It made sense to bring in the Changes from #1520 so I'll close that PR as this handles both issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah maybe not, PowerShell 7 | Windows 10 has already failed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. macOS: Windows: executingAssembly C:\Users\VssAdministrator\AppData\Local\Temp\b5b3251f-c622-45f2-bc10-c8a646797337\b5b3251f-c622-45f2-bc10-c8a646797337\assembly\dl3\e3727c32\64017e66_b772d701\Microsoft.PowerShell.EditorServices.Test.dll https://stackoverflow.com/questions/8309411/what-is-cache-appdata-local-assembly-dl3 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evidently we shouldn't be using GetExecutingAssembly().Location as this can return a Volume Shadow Copy cached version of the DLL. We're using it in 9 other places...
|
||||||||||||||||||||||||||||||||
Assert.True(PSReadLinePromptContext.TryGetPSReadLineProxy( | ||||||||||||||||||||||||||||||||
NullLogger.Instance, | ||||||||||||||||||||||||||||||||
PowerShellContextFactory.initialRunspace, | ||||||||||||||||||||||||||||||||
out PSReadLineProxy proxy)); | ||||||||||||||||||||||||||||||||
NullLogger.Instance, | ||||||||||||||||||||||||||||||||
PowerShellContextFactory.initialRunspace, | ||||||||||||||||||||||||||||||||
s_bundledModulesPath, | ||||||||||||||||||||||||||||||||
out PSReadLineProxy proxy)); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
#region Helper Methods | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is impetus for the change. We are referencing a path relative to the executing assembly instead of honoring the BundledModulePath parameter.