Open yan12125 opened 7 months ago
I can confirm this, after upgrading to PS 7.4 tab completion stopped working and I can't get it working anymore.
@yan12125 Do you know how to quickly workaround this? I tried to rename all occurrences to TabExpansion2
but it didn't help, is even possible to quickly workaround it?
Do you know how to quickly workaround this?
I downgraded PowerShell.
I want to have the latest 7.4 version so that's not what I want to do, I can live for some time w/o these completions.
It looks like the fix should be easy, I don't see anything that can cause bigger problems, and the code doesn't hardly depend on anything what was remove by the TabExpansion PR.
It looks like the problem is how things are exported or something?, but I looked to this only 5mins so I can be wrong.
I'm not sure if there is an easy fix. PowerShell < 7.4 calls the TabExpansion
function provided by Chocolatey for custom tab completions, and that function is no longer called by PowerShell > 7.4 as indicated in the aforementionef PR. A proper fix is probably using newer completion APIs like Register-ArgumentCompleter.
Scoop has a similar issue (https://github.com/Moeologist/scoop-completion/issues/35), and a relevant fix (https://github.com/Moeologist/scoop-completion/pull/25) is quite large.
That aren't a good news, it looks like they will need to rewrite it to make it work again, what means we will be some time without tab completion 😞
For background information, see https://github.com/PowerShell/PowerShell/issues/20930#issuecomment-1897472499
Here's a solution that works in v7.4+:
For those looking to apply an immediate fix, they can apply it as follows:
Open the relevant file for editing:
Invoke-Item "$env:ChocolateyInstall\helpers\ChocolateyTabExpansion.ps1"
Paste the code below at the end, save, and then start a new session.
Caveat: Since this obviously breaks the signature of said file, this will only work if your script-execution policy is set to not require local scripts to be signed.
To fix the problem at the source:
Add the code below to https://github.com/chocolatey/choco/blob/develop/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1
If PowerShell versions prior to Windows PowerShell v5.1 do not need to be supported anymore:
If pre-v5.1 support is still needed:
if ($PSVersionTable.PSVersion -ge '7.4') { ... }
function script:Get-AliasNames($exe) {
@($exe, "$exe.exe") + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
}
Register-ArgumentCompleter -Native -CommandName (Get-AliasNames choco) -ScriptBlock {
param($wordToComplete, $commandAst, $cursorColumn)
# NOTE:
# * The stringified form of $commandAst is the command's own command line (irrespective of
# whether other statements are on the same line or whether it is part of a pipeline).
# * However, trailing whitespace is trimmed in the string representation of $commandAst.
# Therefore, when the actual command line ends in space(s), they must be added back
# so that ChocolateyTabExpansion recognizes the start of a new argument.
$ownCommandLine = [string] $commandAst
$ownCommandLine = $ownCommandLine.Substring(0, [Math]::Min($ownCommandLine.Length, $cursorColumn))
$ownCommandLine += ' ' * ($cursorColumn - $ownCommandLine.Length)
ChocolateyTabExpansion $ownCommandLine
}
I've fixed a problem in the code in the previous comment, which now makes it support intra-line expansions properly too - from what I can tell, this makes it suitable for a fix.
However, if you want to avoid having to modify the module, I've posted a polyfill that overrides the TabExpansion2
function only, and, if added to $PROFILE
, should work with all modules that still rely on the legacy TabExpansion
function - see https://github.com/PowerShell/PowerShell/issues/20930#issuecomment-1899191414
Thank you very much for the update! I just need one more line in Register-ArgumentCompleter
script block to make it work: (the line removed during a comment edit)
param($wordToComplete, $commandAst, $cursorColumn)
Without this, the behavior is kind of different from chocolatey on older PowerShell. When I type choco out
and hit the Tab key, an older PowerShell shows the complete subcommand choco outdated
, while the Register-ArgumentCompleter
approach shows choco --help
.
Ouch! Thank you for catching that, @yan12125 - code in the earlier comment is now updated (I had simply made a copy-paste error when only partially replacing the original code).
@mklement0 Are you able to submit a PR for the changes that you're making? It sounds like you have it very close to working universally across PS versions.
@pauby, I've submitted #3387 - hope that's good enough.
Checklist
What You Are Seeing?
Hitting tab after typing
choco
only shows filenamesWhat is Expected?
Choco commands like
install
,upgrade
, etc. should be shownHow Did You Get This To Happen?
choco
and hit tabSystem Details
0 packages installed.
Installed Packages
Output Log
Additional Context
The cause is that Chocolatey only defines
TabExpansion
function to enable completions, while that function is no longer used by PowerShell 7.4 [1]. A solution might be addingTabExpansion2
function (supported since PowerShell 3) besides the existingTabExpansion
function, so that all users with PowerShell 2 ~ 7.4 can have Chocolatey tab completions.As a side note, if
TabExpansion2
is added, https://github.com/chocolatey/choco/issues/2255 will likely be solved as well.[1] https://github.com/PowerShell/PowerShell/pull/18337