Title: Preventing fallback to default tab-completion behavior works inconsistently between Register-ArgumentCompleter and the [ArgumentCompleter] attribute with script blocks · Issue #19628 · PowerShell/PowerShell · GitHub
Open Graph Title: Preventing fallback to default tab-completion behavior works inconsistently between Register-ArgumentCompleter and the [ArgumentCompleter] attribute with script blocks · Issue #19628 · PowerShell/PowerShell
X Title: Preventing fallback to default tab-completion behavior works inconsistently between Register-ArgumentCompleter and the [ArgumentCompleter] attribute with script blocks · Issue #19628 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/19628
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Preventing fallback to default tab-completion behavior works inconsistently between Register-ArgumentCompleter and the [ArgumentCompleter] attribute with script blocks","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nBy default, when a script block used with `Register-CommandCompleter` or `[ArgumentCompleter]` produces _no output_, PowerShell falls back to default tab-completion (file and directory names).\r\n\r\nThis is usually undesired, and can be prevented by returning `$null` instead, but only from a script block used with `Register-CommandCompleter`, not also with the `[ArgumentCompleter]` attribute.\r\n\r\n* That `$null` works at all seems to be an _accident_, as it seems that it causes an _exception_ in `TabExpansion2` that is swallowed in interactive use, but effectively prevents the fallback.\r\n\r\n * Still, having the ability to suppress fallback to default completion is important, and it should also be available for script-block-based `[ArgumentCompleter]` attributes.\r\n\r\n* By contrast, when using the `[ArgumentCompleter]` attribute with a class that implements `IArgumentCompleter`, fallback suppression _is_ possible, though the behavior is conceptually _reversed_:\r\n * Returning an _empty enumerable_ effectively prevents fallback.\r\n * Returning `$null` functions as an _opt-in_ to fallback behavior.\r\n \r\nI think we need an officially supported - and documented - way to suppress fallback to the default behavior.\r\n\r\n```powershell\r\n# Register an argument completer that uses $null to suppress fallback to\r\n# default tab-completion.\r\nRegister-ArgumentCompleter -CommandName Foo1 -ParameterName Bar -ScriptBlock { $null }\r\nfunction Foo1 { param($Bar) }\r\n\r\n# Try the same with an [ArgumentCompleter] attribute.\r\n# Not effective. Always falls back to default completion.\r\nfunction Foo2 { param([ArgumentCompleter({ $null })] $Bar) }\r\n\r\n# With an IArgumentCompleter implementation, the logic is reversed:\r\n# Returning an *empty* [IEnumerable[CompletionResult]] suppresses the fallback.\r\n# Returning $null opts-into falling back.\r\nclass CustomCompleter : System.Management.Automation.IArgumentCompleter {\r\n [System.Collections.Generic.IEnumerable[System.Management.Automation.CompletionResult]] CompleteArgument(\r\n [string] $CommandName,\r\n [string] $ParameterName,\r\n [string] $WordToComplete,\r\n [System.Management.Automation.Language.CommandAst] $CommandAst,\r\n [System.Collections.IDictionary] $FakeBoundParameters\r\n ) {\r\n # Return empty collection to suppress fallback.\r\n return [System.Collections.Generic.List[System.Management.Automation.CompletionResult]]::new()\r\n }\r\n}\r\n\r\nfunction Foo3 { param([ArgumentCompleter([CustomCompleter])] $Bar) }\r\n\r\n# Perform tab-completion on all 3 functions.\r\n(TabExpansion2 'Foo1 ').CompletionMatches.Count\r\n(TabExpansion2 'Foo2 ').CompletionMatches.Count\r\n(TabExpansion2 'Foo3 ').CompletionMatches.Count\r\n```\r\n\r\n\r\n### Expected behavior\r\n\r\n```console\r\n0\r\n0\r\n0\r\n```\r\n\r\nThat is, all attempts to suppress the fallback behavior should succeed, irrespective of what completer implementation is chosen.\r\n\r\n### Actual behavior\r\n\r\n```console\r\nMethodInvocationException:\r\nLine |\r\n 39 | … return [System.Management.Automation.CommandCompletion]::Complet …\r\n | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n | Exception calling \"CompleteInput\" with \"3\" argument(s): \"Object reference not set to an instance of an object.\"\r\n0\r\n252\r\n0\r\n```\r\n\r\nThat is:\r\n\r\n* The `Register-ArgumentCompleter` script block returning `$null` causes an exception in `TabExpansion2`, but as a result effectively returns _no_ completions, i.e. suppresses fallback behavior.\r\n\r\n* By contrast, returning `$null` from the script block used with `[ArgumentCompleter()]` had _no_ effect, and fallback occurred.\r\n\r\n* The `IArgumentCompleter`-implementing instance returning an empty enumerable effectively suppressed fallback behavior.\r\n\r\n### Error details\r\n\r\n_No response_\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPowerShell 7.4.0-preview.3\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2023-05-08T19:35:41Z","url":"https://github.com/PowerShell/PowerShell/issues/19628"}
| 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:ab5a337c-e1cf-bbff-df09-620e735deb44 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA92:1E6531:7524F:A6486:6AFDD6BD |
| html-safe-nonce | 869e850ea284818dc77f9fb751b5fdc7ff8d73aaa45695063beab0fcf192008c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTkyOjFFNjUzMTo3NTI0RjpBNjQ4Njo2QUZERDZCRCIsInZpc2l0b3JfaWQiOiIyODg2NTI5NTMwMDk1MDY0NzY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | dbf6668bc4e1413b99ff17ff351c4f5f1e7168419c2202fecb783c72254199bd |
| hovercard-subject-tag | issue:1700813624 |
| 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/19628/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5386f30d838a0cac0410bd4449af1b9f1714acdfdee161b5e56e4e05c04c5833/PowerShell/PowerShell/issues/19628 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5386f30d838a0cac0410bd4449af1b9f1714acdfdee161b5e56e4e05c04c5833/PowerShell/PowerShell/issues/19628 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| 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 | 9918bd4004419458bcd838065ac2a76b76ebd11705d73909fff5ab99e996fa23 |
| 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 | a231c174493a6a442c0b0544475c918b30128208 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width