Handle empty-string and null-value results returned from custom argument completer more properly#27398
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the tab-completion pipeline against invalid output from script-based argument completers by filtering out results that would otherwise cause CompletionResult construction to throw, improving completion robustness for custom/native registered completers.
Changes:
- Skip
nullitems returned fromScriptBlock.Invoke()results when processing custom completer output. - Ignore empty-string (
"") string results from custom completers to avoidCompletionResult(string)throwing. - Change the internal “handled” return value to reflect whether any valid completion items were actually added.
|
So somewhat complicating the matter, I've seen some folks think that returning an empty string (or I don't know if that's a good enough reason not to fix the clearly broken code here but it is worth mentioning we'd likely get quite a few confused issues if the behavior changes. |
|
That's a good point. I think we should keep that behavior, but in a more explicit way instead of depending on the exception :) |
…ent completer more properly (PowerShell#27398)
PR Summary
The constructor of
CompletionResultdoesn't allow an empty string argument. Exception will be thrown if an empty string is passed in.For example, with an argument completer defined like this (or, use
return $null):You will get an exception when calling
TabExpansion2forping:This PR improves how
InvokeScriptArgumentCompleterhandles the return values from a custom argument completer script block:ArgumentExceptionorNullReferenceExceptionwhen the return values includenullor empty string.$nullor an empty string, due to the exception (which gets swallowed byPSReadLine), the user-perceived behavior is -- no result, and default completion (e.g. file names) is suppressed. So, it's not uncommon for developers to intentionally depend on this behavior suppress file name completion.$nullor empty string, the completion is considered successfully, and we will not fall back to default completions.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header