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

PSObject wrapped ScriptBlocks should also throw when attempting $using: access #17152

Closed
SeeminglyScience opened this issue Apr 14, 2022 · 4 comments · Fixed by #17234
Closed
Assignees
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-Fixed The issue is fixed. Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors

Comments

@SeeminglyScience
Copy link
Collaborator

SeeminglyScience commented Apr 14, 2022

The check in ForEach-Object -Parallel for ScriptBlocks in $using: values currently doesn't check PSObjects. This was discovered in #17150

This if:

if (item is ScriptBlock)
{
ThrowTerminatingError(
new ErrorRecord(
new PSArgumentException(InternalCommandStrings.ParallelUsingVariableCannotBeScriptBlock),
"ParallelUsingVariableCannotBeScriptBlock",
ErrorCategory.InvalidType,
this));
}
}

Might need to be:

if (item is ScriptBlock or PSObject { BaseObject: ScriptBlock })
{
    // etc
}

Steps to reproduce

$sb = { 'worked' }
0 | ForEach-Object -Parallel { & $using:sb }

$sb = [psobject]{ 'worked' }
0 | ForEach-Object -Parallel { & $using:sb }

Expected behavior

ForEach-Object:
Line |
   2 |  0 | ForEach-Object -Parallel { & $using:sb }
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | A ForEach-Object -Parallel using variable cannot be a script block. Passed-in script block variables are not supported with ForEach-Object -Parallel, and can result in undefined behavior.

ForEach-Object:
Line |
   5 |  0 | ForEach-Object -Parallel { & $using:sb }
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | A ForEach-Object -Parallel using variable cannot be a script block. Passed-in script block variables are not supported with ForEach-Object -Parallel, and can result in undefined behavior.

Actual behavior

ForEach-Object:
Line |
   2 |  0 | ForEach-Object -Parallel { & $using:sb }
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | A ForEach-Object -Parallel using variable cannot be a script block. Passed-in script block variables are not supported with ForEach-Object -Parallel, and can result in undefined behavior.
worked

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.0-preview.3
PSEdition                      Core
GitCommitId                    7.3.0-preview.3
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@SeeminglyScience SeeminglyScience added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 14, 2022
@SeeminglyScience SeeminglyScience added Issue-Bug Issue has been identified as a bug in the product Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Apr 18, 2022
@ryneandal
Copy link
Contributor

If this is up for grabs, I would love to take a crack at it, if that's alright.

@SeeminglyScience
Copy link
Collaborator Author

@ryneandal Absolutely! If you have any questions feel free to ask 🙂

@ryneandal
Copy link
Contributor

Awesome, thanks. Per the contribution guidelines, could I get an assignment from @powershell/powershell?

ryneandal added a commit to ryneandal/PowerShell that referenced this issue Apr 29, 2022
As noted in PowerShell#17152, ScriptBlocks wrapped in PSObjects don't throw an error when passed to ForEach-Object.

This handles that edge case.
ryneandal pushed a commit to ryneandal/PowerShell that referenced this issue Apr 29, 2022
As noted in PowerShell#17152, ScriptBlocks wrapped in PSObjects don't throw an error when passed to ForEach-Object.

This handles that edge case.
@ghost ghost added the In-PR Indicates that a PR is out for the issue label Apr 29, 2022
@ghost ghost added Resolution-Fixed The issue is fixed. and removed In-PR Indicates that a PR is out for the issue labels May 4, 2022
@ghost
Copy link

ghost commented May 23, 2022

🎉This issue was addressed in #17234, which has now been successfully released as v7.3.0-preview.4.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-Fixed The issue is fixed. Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors
Projects
None yet
2 participants