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

Prevent Select-Object with ExpandProperty from updating source PSObjects #21328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BinaryWizard904
Copy link

When using Select-Object to select from a PSObject with both -Property and -ExpandedProperty arguments specified, the source object will be copied instead of referenced.

This PR addresses issue #21308. It ensures that when a PSObject is selected from with both the -ExpandProperty and -Property arguments specified, the source object will not be updated. While technically preventing the source object from being modified is a breaking change, it breaks things in a manner previously approved by the @Powershell/powershell-committee. This PR is needed because the previous attempt at fixing this only works for non-PSObject inputs.

In particular, for operations matching the aforementioned restrictions, this PR does the following: Within the ProcessExpandParameter method, the expandedObject is now set to a copy of the resulting PSObject instead of a reference.

Additionally, Pester tests have been added.

PR Checklist

Always copy the source object instead of referencing it.

This ensures that the source object is never implicitly
updated by the Select-Object command.

PowerShell#21308

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


Quantification details

Label      : Extra Small
Size       : +38 -3
Percentile : 16.4%

Total files changed: 2

Change summary by file extension:
.cs : +2 -2
.ps1 : +36 -1

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.

@mklement0
Copy link
Contributor

Thank you for tackling this, @BinaryWizard904.

Let me summarize the net effects of this PR:

  • It will reliably prevent modification of the object contained in the input object property targeted by -ExpandProperty in combination with -Property arguments, by attaching the -Property values as instance ETS properties directly to a (new) PSObject wrapper around the object (instead of attaching them via the resurrection tables, which is how instance ETS members are normally - and more robustly - attached).

    • For custom objects (PSCustomObject, PowerShell's "property bags", such as created with [pscustomobject] @{ ... }), this effectively creates a (shallow) clone of the input object before the ETS == direct properties are attached.

    • For instances of all other .NET types, it creates a PSObject wrapper with directly attached ETS properties, which are therefore easy to "lose", because - unlike with the resurrection tables - the loss of the wrapper means loss of the ETS properties (e.g., loss of the wrapper / ETS properties occurs in a simple cast of the invisible wrapper to the underlying type or when passing it to a command parameter typed as the underlying type).

      • However, this is the price to pay if modification (in an ETS sense) of the original object is to be avoided.
  • It will not solve Select-Object -ExpandProperty hides ETS instance members #7937, which is an edge case, however:

    • If the input object's -ExpandProperty-targeted property value is a (non-custom) object that has preexisting ETS instance members that are resurrection table-based (as is typical), these instance members are effectively eclipsed by the output PSObject wrapper and its directly attached -Property-targeted ETS instance properties.
    • However, you can technically still access them via .psobject.BaseObject on the wrapper.
    • This limitation seems to come down to there currently being no support for a given PSObject instance to directly surface both directly attached ETS members and ETS members present in the resurrection tables for its base object as direct members.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Mar 18, 2024
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
Extra Small Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants