Skip to content

Push locals of automatic variables to 'DottedScopes' when dotting script cmdlets - #4709

Merged
Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
daxian-dbw:dotsource
Sep 6, 2017
Merged

Dongbo Wang (daxian-dbw) merged 1 commit into
PowerShell:masterfrom
daxian-dbw:dotsource

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

Fix #4688

When dotting a script cmdlet, the locals of automatic variables from the PSScriptCmdlet is not set up in the current scope before parameter binding. The fix is to push the locals in CommandProcessor.OnSetCurrentScope and pop them in CommandProcessor.OnRestorePreviousScope, which will be called from SetCurrentScopeToExecutionScope and RestorePreviousScope respectively.

Summary of changes:

  1. When a new local scope is used, currently we set the locals for CommandProcessor right before parameter binding (in BindCommandLineParametersNoValidation); we set the locals for ScriptCommandProcessor in Prepare. I moved both to the constructor, right after the new scope is created so that the code is more consistent.

  2. In CmdletParameterBinderController.cs, we set up the PSBoundParameters and MyInvocation variables in HandleCommandLineDynamicParameters again, which I think is unnecessary because this method is only called from BindCommandLineParametersNoValidation, where the setup is done for the first time.

  3. Currently, the locals will be set for dotted script cmdlet in EnterScope() and ExitScope(). Now, that logic is moved to OnSetCurrentScope and OnRestorePreviousScope of CommandProcessor. This not only makes sure that locals are set before parameter binding, but also is consistent with the ScriptCommandProcessor.

@daxian-dbw

Copy link
Copy Markdown
Member Author

sergei (@vors) Thanks!
Jason Shirk (@lzybkr) can you please take a look when you have time?

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
…ipt cmdlets (PowerShell#4709)

When dotting a script cmdlet, the locals of automatic variables from the `PSScriptCmdlet` is not set up in the current scope before parameter binding. The fix is to push the locals in `CommandProcessor.OnSetCurrentScope` and pop them in `CommandProcessor.OnRestorePreviousScope`, which will be called from `SetCurrentScopeToExecutionScope` and `RestorePreviousScope` respectively.

Summary of changes:
1. When a new local scope is used, currently we set the locals for `CommandProcessor` right before parameter binding (in `BindCommandLineParametersNoValidation`); we set the locals for `ScriptCommandProcessor` in `Prepare`. I moved both to the constructor, right after the new scope is created so that the code is more consistent.

2. In `CmdletParameterBinderController.cs`, we set up the `PSBoundParameters` and `MyInvocation` variables in `HandleCommandLineDynamicParameters` again, which I think is unnecessary because this method is only called from `BindCommandLineParametersNoValidation`, where the setup is done for the first time.

3. Currently, the locals will be set for dotted script cmdlet in `EnterScope()` and `ExitScope()`. Now, that logic is moved to `OnSetCurrentScope` and `OnRestorePreviousScope` of `CommandProcessor`. This not only makes sure that locals are set before parameter binding, but also is consistent with the `ScriptCommandProcessor`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$PSScriptRoot is not populated when used in a default value for a non-mandatory script parameter

4 participants