Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/System.Management.Automation/engine/MshCommandRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3698,7 +3698,9 @@ internal void RemoveVariableListsInPipe()
if (this.PipelineVariable != null)
{
this.OutputPipe.RemovePipelineVariable();
_state.PSVariable.Remove(this.PipelineVariable);
// '_state' could be null when a 'DynamicParam' block runs because the 'DynamicParam' block runs in 'DoPrepare',
// before 'PipelineProcessor.SetupParameterVariables' is called, where '_state' is initialized.
_state?.PSVariable.Remove(this.PipelineVariable);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/powershell/engine/ParameterBinding/ParameterBinding.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ Describe "Parameter Binding Tests" -Tags "CI" {
}
}

It "PipelineVariable shouldn't cause a NullRef exception when 'DynamicParam' block is present" {
function DynamicParamTest {
[CmdletBinding()]
param()
dynamicparam { }
process { 'hi' }
}

DynamicParamTest -PipelineVariable bar | ForEach-Object { $bar } | Should -Be "hi"
}

Context "Use automatic variables as default value for parameters" {
BeforeAll {
## Explicit use of 'CmdletBinding' make it a script cmdlet
Expand Down