Skip to content

Commit e46bb89

Browse files
Search all assemblies for PSConsoleReadLine
This is sometimes necessary (especially in CI). Co-authored-by: Andrew Schwartzmeyer <[email protected]>
1 parent d03f3d0 commit e46bb89

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs

+20-4
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,37 @@ internal static bool TryGetPSReadLineProxy(
8989
{
9090
readLineProxy = null;
9191
logger.LogTrace("Attempting to load PSReadLine");
92-
Console.WriteLine($"Module path is {_psReadLineTestModulePath}");
9392
using (var pwsh = PowerShell.Create())
9493
{
9594
pwsh.Runspace = runspace;
9695
pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
9796
.AddParameter("Name", testing ? _psReadLineTestModulePath : _psReadLineModulePath)
9897
.Invoke();
9998

99+
if (pwsh.HadErrors)
100+
{
101+
logger.LogWarning("PSConsoleReadline type not found: {Reason}", pwsh.Streams.Error[0].ToString());
102+
return false;
103+
}
104+
100105
var psReadLineType = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2");
101106

102107
if (psReadLineType == null)
103108
{
104-
logger.LogWarning("PSConsoleReadline type not found: {Reason}", pwsh.HadErrors ? pwsh.Streams.Error[0].ToString() : "<Unknown reason>");
105-
Console.WriteLine("Failed to GetType but no PowerShell error");
106-
return false;
109+
// NOTE: For some reason `Type.GetType(...)` can fail to find the type,
110+
// and in that case, this search through the `AppDomain` for some reason will succeed.
111+
// It's slower, but only happens when needed.
112+
logger.LogTrace("PSConsoleReadline type not found using Type.GetType(), searching all loaded assemblies...");
113+
psReadLineType = AppDomain.CurrentDomain
114+
.GetAssemblies()
115+
.FirstOrDefault(asm => asm.GetName().Name.Equals("Microsoft.PowerShell.PSReadLine2"))
116+
?.ExportedTypes
117+
?.FirstOrDefault(type => type.FullName.Equals("Microsoft.PowerShell.PSConsoleReadLine"));
118+
if (psReadLineType == null)
119+
{
120+
logger.LogWarning("PSConsoleReadLine type not found anywhere!");
121+
return false;
122+
}
107123
}
108124

109125
try

0 commit comments

Comments
 (0)