Skip to content

Commit e1e3bf2

Browse files
andyleejordandkattan
authored andcommitted
Removed new parameter and switched instead on whether Stdio is in use determine whether or not to use the NullPSHostUI
1 parent 27b279e commit e1e3bf2

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ private EditorServicesConfig CreateConfigObject()
351351
FeatureFlags = FeatureFlags,
352352
LogLevel = LogLevel,
353353
ConsoleRepl = GetReplKind(),
354+
UseNullPSHostUI = Stdio,
354355
AdditionalModules = AdditionalModules,
355356
LanguageServiceTransport = GetLanguageServiceTransport(),
356357
DebugServiceTransport = GetDebugServiceTransport(),

src/PowerShellEditorServices.Hosting/Configuration/EditorServicesConfig.cs

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public EditorServicesConfig(
8989
/// </summary>
9090
public ConsoleReplKind ConsoleRepl { get; set; } = ConsoleReplKind.None;
9191

92+
/// <summary>
93+
/// Will suppress messages to PSHost (to prevent Stdio clobbering)
94+
/// </summary>
95+
public bool UseNullPSHostUI { get; set; }
96+
9297
/// <summary>
9398
/// The minimum log level to log events with.
9499
/// </summary>

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.PowerShell.EditorServices.Server;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Threading.Tasks;
8-
using Microsoft.PowerShell.EditorServices.Server;
99

1010
namespace Microsoft.PowerShell.EditorServices.Hosting
1111
{
@@ -289,6 +289,7 @@ private HostStartupInfo CreateHostStartupInfo()
289289
_config.LogPath,
290290
(int)_config.LogLevel,
291291
consoleReplEnabled: _config.ConsoleRepl != ConsoleReplKind.None,
292+
useNullPSHostUI: _config.UseNullPSHostUI,
292293
usesLegacyReadLine: _config.ConsoleRepl == ConsoleReplKind.LegacyReadLine,
293294
bundledModulePath: _config.BundledModulePath);
294295
}

src/PowerShellEditorServices/Hosting/HostStartupInfo.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public sealed class HostStartupInfo
7676
/// </summary>
7777
public bool ConsoleReplEnabled { get; }
7878

79+
/// <summary>
80+
/// True if we want to suppress messages to PSHost (to prevent Stdio clobbering)
81+
/// </summary>
82+
public bool UseNullPSHostUI { get; }
83+
7984
/// <summary>
8085
/// If true, the legacy PSES readline implementation will be used. Otherwise PSReadLine will be used.
8186
/// If the console REPL is not enabled, this setting will be ignored.
@@ -154,7 +159,8 @@ public HostStartupInfo(
154159
int logLevel,
155160
bool consoleReplEnabled,
156161
bool usesLegacyReadLine,
157-
string bundledModulePath)
162+
string bundledModulePath,
163+
bool useNullPSHostUI)
158164
{
159165
Name = name ?? DefaultHostName;
160166
ProfileId = profileId ?? DefaultHostProfileId;
@@ -177,6 +183,7 @@ public HostStartupInfo(
177183
"..",
178184
"..",
179185
".."));
186+
UseNullPSHostUI = useNullPSHostUI;
180187
}
181188

182189
#endregion

src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,17 @@ private PowerShellResult InvokePowerShell(PSCommand command)
292292
catch (CmdletInvocationException ex)
293293
{
294294
// We do not want to crash EditorServices for exceptions caused by cmdlet invocation.
295-
// Two main reasons that cause the exception are:
295+
// The main reasons that cause the exception are:
296296
// * PSCmdlet.WriteOutput being called from another thread than Begin/Process
297297
// * CompositionContainer.ComposeParts complaining that "...Only one batch can be composed at a time"
298-
_logger.LogError(ex.Message);
298+
// * PSScriptAnalyzer not being able to find its PSScriptAnalyzer.psd1 because we are hosted by an Assembly other than pwsh.exe
299+
string message = ex.Message;
300+
if (!string.IsNullOrEmpty(ex.ErrorRecord.FullyQualifiedErrorId))
301+
{
302+
// Microsoft.PowerShell.EditorServices.Services.Analysis.PssaCmdletAnalysisEngine: Exception of type 'System.Exception' was thrown. |
303+
message += $" | {ex.ErrorRecord.FullyQualifiedErrorId}";
304+
}
305+
_logger.LogError(message);
299306
}
300307

301308
return result;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ public PsesInternalHost(
193193
Version = hostInfo.Version;
194194

195195
DebugContext = new PowerShellDebugContext(loggerFactory, this);
196-
UI = hostInfo.ConsoleReplEnabled
197-
? new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI)
198-
: new NullPSHostUI();
196+
UI = hostInfo.UseNullPSHostUI
197+
? new NullPSHostUI()
198+
: new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI);
199199
}
200200

201201
public override CultureInfo CurrentCulture => _hostInfo.PSHost.CurrentCulture;

src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
1414
{
15+
using System.Collections;
1516
using System.Management.Automation;
1617

1718
internal static class PowerShellExtensions
@@ -73,6 +74,10 @@ public static Collection<TResult> InvokeAndClear<TResult>(this PowerShell pwsh,
7374
{
7475
try
7576
{
77+
if (pwsh.Runspace.SessionStateProxy.PSVariable.GetValue("error") is ArrayList errors)
78+
{
79+
errors.Clear();
80+
}
7681
return pwsh.Invoke<TResult>(input: null, invocationSettings);
7782
}
7883
finally

test/PowerShellEditorServices.Test/PsesHostFactory.cs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory, bool loadPro
5757
logLevel: (int)LogLevel.None,
5858
consoleReplEnabled: false,
5959
usesLegacyReadLine: false,
60+
useNullPSHostUI: true, // Preserve previous functionality
6061
bundledModulePath: BundledModulePath);
6162

6263
PsesInternalHost psesHost = new(loggerFactory, null, testHostDetails);

0 commit comments

Comments
 (0)