From af953e235d93920f1064132be42f7f3c20c99022 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 21 Jun 2022 13:43:31 -0700 Subject: [PATCH] Fix piping to native commands for Windows PowerShell For an unknown reason, we were explicitly overriding `Console.InputEncoding`, which led to a bug where input piped to a native command in Windows PowerShell (where the default encoding is different than our override) would fail. Instead, we just don't override the encodings at all, which lets PSES be closer to the expected experience. The regression test covers this, though to test the failure I had to move that override outside its guard (as it wasn't being set during tests). Also fix building on ARM64 in Windows PowerShell, which doesn't have `$IsWindows`. --- PowerShellEditorServices.build.ps1 | 2 +- .../Services/PowerShell/Host/PsesInternalHost.cs | 3 --- .../Session/PsesInternalHostTests.cs | 10 ++++++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/PowerShellEditorServices.build.ps1 b/PowerShellEditorServices.build.ps1 index 76687aa65..6bde9dd8a 100644 --- a/PowerShellEditorServices.build.ps1 +++ b/PowerShellEditorServices.build.ps1 @@ -33,7 +33,7 @@ $script:IsNix = $IsLinux -or $IsMacOS # For Apple M1, pwsh might be getting emulated, in which case we need to check # for the proc_translated flag, otherwise we can check the architecture. $script:IsAppleM1 = $IsMacOS -and ((sysctl -n sysctl.proc_translated) -eq 1 -or (uname -m) -eq "arm64") -$script:IsArm64 = $IsWindows -and @("ARM64", "AMD64") -contains $env:PROCESSOR_ARCHITECTURE +$script:IsArm64 = -not $script:IsNix -and @("ARM64", "AMD64") -contains $env:PROCESSOR_ARCHITECTURE $script:BuildInfoPath = [System.IO.Path]::Combine($PSScriptRoot, "src", "PowerShellEditorServices.Hosting", "BuildInfo.cs") $script:PsesCommonProps = [xml](Get-Content -Raw "$PSScriptRoot/PowerShellEditorServices.Common.props") diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index 8dcce509b..69b9f0d72 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -7,7 +7,6 @@ using System.IO; using System.Management.Automation.Host; using System.Reflection; -using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -981,8 +980,6 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace) readLineProvider.OverrideReadLine(readLine); System.Console.CancelKeyPress += OnCancelKeyPress; - System.Console.InputEncoding = Encoding.UTF8; - System.Console.OutputEncoding = Encoding.UTF8; } if (VersionUtils.IsWindows) diff --git a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs index b3cd602b6..179da5f8c 100644 --- a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs +++ b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs @@ -172,5 +172,15 @@ public async Task CanLoadPSReadLine() out IReadLine readLine), CancellationToken.None).ConfigureAwait(true)); } + + // This test asserts that we do not mess up the console encoding, which leads to native + // commands receiving piped input failing. + [Fact] + public async Task ExecutesNativeCommandsCorrectly() + { + await psesHost.ExecutePSCommandAsync( + new PSCommand().AddScript("\"protocol=https`nhost=myhost.com`nusername=john`npassword=doe`n`n\" | git.exe credential approve; if ($LastExitCode) { throw }"), + CancellationToken.None).ConfigureAwait(true); + } } }