Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Description
Steps to reproduce
function Get-A {
param($a)
$a | % {Write-Host "Bug!"}
}
$emptyPipeline = (@() | % {})
$emptyPipeline | % {Write-Host "Proof of 100% empty pipeline "}
Get-A $emptyPipeline
#Get-A @($emptyPipeline) #this trick works, but very frustratingExpected behavior
$a parameter contains AutomationNull and nothing is printed
Actual behavior
Bug!
$a parameter contains $null
Environment data
Name Value
---- -----
PSVersion 6.1.3
PSEdition Core
GitCommitId 6.1.3
OS Linux 4.4.0-17763-Microsoft #253-Microsoft Mon Dec 31 17:49:00 PST 2018
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
mklement0 commentedon Mar 17, 2019
Indeed, passing a "null collection" -
[System.Management.Automation.Internal.AutomationNull]::Value- as an argument (parameter value) currently invariably converts it to$null.I can't speak to why this behavior was chosen, but hopefully others can. /cc Jason Shirk (@lzybkr)
Note that you can more simply create this "null collection" value with
. {}As for the enumeration behavior:
$nullis passed through the pipeline, as you've observed.foreachloop:While you can use this distinction to prevent enumeration (assuming your function doesn't truly need to distinguish between
[System.Management.Automation.Internal.AutomationNull]::Valueand$null, which is currently impossible via a parameter), I personally find it problematic - see #7049 (comment)Of course, you could also use
$null -eq $ato test$afor$nulland skip further processing based on that test.dmitriyse commentedon Mar 17, 2019
This is 100% abstraction leak. When you just want to write a script, you should always think about $null, [System.Management.Automation.Internal.AutomationNull]::Value and @(). Also about single element enumeration where you cannot get Count = 1.
This is horrible error-prone part of PowerShell.
Hope to see some cleanup here in PowerShell 7.0+
dmitriyse commentedon Mar 17, 2019
The problems of this kind make PowerShell less attractive that it could be and kills many other benefits.
dmitriyse commentedon Mar 17, 2019
As a current workaround, you need always put
@(<something>)to force<something>behave as a collection.mklement0 commentedon Mar 18, 2019
I hear you, Dmitriy Ivanov (@dmitriyse).
If backward compatibility weren't a concern, my vote would be for the following:
Default uninitialized variables to
[System.Management.Automation.Internal.AutomationNull]::Valuerather than$null.Preserve
[System.Management.Automation.Internal.AutomationNull]::Valuewhen passed as an argument.Make
foreachloops enumerate$null(and ignore[System.Management.Automation.Internal.AutomationNull]::Value), just as the pipeline does.Continue to treat
[System.Management.Automation.Internal.AutomationNull]::Valuelike$nullin conditionals and .NET method calls, but provide a way to distinguish between the two, if needed (see below).The above would make for consistent behavior, and there may then rarely be a need to explicitly distinguish between
$nulland[System.Management.Automation.Internal.AutomationNull]::Valuein conditionals, but it's still worth doing the following:Introduce a new automatic variable named, say,
$nullArrayto more succinctly represent[System.Management.Automation.Internal.AutomationNull]::Value.Make
-eqwith a[System.Management.Automation.Internal.AutomationNull]::ValueLHS only be equal to itself, to more easily allow distinguishing it from$null(whereas it's fine to continue to implicitly convert the former to$null, notably in .NET method calls).[System.Management.Automation.Internal.AutomationNull]::Value:@($potentialAutomationNull).Count -eq 0I encourage you to read the discussion at #6745, which calls for a future PowerShell version not encumbered by the need to maintain backward compatibility.
As it turns out, the issue at hand is already mentioned there, at #6745 (comment)
lzybkr commentedon Mar 20, 2019
I'm not sure this was intentional, more like an oversight.
I believe the following scenarios differed in V2 and I changed that in V3.
If my memory is correct, then I just missed the scenario of passing the value as an argument.
mklement0 commentedon Mar 20, 2019
Thanks for the insight, Jason Shirk (@lzybkr).
Indeed, in v2 assigning
[System.Management.Automation.Internal.AutomationNull]::Valueto a variable (such as returned from a pipeline with no output) implicitly converted it to$null, so sending such a variable through the pipeline later then unexpectedly resulted in$nullgetting sent as the input object to the next command in the pipeline (rather than not sending anything).Fixing the argument case too is a breaking change; however, if we leave the aspect of defaulting uninitialized variables to AutomationNull and fixing the
foreachloop behavior aside, could it be considered a bucket 3 change?In any event, these fixes are worth considering for a future version that is not beholden to backward compatibility.
(I've cleaned up the proposal in my previous comment.)
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.