Skip to content
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

Add UseNullPSHostUI config so apps hosting PSES can disable it #2122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ private EditorServicesConfig CreateConfigObject()
FeatureFlags = FeatureFlags,
LogLevel = LogLevel,
ConsoleRepl = GetReplKind(),
UseNullPSHostUI = Stdio, // If Stdio is used we can't write anything else out
AdditionalModules = AdditionalModules,
LanguageServiceTransport = GetLanguageServiceTransport(),
DebugServiceTransport = GetDebugServiceTransport(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public EditorServicesConfig(
/// </summary>
public ConsoleReplKind ConsoleRepl { get; set; } = ConsoleReplKind.None;

/// <summary>
/// Will suppress messages to PSHost (to prevent Stdio clobbering)
/// </summary>
public bool UseNullPSHostUI { get; set; }

/// <summary>
/// The minimum log level to log events with.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private HostStartupInfo CreateHostStartupInfo()
_config.LogPath,
(int)_config.LogLevel,
consoleReplEnabled: _config.ConsoleRepl != ConsoleReplKind.None,
useNullPSHostUI: _config.UseNullPSHostUI,
usesLegacyReadLine: _config.ConsoleRepl == ConsoleReplKind.LegacyReadLine,
bundledModulePath: _config.BundledModulePath);
}
Expand Down
8 changes: 8 additions & 0 deletions src/PowerShellEditorServices/Hosting/HostStartupInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public sealed class HostStartupInfo
/// </summary>
public bool ConsoleReplEnabled { get; }

/// <summary>
/// True if we want to suppress messages to PSHost (to prevent Stdio clobbering)
/// </summary>
public bool UseNullPSHostUI { get; }

/// <summary>
/// If true, the legacy PSES readline implementation will be used. Otherwise PSReadLine will be used.
/// If the console REPL is not enabled, this setting will be ignored.
Expand Down Expand Up @@ -139,6 +144,7 @@ public sealed class HostStartupInfo
/// <param name="logPath">The path to log to.</param>
/// <param name="logLevel">The minimum log event level.</param>
/// <param name="consoleReplEnabled">Enable console if true.</param>
/// <param name="useNullPSHostUI">Whether or not to use the Null UI.</param>
/// <param name="usesLegacyReadLine">Use PSReadLine if false, otherwise use the legacy readline implementation.</param>
/// <param name="bundledModulePath">A custom path to the expected bundled modules.</param>
public HostStartupInfo(
Expand All @@ -153,6 +159,7 @@ public HostStartupInfo(
string logPath,
int logLevel,
bool consoleReplEnabled,
bool useNullPSHostUI,
bool usesLegacyReadLine,
string bundledModulePath)
{
Expand All @@ -167,6 +174,7 @@ public HostStartupInfo(
LogPath = logPath;
LogLevel = logLevel;
ConsoleReplEnabled = consoleReplEnabled;
UseNullPSHostUI = useNullPSHostUI;
UsesLegacyReadLine = usesLegacyReadLine;

// Respect a user provided bundled module path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,17 @@ private PowerShellResult InvokePowerShell(PSCommand command)
catch (CmdletInvocationException ex)
{
// We do not want to crash EditorServices for exceptions caused by cmdlet invocation.
// Two main reasons that cause the exception are:
// The main reasons that cause the exception are:
// * PSCmdlet.WriteOutput being called from another thread than Begin/Process
// * CompositionContainer.ComposeParts complaining that "...Only one batch can be composed at a time"
_logger.LogError(ex.Message);
// * PSScriptAnalyzer not being able to find its PSScriptAnalyzer.psd1 because we are hosted by an Assembly other than pwsh.exe
andyleejordan marked this conversation as resolved.
Show resolved Hide resolved
string message = ex.Message;
if (!string.IsNullOrEmpty(ex.ErrorRecord.FullyQualifiedErrorId))
{
// Microsoft.PowerShell.EditorServices.Services.Analysis.PssaCmdletAnalysisEngine: Exception of type 'System.Exception' was thrown. |
message += $" | {ex.ErrorRecord.FullyQualifiedErrorId}";
}
_logger.LogError(message);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public PsesInternalHost(
Version = hostInfo.Version;

DebugContext = new PowerShellDebugContext(loggerFactory, this);
UI = hostInfo.ConsoleReplEnabled
? new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI)
: new NullPSHostUI();
UI = hostInfo.UseNullPSHostUI
? new NullPSHostUI()
: new EditorServicesConsolePSHostUserInterface(loggerFactory, hostInfo.PSHost.UI);
}

public override CultureInfo CurrentCulture => _hostInfo.PSHost.CurrentCulture;
Expand Down
1 change: 1 addition & 0 deletions test/PowerShellEditorServices.Test/PsesHostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory, bool loadPro
logLevel: (int)LogLevel.None,
consoleReplEnabled: false,
usesLegacyReadLine: false,
useNullPSHostUI: true,
bundledModulePath: BundledModulePath);

PsesInternalHost psesHost = new(loggerFactory, null, testHostDetails);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class PSScriptAnalyzerTests
logPath: null,
logLevel: 0,
consoleReplEnabled: false,
useNullPSHostUI: true,
usesLegacyReadLine: false,
bundledModulePath: PsesHostFactory.BundledModulePath));

Expand Down
Loading