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

Conversation

jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Feb 14, 2024

PR Summary

Adds the $PSApplicationOutputEncoding variable that can be used to control the encoding PowerShell uses when reading the raw bytes to a string of an external application. This provides a scoped option to control the encoding used without resorting to the process wide setting of [Console]::OutputEncoding.

If the variable is unset (the default) or $null, the [Console]::OutputEncoding value is used to preserve the existing behaviour.

I ended up with $PSApplicationOutputEncoding to reflect that this is for the "Application" commands in PowerShell (Get-Command ... -CommandType Application) and $OutputEncoding is already taken as the confusingly named option for controlling how PowerShell encodes input to an application.

While not covered in this PR this could potentially be expanded in the future with:

  • Argument converters to support setting from a string/integer $PSApplicationOutputEncoding = 'utf-8'
  • A custom "raw/byte" encoding to have PowerShell output the byte[] without any encoding

PR Context

#16868 for this specific ask but there are many related issues to this.

Currently PowerShell uses the value of [Console]::OutputEncoding to control what encoding is used when reading the output from a native application. This can be problematic as:

  • It's set process wide, multithreading code can impact each other
  • You need an annoying try/finally to temporarily set it to another value
  • It also changes how the current process itself might write the output if the end user forgets to reset back to the default

A very common example is using wsl.exe which is hardcoded to output as UTF-16-LE/Unicode. To do this in PowerShell you would need to do:

$origEncoding = [Console]::OutputEncoding
try {
    [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
    $res = wsl.exe status
}
finally {
    [Console]::OutputEncoding = $origEncoding
}

With this PR you can now do

$res = & {
    $PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
    wsl.exe status
}

Not only is this less lines, it is thread safe so you can run this in parallel in multiple runspaces at the same time and it also won't change how PowerShell might output it's strings to the console. The scriptblock can even be omitted if already running in a child scope that doesn't need to go back to the default.

Other known examples of external applications that don't follow the value of [Console]::OutputEncoding and typically need the user to set it when calling

  • winget.exe - always uses UTF-8
  • python.exe - uses the Windows locale encoding (WinPS called this ANSI), or can be UTF-8 if an env var or argument is set to force it

PR Checklist

Adds the $PSApplicationOutputEncoding variable that can be used to
control the encoding PowerShell uses when reading the raw bytes to a
string of an external application. This provides a scoped option to
control the encoding used without resorting to the process wide setting
of [Console]::OutputEncoding.

This PR has 74 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +72 -2
Percentile : 29.6%

Total files changed: 6

Change summary by file extension:
.cs : +14 -2
.resx : +3 -0
.ps1 : +55 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

This PR has 78 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Small
Size       : +76 -2
Percentile : 31.2%

Total files changed: 6

Change summary by file extension:
.cs : +18 -2
.resx : +3 -0
.ps1 : +55 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review - Needed The PR is being reviewed Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant