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 PSApplicationOutputEncoding variable #21219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4527,6 +4527,14 @@ static InitialSessionState()
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding))),

// Variable which controls the encoding for decoding data from a NativeCommand
new SessionStateVariableEntry(
SpecialVariables.PSApplicationOutputEncoding,
null,
RunspaceInit.PSApplicationOutputEncodingDescription,
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(Encoding))),

// Preferences
//
// NTRAID#Windows Out Of Band Releases-931461-2006/03/13
Expand Down
19 changes: 17 additions & 2 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Expand Up @@ -1433,16 +1433,17 @@ NativeParameterBinderController.ArgumentPassingStyle switch
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = redirectInput;

Encoding outputEncoding = GetOutputEncoding();
if (redirectOutput)
{
startInfo.RedirectStandardOutput = true;
startInfo.StandardOutputEncoding = Console.OutputEncoding;
startInfo.StandardOutputEncoding = outputEncoding;
}

if (redirectError)
{
startInfo.RedirectStandardError = true;
startInfo.StandardErrorEncoding = Console.OutputEncoding;
startInfo.StandardErrorEncoding = outputEncoding;
}
}

Expand Down Expand Up @@ -1499,6 +1500,20 @@ NativeParameterBinderController.ArgumentPassingStyle switch
return startInfo;
}

#nullable enable
/// <summary>
/// Gets the encoding to use for a process' output/error pipes.
/// </summary>
/// <returns>The encoding to use for the process output.</returns>
private Encoding GetOutputEncoding()
{
Encoding? applicationOutputEncoding = Context.GetVariableValue(
SpecialVariables.PSApplicationOutputEncodingVarPath) as Encoding;

return applicationOutputEncoding ?? Console.OutputEncoding;
}
#nullable disable

/// <summary>
/// Determine if we have a special file which will change the way native argument passing
/// is done on Windows. We use legacy behavior for cmd.exe, .bat, .cmd files.
Expand Down
5 changes: 5 additions & 0 deletions src/System.Management.Automation/engine/SpecialVariables.cs
Expand Up @@ -41,6 +41,10 @@ internal static class SpecialVariables

internal static readonly VariablePath OutputEncodingVarPath = new VariablePath(OutputEncoding);

internal const string PSApplicationOutputEncoding = nameof(PSApplicationOutputEncoding);

internal static readonly VariablePath PSApplicationOutputEncodingVarPath = new VariablePath(PSApplicationOutputEncoding);

internal const string VerboseHelpErrors = "VerboseHelpErrors";

internal static readonly VariablePath VerboseHelpErrorsVarPath = new VariablePath(VerboseHelpErrors);
Expand Down Expand Up @@ -386,6 +390,7 @@ internal static class SpecialVariables
SpecialVariables.NestedPromptLevel,
SpecialVariables.pwd,
SpecialVariables.Matches,
SpecialVariables.PSApplicationOutputEncoding,
},
StringComparer.OrdinalIgnoreCase
);
Expand Down
3 changes: 3 additions & 0 deletions src/System.Management.Automation/resources/RunspaceInit.resx
Expand Up @@ -153,6 +153,9 @@
<data name="OutputEncodingDescription" xml:space="preserve">
<value>The text encoding used when piping text to a native executable file</value>
</data>
<data name="PSApplicationOutputEncodingDescription" xml:space="preserve">
<value>The text encoding used when reading output text from a native executable file</value>
</data>
<data name="PSStyleDescription" xml:space="preserve">
<value>Configuration controlling how text is rendered.</value>
</data>
Expand Down
62 changes: 62 additions & 0 deletions test/powershell/engine/Basic/NativeCommandEncoding.Tests.ps1
@@ -0,0 +1,62 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Native command output encoding tests' -Tags 'CI' {
BeforeAll {
$WriteConsoleOutPath = Join-Path $PSScriptRoot assets WriteConsoleOut.ps1
$defaultEncoding = [Console]::OutputEncoding.WebName
}

BeforeEach {
Clear-Variable -Name PSApplicationOutputEncoding
}

AfterEach {
Clear-Variable -Name PSApplicationOutputEncoding
}

It 'Defaults to [Console]::OutputEncoding if not set' {
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding $defaultEncoding
$actual | Should -Be café
}

It 'Defaults to [Console]::OutputEncoding if set to $null' {
$PSApplicationOutputEncoding = $null
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding $defaultEncoding
$actual | Should -Be café
}

It 'Uses scoped $PSApplicationOutputEncoding value' {
$PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
$actual = & {
$PSApplicationOutputEncoding = [System.Text.UTF8Encoding]::new()
pwsh -File $WriteConsoleOutPath -Value café -Encoding utf-8
}

$actual | Should -Be café

# Will use UTF-16-LE hence the different values
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding Unicode
$actual | Should -Be café
}

It 'Uses variable in class method' {
class NativeEncodingTestClass {
static [string] RunTest([string]$Script) {
$PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
return pwsh -File $Script -Value café -Encoding unicode
}
}

$actual = [NativeEncodingTestClass]::RunTest($WriteConsoleOutPath)
$actual | Should -Be café
}

It 'Fails to set variable with invalid encoding object' {
$ps = [PowerShell]::Create()
$ps.AddScript('$PSApplicationOutputEncoding = "utf-8"').Invoke()

$ps.Streams.Error.Count | Should -Be 1
[string]$ps.Streams.Error[0] | Should -Be 'Cannot convert the "utf-8" value of type "System.String" to type "System.Text.Encoding".'
}
}
23 changes: 23 additions & 0 deletions test/powershell/engine/Basic/assets/WriteConsoleOut.ps1
@@ -0,0 +1,23 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Value,

[Parameter(Mandatory)]
[string]
$Encoding
)

$enc = [System.Text.Encoding]::GetEncoding($Encoding)
$data = $enc.GetBytes($Value)

$outStream = [System.Console]::OpenStandardOutput()
try {
$outStream.Write($data, 0, $data.Length)
} finally {
$outStream.Dispose()
}