PowerShell
PowerShell copied to clipboard
Prevent Select-Object with ExpandProperty from updating source PSObjects
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
- [X] PR has a meaningful title
- Use the present tense and imperative mood when describing your changes
- [X] Summarized changes
- [X] Make sure all
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header - [X] This PR is ready to merge and is not Work in Progress.
- If the PR is work in progress, please add the prefix
WIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.
- If the PR is work in progress, please add the prefix
- Breaking changes
- [X] Yes - In a manner previously approved by the @Powershell/powershell-committee
- User-facing changes
- [X] Not Applicable
- Testing - New and feature
- Tooling
- [X] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
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
Excludedsection from yourprquantifier.yamlcontext profile. - Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your
prquantifier.yamlcontext profile. - Only use the labels that matter to you, see context specification to customize your
prquantifier.yamlcontext 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).
- For PRs that fall outside of the desired spectrum, review the details and check if:
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? :thumbsup: :ok_hand: :thumbsdown: (Email) Customize PullRequestQuantifier for this repository.
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
-ExpandPropertyin combination with-Propertyarguments, by attaching the-Propertyvalues as instance ETS properties directly to a (new)PSObjectwrapper 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
PSObjectwrapper 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 #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 outputPSObjectwrapper and its directly attached-Property-targeted ETS instance properties. - However, you can technically still access them via
.psobject.BaseObjecton the wrapper. - This limitation seems to come down to there currently being no support for a given
PSObjectinstance to directly surface both directly attached ETS members and ETS members present in the resurrection tables for its base object as direct members.
- If the input object's
@PowerShell/wg-powershell-cmdlets discussed this. We have concerns about any change in behavior now would likely break existing scripts that may not be aware are depending on this behavior. So, we must reject this change in behavior, but will open a doc issue to explicitly state that the input object is modified and an example of how to achieve the desired result by creating a new object instead of using Select-Object. In addition, given the history of Select-Object, we would consider a new set of cmdlet(s) that exhibit the desired behavior such as Expand-Object (published to PSGallery).
@SteveL-MSFT, please note that you had previously approved this breaking change; quoting https://github.com/PowerShell/PowerShell/issues/7768#issuecomment-459158766 (which the initial post also links to):
@PowerShell/powershell-committee reviewed this. We believe the original intent is to not modify the original object and this looks like a bug. This is a Breaking Change, but the benefit outweighs the potential impact. We agree that we should just fix the original behavior so that a new object is emitted.