-
Notifications
You must be signed in to change notification settings - Fork 234
Run new PSReadLine test on Windows #1522
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 all commits
361b29e
3c4453d
6d5e709
41de2c8
f54d6b6
773e8b6
f911ef1
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 |
---|---|---|
|
@@ -17,20 +17,6 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext | |
|
||
internal class PSReadLinePromptContext : IPromptContext | ||
{ | ||
private static readonly string _psReadLineModulePath = Path.Combine( | ||
Path.GetDirectoryName(typeof(PSReadLinePromptContext).Assembly.Location), | ||
"..", | ||
"..", | ||
"..", | ||
#if TEST | ||
// When using xUnit (dotnet test) the assemblies are deployed to the | ||
// test project folder, invalidating our relative path assumption. | ||
"..", | ||
"..", | ||
"module", | ||
#endif | ||
"PSReadLine"); | ||
|
||
private static readonly Lazy<CmdletInfo> s_lazyInvokeReadLineForEditorServicesCmdletInfo = new Lazy<CmdletInfo>(() => | ||
{ | ||
var type = Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineForEditorServicesCommand, Microsoft.PowerShell.EditorServices.Hosting"); | ||
|
@@ -79,6 +65,7 @@ internal PSReadLinePromptContext( | |
internal static bool TryGetPSReadLineProxy( | ||
ILogger logger, | ||
Runspace runspace, | ||
string bundledModulePath, | ||
out PSReadLineProxy readLineProxy) | ||
{ | ||
readLineProxy = null; | ||
|
@@ -87,15 +74,33 @@ internal static bool TryGetPSReadLineProxy( | |
{ | ||
pwsh.Runspace = runspace; | ||
pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module") | ||
.AddParameter("Name", _psReadLineModulePath) | ||
.AddParameter("Name", Path.Combine(bundledModulePath, "PSReadLine")) | ||
.Invoke(); | ||
|
||
if (pwsh.HadErrors) | ||
{ | ||
logger.LogWarning("PSConsoleReadline type not found: {Reason}", pwsh.Streams.Error[0].ToString()); | ||
return false; | ||
} | ||
|
||
var psReadLineType = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2"); | ||
|
||
if (psReadLineType == null) | ||
{ | ||
logger.LogWarning("PSConsoleReadline type not found: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : "<Unknown reason>"); | ||
return false; | ||
// NOTE: For some reason `Type.GetType(...)` can fail to find the type, | ||
// and in that case, this search through the `AppDomain` for some reason will succeed. | ||
// It's slower, but only happens when needed. | ||
logger.LogTrace("PSConsoleReadline type not found using Type.GetType(), searching all loaded assemblies..."); | ||
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. @rjmholt This should only happen in the case that it needs to. When 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. Not saying it's "right" but I keep seeing code similar to this come up when I research the issue. I think we will get to the bottom of it as we continue to refactor but this works. 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. My suspicion is that this always worked in script because it always runs on the pipeline thread and through PowerShell's own type search logic. When we sift through the whole AppDomain (which is kind of a dead concept) for a type, we ignore assembly load contexts, and so that might be why the direction reflection query fails but the search succeeds. Just a theory though. An ALC shouldn't be thread-dependent though and I don't think anyone is using something like |
||
psReadLineType = AppDomain.CurrentDomain | ||
.GetAssemblies() | ||
.FirstOrDefault(asm => asm.GetName().Name.Equals("Microsoft.PowerShell.PSReadLine2")) | ||
?.ExportedTypes | ||
?.FirstOrDefault(type => type.FullName.Equals("Microsoft.PowerShell.PSConsoleReadLine")); | ||
if (psReadLineType == null) | ||
{ | ||
logger.LogWarning("PSConsoleReadLine type not found anywhere!"); | ||
return false; | ||
} | ||
} | ||
|
||
try | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"parallelizeTestCollections": false | ||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", | ||
"appDomain": "denied", | ||
"parallelizeTestCollections": false, | ||
"methodDisplay": "method" | ||
} | ||
|
This file was deleted.
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.
@dkattan So we resurrected the
SetExecutionPolicy()
function because on closer inspection, the actual issue was that in the unit test scenario we weren't havingBypass
set (because the codebase's expectation is that the startup script is run with-ExecutionPolicy Bypass
, like what the extension does), and theSetExecutionPolicy()
actually sets the execution policy back to the original inherited scoped policy, not the process policy. That is, it override's the process/runspace's policy after everything is loaded to be equivalent to the user's machine/user policy.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.
@rjmholt I might rename that function
ResetExecutionPolicy()
😆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.
That works. I'm a fan of removing large chunks of code, as long as it doesn't change expected functionality :D