Skip to content

Commit 6ebadcc

Browse files
dkattanandyleejordan
authored andcommitted
Add UseNullPSHostUI config so apps hosting PSES can disable it
Even if they're using stdio.
1 parent 6361eb2 commit 6ebadcc

File tree

8 files changed

+29
-5
lines changed

8 files changed

+29
-5
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, // If Stdio is used we can't write anything else out
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

+1
Original file line numberDiff line numberDiff line change
@@ -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
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.
@@ -139,6 +144,7 @@ public sealed class HostStartupInfo
139144
/// <param name="logPath">The path to log to.</param>
140145
/// <param name="logLevel">The minimum log event level.</param>
141146
/// <param name="consoleReplEnabled">Enable console if true.</param>
147+
/// <param name="useNullPSHostUI">Whether or not to use the Null UI.</param>
142148
/// <param name="usesLegacyReadLine">Use PSReadLine if false, otherwise use the legacy readline implementation.</param>
143149
/// <param name="bundledModulePath">A custom path to the expected bundled modules.</param>
144150
public HostStartupInfo(
@@ -153,6 +159,7 @@ public HostStartupInfo(
153159
string logPath,
154160
int logLevel,
155161
bool consoleReplEnabled,
162+
bool useNullPSHostUI,
156163
bool usesLegacyReadLine,
157164
string bundledModulePath)
158165
{
@@ -167,6 +174,7 @@ public HostStartupInfo(
167174
LogPath = logPath;
168175
LogLevel = logLevel;
169176
ConsoleReplEnabled = consoleReplEnabled;
177+
UseNullPSHostUI = useNullPSHostUI;
170178
UsesLegacyReadLine = usesLegacyReadLine;
171179

172180
// Respect a user provided bundled module path.

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;

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,
6061
bundledModulePath: BundledModulePath);
6162

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

test/PowerShellEditorServices.Test/Services/Symbols/PSScriptAnalyzerTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PSScriptAnalyzerTests
3636
logPath: null,
3737
logLevel: 0,
3838
consoleReplEnabled: false,
39+
useNullPSHostUI: true,
3940
usesLegacyReadLine: false,
4041
bundledModulePath: PsesHostFactory.BundledModulePath));
4142

0 commit comments

Comments
 (0)