Title: Fix indexer for System.Management.Automation.PathInfoStack type · Issue #4642 · PowerShell/PowerShell · GitHub
Open Graph Title: Fix indexer for System.Management.Automation.PathInfoStack type · Issue #4642 · PowerShell/PowerShell
X Title: Fix indexer for System.Management.Automation.PathInfoStack type · Issue #4642 · PowerShell/PowerShell
Description: 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...
Open Graph Description: Get-Location -Stack currently outputs a [System.Management.Automation.PathInfoStack] stack object rather than an array of [System.Management.Automation.PathInfo] instances representing the individu...
X Description: Get-Location -Stack currently outputs a [System.Management.Automation.PathInfoStack] stack object rather than an array of [System.Management.Automation.PathInfo] instances representing the individu...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4642
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fix indexer for System.Management.Automation.PathInfoStack type","articleBody":"`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.\r\n\r\nWhile this is behavior is documented, and changing it would technically be a breaking change, it should be considered for two reasons:\r\n\r\n* 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.\r\n\r\n * Stack management support in these cmdlets currently lacks options for deleting / clearing a stack, but that is a separate issue - see #4643.\r\n\r\n * 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).\r\n\r\n* 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).\r\n\r\n * The current default output certainly _looks_ like a _collection_ (array) of `[System.Management.Automation.PathInfo]` instances.\r\n\r\n\r\nCurrent behavior\r\n------------------\r\n\r\n```powershell\r\n# Try to obtain the 2nd location from the bottom of the stack, which is \"/\"\r\n> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]\r\n# !! No output\r\n```\r\n\r\nBecause `Get-Location -Stack` returns a _scalar_ - the stack object - indexing into the scalar with `[-2]` makes PowerShell return `$null`.\r\n\r\nTo make this work currently, `.ToArray()` must be used:\r\n\r\n```powershell\r\n# Note the awkward .ToArray()\r\n> Push-Location /; Push-Location $HOME; (Get-Location -Stack).ToArray()[-2]\r\n\r\nPath\r\n----\r\n/ \r\n\r\n```\r\n\r\n\r\nDesired behavior\r\n-----------------\r\n\r\nIf a regular array is returned, indexing will work as expected.\r\n\r\n```none\r\n> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]\r\n\r\nPath\r\n----\r\n/ \r\n\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta.5 on macOS 10.12.6\r\nPowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS\r\nPowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-08-22T17:13:22Z","url":"https://github.com/PowerShell/PowerShell/issues/4642"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7e040616-ad1e-b246-1d20-8e3c2880c054 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C766:128588:28B1E9:3B0066:6AFD7A6A |
| html-safe-nonce | e573aa0e91d3a843c2e8e525b0986ee408fb81f2be54652bfe6868e491f222fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzY2OjEyODU4ODoyOEIxRTk6M0IwMDY2OjZBRkQ3QTZBIiwidmlzaXRvcl9pZCI6IjgwNzYzOTAxMzc3ODA5NDY5OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8dcdc16abd95dcc316d377893317edfd879e72c8ed8c44ea28cce2a75e68e59c |
| hovercard-subject-tag | issue:252028318 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/4642/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7fb9c7c21c1ce9f51285eeec5c7bf137f7efe16a03429943cf1d6029b15975fc/PowerShell/PowerShell/issues/4642 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7fb9c7c21c1ce9f51285eeec5c7bf137f7efe16a03429943cf1d6029b15975fc/PowerShell/PowerShell/issues/4642 |
| og:image:alt | Get-Location -Stack currently outputs a [System.Management.Automation.PathInfoStack] stack object rather than an array of [System.Management.Automation.PathInfo] instances representing the individu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 544ff6b59bd215c4609120f6e334dbd0b00d34fb6f8bc03a476f72256998887e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 71c92f8b27ec5e9aa6f6477b4e067a4727fc9edb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width