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/CommandMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,9 @@ internal string GetCleanBlock()
// 1. the 'Clean' block doesn't propagate up any exception (terminating error);
// 2. only one expression in the script, so nothing else needs to be stopped when invoking the method fails.
return @"
$steppablePipeline.Clean()
if ($null -ne $steppablePipeline) {
$steppablePipeline.Clean()
}
";
}

Expand Down
13 changes: 13 additions & 0 deletions test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,19 @@ Describe 'Function Pipeline Behaviour' -Tag 'CI' {
## Dispose the steppable pipeline.
$step.Dispose()
}

It "Clean block runs fine in a proxy function when a dynamic parameter fails to bind" {
$function:TestProxyGci = [scriptblock]::Create(
[Management.Automation.ProxyCommand]::Create(
(Get-Command Get-ChildItem)))

## The proxy function 'TestProxyGci' contains the 'dynamicparam' block, which will
## run during parameter binding. However, the parameter binding failed, and thus
## the 'begin', 'process', and 'end' blocks will not run, so '$steppablePipeline'
## in the proxy function is null (never created). The 'clean' block will run anyway,
## but it should skip calling '$steppablePipeline.Clean()' in this case.
{ TestProxyGci -Attributes } | Should -Throw -ErrorId 'MissingArgument,TestProxyGci'
}
}

Context "'exit' statement in command" {
Expand Down