Skip to content

Commit fa6a043

Browse files
Fix piping to native commands for Windows PowerShell (#1836)
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`.
1 parent 6756bf8 commit fa6a043

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

PowerShellEditorServices.build.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $script:IsNix = $IsLinux -or $IsMacOS
3333
# For Apple M1, pwsh might be getting emulated, in which case we need to check
3434
# for the proc_translated flag, otherwise we can check the architecture.
3535
$script:IsAppleM1 = $IsMacOS -and ((sysctl -n sysctl.proc_translated) -eq 1 -or (uname -m) -eq "arm64")
36-
$script:IsArm64 = $IsWindows -and @("ARM64", "AMD64") -contains $env:PROCESSOR_ARCHITECTURE
36+
$script:IsArm64 = -not $script:IsNix -and @("ARM64", "AMD64") -contains $env:PROCESSOR_ARCHITECTURE
3737
$script:BuildInfoPath = [System.IO.Path]::Combine($PSScriptRoot, "src", "PowerShellEditorServices.Hosting", "BuildInfo.cs")
3838
$script:PsesCommonProps = [xml](Get-Content -Raw "$PSScriptRoot/PowerShellEditorServices.Common.props")
3939

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

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO;
88
using System.Management.Automation.Host;
99
using System.Reflection;
10-
using System.Text;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using Microsoft.Extensions.Logging;
@@ -981,8 +980,6 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
981980

982981
readLineProvider.OverrideReadLine(readLine);
983982
System.Console.CancelKeyPress += OnCancelKeyPress;
984-
System.Console.InputEncoding = Encoding.UTF8;
985-
System.Console.OutputEncoding = Encoding.UTF8;
986983
}
987984

988985
if (VersionUtils.IsWindows)

test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,15 @@ public async Task CanLoadPSReadLine()
172172
out IReadLine readLine),
173173
CancellationToken.None).ConfigureAwait(true));
174174
}
175+
176+
// This test asserts that we do not mess up the console encoding, which leads to native
177+
// commands receiving piped input failing.
178+
[Fact]
179+
public async Task ExecutesNativeCommandsCorrectly()
180+
{
181+
await psesHost.ExecutePSCommandAsync(
182+
new PSCommand().AddScript("\"protocol=https`nhost=myhost.com`nusername=john`npassword=doe`n`n\" | git.exe credential approve; if ($LastExitCode) { throw }"),
183+
CancellationToken.None).ConfigureAwait(true);
184+
}
175185
}
176186
}

0 commit comments

Comments
 (0)