X Tutup
The Wayback Machine - https://web.archive.org/web/20220607124900/https://github.com/PowerShell/PowerShell/pull/17455
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

Make Out-String and Out-File keep string input unchanged #17455

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented May 28, 2022

PR Summary

Fix #17452

The changes in this PR update the behavior of Out-File and Out-String regarding the RenderingOutput setting to be the following:

  • when the input object is pure string, these 2 cmdlets keep the string input unchanged regardless of the RenderingOutput setting
  • when the input object needs to have formatting views applied to it, these 2 cmdlets remove escape sequences from the formatting output strings based on the RenderingOutput setting.

This change is a breaking change to these 2 cmdlets' existing behavior regarding RenderingOutput.

PR Checklist

@pull-request-quantifier
Copy link

@pull-request-quantifier pull-request-quantifier bot commented May 29, 2022

This PR has 66 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       : +60 -6
Percentile : 26.4%

Total files changed: 5

Change summary by file extension:
.cs : +6 -4
.ps1 : +54 -2

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 detetcted.
  • 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? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented May 31, 2022

We need to update docs https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.2

Describes the features of PowerShell that use ANSI escape sequences and the terminal hosts that support them.

Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

LGTM! Thank you for making it still possible to retain formatting when output rendering is Ansi ❤️

@daxian-dbw daxian-dbw added the Review - Committee label Jun 6, 2022
@daxian-dbw
Copy link
Member Author

@daxian-dbw daxian-dbw commented Jun 6, 2022

@PowerShell/powershell-committee Need the committee to review the breaking change comparing with 7.2.x.
Summary of the breaking change. The corresponding issue is #17452.

In 7.2.x, Out-File and Out-String remove escape sequences from the passed-in strings when $PSStyle.OutputRendering is Host or PlainText.

When the passed-in object has formatting views applied to it, removing escape sequences from the resulted formatting text makes perfect sense when the output rendering option is Host and PlainText.

However, when the passed-in objects are pure strings, those 2 cmdlets shouldn't make any changes to the string objects, because

  1. that will make the scenarios like (Get-Content file-a -Raw).Replace("`r`n", "`n") > file-b not working. Another example is that Set-Content and Out-File behave differently on string input under those output rendering options but they should really behave the same:

    ## Out-File changes the input strings when output rendering is `Host` or `PlainText`
    "`e[31mabc`e[0m" | Set-Content -Path .\abc.txt
    "`e[31mabc`e[0m" | Out-File -Path .\abc.txt
    
  2. that also cause potential security problems as we are altering the string input when doing redirection. This was the reason why we changed the default OutputRendering option from Host to Ansi in v7.2.1

  3. given that we have changed the default output rendering option back to Host in 7.3 preview, "e[31mabce[0m" > .\abc.txt won't work the same as in 7.0.x and 7.2.x by default. That is a regression to 2 LTS releases.

I think the $PSStyle.OutputRendering option should only affect formatting output when it comes to Out-File and Out-String. When the input objects are pure strings, they should not be altered by Out-File or Out-String regardless of the output rendering option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking-Change CL-Engine Review - Committee Small
Projects
None yet
5 participants
X Tutup