Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix indexer for System.Management.Automation.PathInfoStack type #4642

@mklement0

Description

@mklement0

Get-Location -Stack currently outputs a [System.Management.Automation.PathInfoStack] stack object rather than an array of [System.Management.Automation.PathInfo] instances representing the individual locations in the stack.

While this is behavior is documented, and changing it would technically be a breaking change, it should be considered for two reasons:

  • Outputting a custom stack object rather than an array of locations is of little benefit, given that location stacks are meant to be managed via the *-Location cmdlets.

    • Stack management support in these cmdlets currently lacks options for deleting / clearing a stack, but that is a separate issue - see Managing location stacks via the *-Location cmdlets lacks ability to clear stacks #4643.

    • As an aside: While calling the .Clear() method on the stack object may seem like a workaround for the current inability to clear a stack using cmdlets, it doesn't actually work (only clears the object returned, not the actual stack).

  • Outputting a custom stack object rather than an array of locations contravenes the expectation that a regular PowerShell array is output that can be indexed into, which doesn't work as intended with the stack object (.ToArray()[0] must be used, for instance).

    • The current default output certainly looks like a collection (array) of [System.Management.Automation.PathInfo] instances.

Current behavior

# Try to obtain the 2nd location from the bottom of the stack, which is "/"
> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]
# !! No output

Because Get-Location -Stack returns a scalar - the stack object - indexing into the scalar with [-2] makes PowerShell return $null.

To make this work currently, .ToArray() must be used:

# Note the awkward .ToArray()
> Push-Location /; Push-Location $HOME; (Get-Location -Stack).ToArray()[-2]

Path
----
/   

Desired behavior

If a regular array is returned, indexing will work as expected.

> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]

Path
----
/   

Environment data

PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersIssue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions