Skip to content

ScriptBlock.InvokeWithContext mutates variable lists passed as arguments #12928

Closed

Description

If passed a variable that is (for lack of established term afaik) intrinsic to the scope, the variable entry will be removed from the list passed in variablesToDefine. This includes vars such as $this, $input, $_ and $PSItem.

Steps to reproduce

$list = [System.Collections.Generic.List[psvariable]]@(
    [psvariable]::new('_', $null),
    [psvariable]::new('something-not-scope-intrinsic', $null))

{}.InvokeWithContext($null, $list, @())
$list

Expected behavior

Name                           Value
----                           -----
_
something-not-scope-intrinsic

Actual behavior

Name                           Value
----                           -----
something-not-scope-intrinsic

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.2
PSEdition                      Core
GitCommitId                    7.1.0-preview.2
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. bpayette commented on Jun 10, 2020

    @bpayette
    Contributor

    Patrick Meinecke (@SeeminglyScience) Correct and by design. Intrinsic variables already have a set of rules for when and how they are bound. This API was not intended to change those rules. What it does is allow you to provide ambient context to the script block when it executes. This is used by DSC internally to define the set of resources (functions) available inside a configuration (function). For example, if you want to make a function available to a scriptblock without setting it in the current scope and thus making it visible to everything you do, you can use this API. It's particularly useful in modules where you can't use dynamic scoping to provision an execution environment. Does that make sense?

  2. bpayette commented on Jun 10, 2020

    @bpayette
    Contributor

    Patrick Meinecke (@SeeminglyScience) Correct and by design. Intrinsic variables already have a set of rules for when and how they are bound. This API was not intended to change those rules. What it does is allow you to provide ambient context to the script block when it executes. This is used by DSC internally to define the set of resources (functions) available inside a configuration (function). For example, if you want to make a function available to a scriptblock without setting it in the current scope and thus making it visible to everything you do, you can use this API. It's particularly useful in modules where you can't use dynamic scoping to provision an execution environment. Does that make sense?

  3. SeeminglyScience commented on Jun 10, 2020

    @SeeminglyScience
    ContributorAuthor

    Bruce Payette (@bpayette) It works, you can set intrinsic variables with this API. The API iterates the List<PSVariable> looking for intrinsic variables and removes them to be handled separately (through InvokeWithPipe parameters).

    This issue is just about the List<> argument getting mutated because of internal logic. I would have expected a copy of the list to have been made if changes were needed before it passed the list to InvokeWithPipe.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions