From 9a388525f28669f9025b1665611d620a07783ab5 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 1 Feb 2022 15:39:32 -0800 Subject: [PATCH 1/3] Fix the clean block for generated proxy function --- src/System.Management.Automation/engine/CommandMetadata.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/CommandMetadata.cs b/src/System.Management.Automation/engine/CommandMetadata.cs index bbe2b6666b2..a9094f54b36 100644 --- a/src/System.Management.Automation/engine/CommandMetadata.cs +++ b/src/System.Management.Automation/engine/CommandMetadata.cs @@ -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() + } "; } From 6023ce867cc67a82caf679232db6eb6ff94f67ab Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 1 Feb 2022 16:01:07 -0800 Subject: [PATCH 2/3] Add test --- .../Language/Scripting/PipelineBehaviour.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 b/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 index 9551343da85..99d372f3989 100644 --- a/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 +++ b/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 @@ -478,6 +478,13 @@ 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))) + { ProxyGci -Attributes } | Should -Throw -ErrorId 'MissingArgument,ProxyGci' + } } Context "'exit' statement in command" { From a3c6205412442c15304751e33f76b42c8c06e7bc Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 1 Feb 2022 16:10:57 -0800 Subject: [PATCH 3/3] Add comments --- .../Language/Scripting/PipelineBehaviour.Tests.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 b/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 index 99d372f3989..0478492c910 100644 --- a/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 +++ b/test/powershell/Language/Scripting/PipelineBehaviour.Tests.ps1 @@ -483,7 +483,13 @@ Describe 'Function Pipeline Behaviour' -Tag 'CI' { $function:TestProxyGci = [scriptblock]::Create( [Management.Automation.ProxyCommand]::Create( (Get-Command Get-ChildItem))) - { ProxyGci -Attributes } | Should -Throw -ErrorId 'MissingArgument,ProxyGci' + + ## 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' } }