Skip to content

Commit

Permalink
Removed new parameter and switched instead on whether Stdio is in use…
Browse files Browse the repository at this point in the history
… determine whether or not to use the NullPSHostUI
  • Loading branch information
andyleejordan authored and dkattan committed Jan 4, 2024
1 parent 27b279e commit e1e3bf2
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
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,
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 sealed class 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
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Server;

namespace Microsoft.PowerShell.EditorServices.Hosting
{
Expand Down 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
9 changes: 8 additions & 1 deletion 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 @@ -154,7 +159,8 @@ public sealed class HostStartupInfo
int logLevel,
bool consoleReplEnabled,
bool usesLegacyReadLine,
string bundledModulePath)
string bundledModulePath,
bool useNullPSHostUI)
{
Name = name ?? DefaultHostName;
ProfileId = profileId ?? DefaultHostProfileId;
Expand All @@ -177,6 +183,7 @@ public sealed class HostStartupInfo
"..",
"..",
".."));
UseNullPSHostUI = useNullPSHostUI;
}

#endregion
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
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 @@ static 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
{
using System.Collections;
using System.Management.Automation;

internal static class PowerShellExtensions
Expand Down Expand Up @@ -73,6 +74,10 @@ public static Collection<TResult> InvokeAndClear<TResult>(this PowerShell pwsh,
{
try
{
if (pwsh.Runspace.SessionStateProxy.PSVariable.GetValue("error") is ArrayList errors)
{
errors.Clear();
}
return pwsh.Invoke<TResult>(input: null, invocationSettings);
}
finally
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, // Preserve previous functionality
bundledModulePath: BundledModulePath);

PsesInternalHost psesHost = new(loggerFactory, null, testHostDetails);
Expand Down

0 comments on commit e1e3bf2

Please sign in to comment.