Title: Property-based pipeline-parameter binding should be applied to argument-based parameter binding as well · Issue #6057 · PowerShell/PowerShell · GitHub
Open Graph Title: Property-based pipeline-parameter binding should be applied to argument-based parameter binding as well · Issue #6057 · PowerShell/PowerShell
X Title: Property-based pipeline-parameter binding should be applied to argument-based parameter binding as well · Issue #6057 · PowerShell/PowerShell
Description: Note: Fixing this would be a breaking change, but arguably one that falls into Bucket 3: Unlikely Grey Area. Currently, when you use a ValueFromPipelineByPropertyName parameter attribute, the property named there is only bound when using...
Open Graph Description: Note: Fixing this would be a breaking change, but arguably one that falls into Bucket 3: Unlikely Grey Area. Currently, when you use a ValueFromPipelineByPropertyName parameter attribute, the prope...
X Description: Note: Fixing this would be a breaking change, but arguably one that falls into Bucket 3: Unlikely Grey Area. Currently, when you use a ValueFromPipelineByPropertyName parameter attribute, the prope...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6057
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Property-based pipeline-parameter binding should be applied to argument-based parameter binding as well","articleBody":"Note: Fixing this would be a breaking change, but arguably one that falls into [Bucket 3: Unlikely Grey Area](https://github.com/PowerShell/PowerShell/blob/master/docs/dev-process/breaking-change-contract.md#bucket-3-unlikely-grey-area).\r\n\r\n**Currently, when you use a `ValueFromPipelineByPropertyName` parameter attribute, the property named there is only bound when using _pipeline_ input and not also when passing the same object as a direct _argument_.**\r\n\r\nTo illustrate the discrepancy with a simple example:\r\n\r\n```powershell\r\n# Declare a function with a parameter that binds pipeline input by the value of \r\n# property .LiteralPath or its alias .PSPath\r\nfunction foo { \r\n param(\r\n [Parameter(ValueFromPipelineByPropertyName)]\r\n [Alias('PSPath')]\r\n [string[]] ${LiteralPath}\r\n ) \r\n \"`$LiteralPath: [$LiteralPath]\" \r\n}\r\n\r\n# Create object with a .PSPath property.\r\n$obj = [pscustomobject] @{ PSPath = 'pspath' }\r\n\r\n# Pass the object to the function, first via the pipeline, then as a direct argument\r\n$obj | foo\r\nfoo -LiteralPath $obj\r\n```\r\n\r\nThe output shows that the `.PSPath` property is bound es expected via the pipeline, but _not_ as a direct argument:\r\n\r\n```none\r\n$LiteralPath: [pspath] # OK (pipeline): .PsPath value was bound.\r\n$LiteralPath: [@{PSPath=pspath}] # !! (argument): $obj was simply stringified (.ToString())\r\n```\r\n\r\nThe **binding of the direct _argument_ pays attention only to the parameter _type_**, causing it to simply _stringify_ `$obj` with `.ToString()` (not with default output formatting).\r\n\r\nThis discrepancy is not only surprising, it **can have grave consequences** when dealing with `[System.IO.FileInfo]` instances output by **`Get-ChildItem`**, because the latter **often stringify to a _mere filename_**, potentially causing unexpected and destructive behavior with `-LiteralPath` cmdlets such as `Remove-Item`, `Move-Item` and `Copy-Item`:\r\n\r\n```powershell\r\n# Create sample file 'tmpFile' in subfolder 'tmpDir'; i.e.: tmpDir/tmpFile\r\n$null > (Join-Path (New-Item -force -Type Directory tmpDir) tmpFile)\r\n\r\n# Obtain a [System.IO.FileInfo] instance representing the new file.\r\n# Note that `Get-ChildItem tmpDir` rather than `Get-Item tmpDir/tmpFile` (or\r\n# `Get-ChildItem tmpDir/tmpFile` is purposely used, because it is only\r\n# `Get-ChildItem` without a filename component in the path that stringifies \r\n# the resulting instances to their filename only.\r\n$f = Get-ChildItem tmpDir\r\n\r\n# Demonstrate that $f stringies to just 'tmpFile' - the mere filename.\r\n\"`$f stringifies to: [$f]\"\r\n\r\n# Now bind $f to the -LiteralPath parameter as a *direct argument* of \r\n# several cmdlets, which *fails*, because trying to locate the input object\r\n# as a *string* that is the *filename only* fails, given that file is in\r\n# a *subdirectory*.\r\nRemove-Item -WhatIf -LiteralPath $f\r\nMove-Item -WhatIf -LiteralPath $f foo\r\nCopy-Item -WhatIf -LiteralPath $f foo\r\n```\r\n\r\nThe last 3 calls fail unexpectedly; while this happens not to be destructive / make unexpected changes in this example, it would be the case if namesake files happened to exist in the current location.\r\n\r\n**The fact that whether this problem arises depends on the specific method through which the `[System.IO.FileInfo]`, `[System.IO.DirectoryInfo]` instances were obtained makes this behavior particularly insidious.**\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0 on macOS 10.13.2\r\nPowerShell Core v6.0.0 on Ubuntu 16.04.3 LTS\r\nPowerShell Core v6.0.0 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.674 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":"2018-01-28T23:31:54Z","url":"https://github.com/PowerShell/PowerShell/issues/6057"}
| 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:d9c17789-7ec0-14c0-409a-b231ca0ad52c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E890:3676C:31CD8C:44E2F6:6AFDA393 |
| html-safe-nonce | cc2dd7dfbf2f9313f67f730b54a64eed3ff67f8f2af6d886c3970915fd32181f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODkwOjM2NzZDOjMxQ0Q4Qzo0NEUyRjY6NkFGREEzOTMiLCJ2aXNpdG9yX2lkIjoiMTI3MjY3OTA5MzEyODUwNDIxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7c0add1716804e19c91d251fd98256b5fa9229378441cbb64b1169bce8f986f7 |
| hovercard-subject-tag | issue:292250255 |
| 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/6057/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d55ea2fb8cffb6a4c078e65e03004e2d79d8a85b58c1ce4558a7694813c6620a/PowerShell/PowerShell/issues/6057 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d55ea2fb8cffb6a4c078e65e03004e2d79d8a85b58c1ce4558a7694813c6620a/PowerShell/PowerShell/issues/6057 |
| og:image:alt | Note: Fixing this would be a breaking change, but arguably one that falls into Bucket 3: Unlikely Grey Area. Currently, when you use a ValueFromPipelineByPropertyName parameter attribute, the prope... |
| 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 | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| 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 | 0ddf53eb35c87f1f8df13dbf75c3bdadbfc6e969 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width