You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
$Null, $VariableThatDoesntExist result in pipeline execution whereas @() doesn't #7049
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics
Name Value
---------
PSVersion 6.0.2
PSEdition Core
GitCommitId v6.0.2
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
To follow the principle of least astonishment I would expect all of these to do nothing.
Consider $null, $null, $null | %{ 1 }. What would you expect to happen? How about $null, $null | % { 1 }? Now given the first two, what would the consistent behaviour for $null | %{ 1 } be?
Bruce Payette (@BrucePay) With $null, $null, $null | %{ 1 } and $null, $null | % { 1 } the behavior is driven by the fact that an array is sent through the pipeline, irrespective of what is in the array, so the fact that those examples produce a 1 for each element of the array makes sense.
If I was saying that @($null) | %{ 1 } shouldn't produce output then your examples would be directly comparable but in my examples no array with given size (regardless of what it contains) is being created.
To be fair though my biggest beef is with the $VarNotDefined | % {1} scenario though I suspect that under the hood somewhere the $null | % {1} scenario is related to $VarNotDefined | % {1} and it still also feels wrong.
Chris Magnuson (@ChrisMagnuson) When sending the result of an expression evaluation into the pipeline, there is effectively an implicit @( ) around the input such that 1 | % { 1 } is equivalent to @( 1 ) | % { 1 }.
To be fair though my biggest beef is with the $VarNotDefined | % {1} scenario
By default, referencing an undefined variable results in $null. You can change this behaviour using the Set-StrctMode cmdlet.
Bruce Payette (@BrucePay) If I understand properly, what Set-StrictMode will do is generate an error where as the behavior that is desired is to do nothing when a variable that doesn't exist is piped into the pipeline.
Thanks for the explanation about the implicit @( ).
I still feel that the expected behavior listed initially is best for least astonishment.
Do you have examples of where it is desirable that $null | % {1} executes the pipeline?
Chris Magnuson (@ChrisMagnuson): Indeed, Set-StrictMode -Version 1 and higher cause a statement-terminating error when you reference an uninitialized variable, but don't allow you to change the enumeration behavior.
The only way to avoid enumeration of $null is to use the foreachloop instead of the pipeline (e.g. foreach($v in $null) { <# never entered #> }) - a discrepancy that is in itself problematic, let alone that substituting one for the other is not always an option.
One way out of this would be to let uninitialized variables default to the "null collection", [System.Management.Automation.Internal.AutomationNull]::Value rather than to $null, given that this null collection is enumerated in neither scenario.
That would still enumerate explicit$null values (in the pipeline), but that is less problematic, given that commands must go out of their way to output $null (not producing any output implicitly results in [System.Management.Automation.Internal.AutomationNull]::Value).
Actually, that's not quite correct. Michael Klement (@mklement0) -- the first reference you make, to having the foreach loop use a straight $null, isn't quite analogous to the pipeline examples.
Because the pipeline is incapable of determining the difference between an array and a single object (because by default all arrays are passed as individual objects) it has to assume that the $null it's receiving is intentional and part of a collection, so it has to operate on it just like anything else. To the pipeline, there's no difference between @($null) and $null, so it has to behave the same for both.
What this comes down to is consistency and, as a corollary, predictability:
Both <stmt> | ... and foreach ($var in <stmt>) { ... } are enumeration contexts.
In PowerShell, this implies:
If <stmt> evaluates to something that already is enumerable, enumerate it.
If it doesn't, treat it like an enumerable, and treat that something as the one and only member of the pretend-enumerable, except if that "something" is the PS-specific "null collection" value whose very purpose is to signal that there is nothing to enumerate (see below).
Not treating these two enumeration contexts the same is an inconsistency that invites confusion and is hard to remember.
Arguably, in the context of PowerShell:
$null is a something that happens to represent a "single nothing" and is therefore enumerable - it corresponds to null in C#.
By contrast, [System.Management.Automation.Internal.AutomationNull]::Value is a PS-specific representation of an enumerable that is nothing in itself and has no elements: It is the "null collection" whose purpose is to signal "I represent nothing - I am not an object myself (unless I'm forced to act as one (as a scalar), in which case I'll pretend to be $null), and enumerating me results in no iterations".
It it is the "value" that commands that produce no output implicitly "return".
Therefore, both the foreach loop and the pipeline:
should treat $null as an enumerable and result in a single iteration whose iteration variable is $null.
should result in no iterations for [System.Management.Automation.Internal.AutomationNull]::Value, given that its very purpose is to signal that there is nothing to enumerate.
Yet, these two contexts currently act differently with respect to uninitialized variables (which default to $null), as the following example demonstrates:
The only time the discrepancy does not surface is by direct use of a command that outputs $null:
PS>foreach ($varin& { $null }) { 'inside the foreach loop' }; & { $null } |ForEach-Object { 'inside inside the foreach loop' }
inside the foreach loop
inside inside the foreach loop
Note how the foreach loop now too enumerated the $null output by the script block.
This is the subject of #5674, though the OP there is looking for the opposite behavior: they want foreach not to enumerate in this case either.
Again: These inconsistencies could be resolved if:
both the foreach loop and the pipeline consistently enumerate $null
if uninitialized variables default to [System.Management.Automation.Internal.AutomationNull]::Value rather than $null.
Remember that $null -eq $noSuchVar would continue to work, because, as stated, [System.Management.Automation.Internal.AutomationNull]::Value acts like $null when forced into a scalar context (try $null -eq (& {}) - & {} being the simplest statement that produces a [System.Management.Automation.Internal.AutomationNull]::Value value).
Note that reversing the operands should arguably work differently, given that [System.Management.Automation.Internal.AutomationNull]::Value should be considered an enumerable (array-valued) LHS, but (& {}) -eq $null does currently return $True, i.e., treats [System.Management.Automation.Internal.AutomationNull]::Value as a scalar
#3866 (comment) discusses this and shows that the current behavior is inconsistent in that the behavior varies depending on the operator used.
While it would be warranted from a consistency perspective, making $noSuchVar -eq $null return @() - i.e., treating the [System.Management.Automation.Internal.AutomationNull]::Value LHS as array-valued - would be a massively breaking change, because existing code that relies on such conditionals to return $True (whether explicitly or not) would break.
As another (farther) aside: #6823 is another example of inconsistent use of $null vs. [System.Management.Automation.Internal.AutomationNull]::Value.
Reacted by Chris Magnuson
microsoft-github-policy-service commented on 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.
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 commented on 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 commented on Nov 23, 2023
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics
Activity
BrucePay commentedon Jun 13, 2018
Consider
$null, $null, $null | %{ 1 }. What would you expect to happen? How about$null, $null | % { 1 }? Now given the first two, what would the consistent behaviour for$null | %{ 1 }be?ChrisMagnuson commentedon Jun 13, 2018
Bruce Payette (@BrucePay) With
$null, $null, $null | %{ 1 }and$null, $null | % { 1 }the behavior is driven by the fact that an array is sent through the pipeline, irrespective of what is in the array, so the fact that those examples produce a1for each element of the array makes sense.If I was saying that
@($null) | %{ 1 }shouldn't produce output then your examples would be directly comparable but in my examples no array with given size (regardless of what it contains) is being created.To be fair though my biggest beef is with the
$VarNotDefined | % {1}scenario though I suspect that under the hood somewhere the$null | % {1}scenario is related to$VarNotDefined | % {1}and it still also feels wrong.BrucePay commentedon Jun 13, 2018
Chris Magnuson (@ChrisMagnuson) When sending the result of an expression evaluation into the pipeline, there is effectively an implicit
@( )around the input such that1 | % { 1 }is equivalent to@( 1 ) | % { 1 }.By default, referencing an undefined variable results in
$null. You can change this behaviour using theSet-StrctModecmdlet.ChrisMagnuson commentedon Jun 13, 2018
Bruce Payette (@BrucePay) If I understand properly, what
Set-StrictModewill do is generate an error where as the behavior that is desired is to do nothing when a variable that doesn't exist is piped into the pipeline.Thanks for the explanation about the implicit
@( ).I still feel that the expected behavior listed initially is best for least astonishment.
Do you have examples of where it is desirable that
$null | % {1}executes the pipeline?mklement0 commentedon Jun 13, 2018
Chris Magnuson (@ChrisMagnuson): Indeed,
Set-StrictMode -Version 1and higher cause a statement-terminating error when you reference an uninitialized variable, but don't allow you to change the enumeration behavior.The only way to avoid enumeration of
$nullis to use theforeachloop instead of the pipeline (e.g.foreach($v in $null) { <# never entered #> }) - a discrepancy that is in itself problematic, let alone that substituting one for the other is not always an option.One way out of this would be to let uninitialized variables default to the "null collection",
[System.Management.Automation.Internal.AutomationNull]::Valuerather than to$null, given that this null collection is enumerated in neither scenario.That would still enumerate explicit
$nullvalues (in the pipeline), but that is less problematic, given that commands must go out of their way to output$null(not producing any output implicitly results in[System.Management.Automation.Internal.AutomationNull]::Value).However, that would clearly be a breaking change.
vexx32 commentedon Jun 24, 2018
Actually, that's not quite correct. Michael Klement (@mklement0) -- the first reference you make, to having the foreach loop use a straight $null, isn't quite analogous to the pipeline examples.
Because the pipeline is incapable of determining the difference between an array and a single object (because by default all arrays are passed as individual objects) it has to assume that the
$nullit's receiving is intentional and part of a collection, so it has to operate on it just like anything else. To the pipeline, there's no difference between@($null)and$null, so it has to behave the same for both.mklement0 commentedon Jun 25, 2018
Rain Sallow (/u/ta11ow) (@vexx32):
What this comes down to is consistency and, as a corollary, predictability:
Both
<stmt> | ...andforeach ($var in <stmt>) { ... }are enumeration contexts.In PowerShell, this implies:
If
<stmt>evaluates to something that already is enumerable, enumerate it.If it doesn't, treat it like an enumerable, and treat that something as the one and only member of the pretend-enumerable, except if that "something" is the PS-specific "null collection" value whose very purpose is to signal that there is nothing to enumerate (see below).
Not treating these two enumeration contexts the same is an inconsistency that invites confusion and is hard to remember.
Arguably, in the context of PowerShell:
$nullis a something that happens to represent a "single nothing" and is therefore enumerable - it corresponds tonullin C#.By contrast,
[System.Management.Automation.Internal.AutomationNull]::Valueis a PS-specific representation of an enumerable that is nothing in itself and has no elements: It is the "null collection" whose purpose is to signal "I represent nothing - I am not an object myself (unless I'm forced to act as one (as a scalar), in which case I'll pretend to be$null), and enumerating me results in no iterations".It it is the "value" that commands that produce no output implicitly "return".
Therefore, both the
foreachloop and the pipeline:should treat
$nullas an enumerable and result in a single iteration whose iteration variable is$null.should result in no iterations for
[System.Management.Automation.Internal.AutomationNull]::Value, given that its very purpose is to signal that there is nothing to enumerate.Yet, these two contexts currently act differently with respect to uninitialized variables (which default to
$null), as the following example demonstrates:The
foreachloop didn't enumerate anything, but the pipeline did:It is the pipeline that acts as expected here: given that uninitialized variables default to
$null, it enumerates that$null.Unexpectedly, the
foreachloop does not enumerate the$null.The same discrepancy happens with an initialized variable that happens to contain
$null:The only time the discrepancy does not surface is by direct use of a command that outputs
$null:Note how the
foreachloop now too enumerated the$nulloutput by the script block.This is the subject of #5674, though the OP there is looking for the opposite behavior: they want
foreachnot to enumerate in this case either.Again: These inconsistencies could be resolved if:
both the
foreachloop and the pipeline consistently enumerate$nullif uninitialized variables default to
[System.Management.Automation.Internal.AutomationNull]::Valuerather than$null.Remember that
$null -eq $noSuchVarwould continue to work, because, as stated,[System.Management.Automation.Internal.AutomationNull]::Valueacts like$nullwhen forced into a scalar context (try$null -eq (& {})-& {}being the simplest statement that produces a[System.Management.Automation.Internal.AutomationNull]::Valuevalue).Note that reversing the operands should arguably work differently, given that
[System.Management.Automation.Internal.AutomationNull]::Valueshould be considered an enumerable (array-valued) LHS, but(& {}) -eq $nulldoes currently return$True, i.e., treats[System.Management.Automation.Internal.AutomationNull]::Valueas a scalar#3866 (comment) discusses this and shows that the current behavior is inconsistent in that the behavior varies depending on the operator used.
While it would be warranted from a consistency perspective, making
$noSuchVar -eq $nullreturn@()- i.e., treating the[System.Management.Automation.Internal.AutomationNull]::ValueLHS as array-valued - would be a massively breaking change, because existing code that relies on such conditionals to return$True(whether explicitly or not) would break.As another (farther) aside: #6823 is another example of inconsistent use of
$nullvs.[System.Management.Automation.Internal.AutomationNull]::Value.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.