Skip to content

Commit 30f0156

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 30f0156

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,35 @@ 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+
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
114+
var assemblies = allAssemblies.FirstOrDefault(a => a.FullName.Contains("Microsoft.PowerShell.PSReadLine2"));
115+
psReadLineType = assemblies?.ExportedTypes?.FirstOrDefault(a => a.FullName == "Microsoft.PowerShell.PSConsoleReadLine");
116+
if (psReadLineType == null)
117+
{
118+
logger.LogWarning("PSConsoleReadLine type not found anywhere!");
119+
return false;
120+
}
107121
}
108122

109123
try

0 commit comments

Comments
 (0)