Skip to content

$null should be indicated by a blank line, especially when within an array #17683

Closed

Description

@maettu-this

Summary of the new feature / enhancement

In the attached script a variable accidentally got forgotten to be assigned to, resulting in a value of $null being sent down the pipeline.

ResultWithAccidentalNull.ps1.txt

Consequently, the result of the script accidentally is an array of [$null, 1], but:

PS C:\<PATH> .\ResultWithAccidentalNull.ps1
1                               <= seems to look good

But when assigning the result to a variable it gets queerish:

PS C:\<PATH> $result = .\ResultWithAccidentalNull.ps1
PS C:\<PATH> $result
1                               <= still seems good
PS C:\<PATH> $result.Length
2                               <= what ?!?
PS C:\<PATH> $result[0]         <= nothing ?!?
PS C:\<PATH> $result[1]
1                               <= only now it becomes apparent where the intended result went to

This behavior is rather confusing:
The $null value exists but is not indicated in any way.

This behavior is also inconsistent:
A result[1] of an array with 2 or more items is expected to be indicated on the second line.

Proposed technical implementation details (optional)

The real world script of course is much bigger. Can you imagine how long it took us to find the root cause of this issue? I think there would be an obvious fix to improve this situation.

Ideal result of this script:

PS C:\<PATH> $result = .\ResultWithAccidentalNull.ps1
PS C:\<PATH> $result
                                <= result[0] is $null
1                               <= result[1] is 1

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Jul 14, 2022
  2. SteveL-MSFT commented on Jul 18, 2022

    @SteveL-MSFT
    Member

    $null when written to the host needs to be converted to a string, so it becomes an empty string:

    ($null | out-string) -eq ""

    Replacing it with a newline would certainly be a breaking change and can cause confusion in the other direction.

    In your example, $result[0] returns $null which is an empty string so nothing is written out. You can check that $null -eq $result[0] is $true.

    Since $null is a valid value in an array, some folks are likely depending on this behavior.

  3. added
    Resolution-By Design
    and removed
    Needs-Triage
    on Jul 18, 2022
  4. JamesWTruher commented on Jul 18, 2022

    @JamesWTruher
    Collaborator

    $null when written to the host needs to be converted to a string, so it becomes an empty string:

    ($null | out-string) -eq ""

    it's also not necessary to ($null | out-string) -eq "" as "$null" -eq "" gives the same result

  5. maettu-this commented on Jul 19, 2022

    @maettu-this
    Author

    $null when written to the host needs to be converted to a string, so it becomes an empty string:

    ($null | out-string) -eq ""

    This is fine, no need to change anything on this behavior.

    However, I think it is inconsistent behavior when outputting an array containing $null. Why? If "$null" -eq "", outputting an array containing "$null" or "" should result in the same. But it doesn't. Array values of "" result in empty lines, whereas array values of "$null" result in nothing.
    This doesn't make sense. The fact that lines are broken, i.e. an array value per line is output, is the behavior of outputting an array. It should have nothing to do with the value of the respective array item. The value of an array item should only define the content of the line, but not the fact that a line is output.

    Of course I do understand the compatibility / breaking change related considerations. Still, I think such inconsistency in behavior should also be considered to get fixed in the long run, in order to prevent people falling into (unecessary) traps.

  6. alxfnd commented on Jul 19, 2022

    @alxfnd

    Why don't you try assigning the values into a hash table and reading from the table?

    $result = .\ResultWithAccidentalNull.ps1
    $table = @{
    A = $result[0]
    B = $result[1]
    }
    $table

    This should print out your null value and makes it easier to reference specific values later on.

  7. SeeminglyScience commented on Jul 19, 2022

    @SeeminglyScience
    Contributor

    $null when written to the host needs to be converted to a string, so it becomes an empty string:

    I believe they're saying that formatting shouldn't skip $null, but instead treat it the same way it does an empty string:

    image

    Not weighing in on if it should be done, just clarifying the request.

  8. vexx32 commented on Jul 19, 2022

    @vexx32
    Collaborator

    Yeah, it can be quite confusing to track down null values as it is, and this behaviour makes it even more confusing at times.

    I'd even be open to us writing out $null or NULL or something to make them easier to spot, but a blank entry instead of skipping the entry would be a good step forward nonetheless.

  9. ghost closed this as completedon Jul 20, 2022
  10. maettu-this commented on Jul 22, 2022

    @maettu-this
    Author

    This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.

    Hmm... msftbot... Closing after just a day of inactivity? What if there is inactivity over a weekend? What if the by-design tag was applied a bit too early and no real resolution is there yet? Besides PowerShell handling $null values within an array, MS should also consider refining the configuration of the msftbot...

  11. maettu-this commented on Jul 22, 2022

    @maettu-this
    Author

    Even more so that apparently I am not the only to question the handling of $null, see comments and #17701.

  12. added
    Needs-Triage
    and removed
    Resolution-By Design
    on Jul 22, 2022
  13. SteveL-MSFT commented on Aug 1, 2022

    @SteveL-MSFT
    Member

    If the request is purely changing how it is formatted, then formatting changes are not considered breaking changes (from a scripting point of view anyways). Since $true is represented as True, then it may make sense for $null to be represented as NULL.

  14. vexx32 commented on Aug 1, 2022

    @vexx32
    Collaborator

    Tangentially related but possibly also potentially warranting a specific display usecase would be [dbnull]::Value which is currently indistinguishable in console output from $null.

  15. JamesWTruher commented on Oct 30, 2023

    @JamesWTruher
    Collaborator

    this would be a fairly substantial breaking change if addressed

  16. theJasonHelmick commented on Nov 1, 2023

    @theJasonHelmick
    Collaborator

    Hi maettu-this - thank you for reporting this issue. The Interactive WG has reviewed this feature request. As mentioned previously in this issue, the suggested change would create substantial breaking changes. The working group recommends a work around using foreach to iterate the values if needed.

    1,$null,3 | %{ "'$_'" }
  17. added
    Resolution-By Design
    Resolution-Declined
    and removed
    Needs-Triage
    Resolution-By Design
    on Nov 1, 2023
  18. microsoft-github-policy-service commented on Nov 2, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as declined and has not had any activity for 1 day. 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-DeclinedWG-Interactive-Console

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions