Skip to content

No any way to pass empty pipeline (AutomationNull) to function parameter #9150

Closed

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 frustrating

Expected 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

Activity

  1. added
    Issue-Question
    on Mar 16, 2019
  2. mklement0 commented on Mar 17, 2019

    @mklement0
    Contributor

    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:

    • $null is passed through the pipeline, as you've observed.
    PS> $null | ForEach-Object { 'hi' }
    hi
    • By contrast, it isn't if you use a foreach loop:
    PS> foreach ($val in $null) { 'hi' }
     # (no output)

    While you can use this distinction to prevent enumeration (assuming your function doesn't truly need to distinguish between [System.Management.Automation.Internal.AutomationNull]::Value and $null, which is currently impossible via a parameter), I personally find it problematic - see #7049 (comment)

    Of course, you could also use $null -eq $a to test $a for $null and skip further processing based on that test.

  3. dmitriyse commented on Mar 17, 2019

    @dmitriyse
    Author

    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+

  4. dmitriyse commented on Mar 17, 2019

    @dmitriyse
    Author

    The problems of this kind make PowerShell less attractive that it could be and kills many other benefits.

  5. dmitriyse commented on Mar 17, 2019

    @dmitriyse
    Author

    As a current workaround, you need always put @(<something>) to force <something> behave as a collection.

  6. mklement0 commented on Mar 18, 2019

    @mklement0
    Contributor

    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]::Value rather than $null.

    • Preserve [System.Management.Automation.Internal.AutomationNull]::Value when passed as an argument.

    • Make foreach loops enumerate $null (and ignore [System.Management.Automation.Internal.AutomationNull]::Value), just as the pipeline does.

    • Continue to treat [System.Management.Automation.Internal.AutomationNull]::Value like $null in 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 $null and [System.Management.Automation.Internal.AutomationNull]::Value in conditionals, but it's still worth doing the following:

    • Introduce a new automatic variable named, say, $nullArray to more succinctly represent [System.Management.Automation.Internal.AutomationNull]::Value.

    • Make -eq with a [System.Management.Automation.Internal.AutomationNull]::Value LHS 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).

      • Currently, you have to use the following, somewhat obscure, technique to detect [System.Management.Automation.Internal.AutomationNull]::Value: @($potentialAutomationNull).Count -eq 0

    I 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)

  7. lzybkr commented on Mar 20, 2019

    @lzybkr
    Contributor

    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.

    # direct piping
    Get-EmptyPipeline | Do-Something
    
    # piping after assignment
    $something = Get-EmptyPipeline
    $something | Do-Something 

    If my memory is correct, then I just missed the scenario of passing the value as an argument.

  8. mklement0 commented on Mar 20, 2019

    @mklement0
    Contributor

    Thanks for the insight, Jason Shirk (@lzybkr).

    Indeed, in v2 assigning [System.Management.Automation.Internal.AutomationNull]::Value to 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 $null getting 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 foreach loop 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.)

  9. added
    WG-Engine
    Issue-Enhancement
    and removed
    Issue-Question
    on Jan 30, 2020
  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  13. added
    Resolution-No Activity
    on Nov 16, 2023
  14. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions