cli: tab-completions for powershell don't work on the root command

CLI version: 1.0.0 🚀 Related: #695

Describe the bug

  • gh <tab> does not offer completions, even after running gh completion --shell powershell |Out-String |Invoke-Expression.
  • However, gh <command> <tab> does offer completions.

Steps to reproduce the behavior

  1. Open Powershell
  2. Type gh
  3. Press tab.
  4. No completion occurs.

Expected vs actual behavior

Expected <kbd>gh</kbd> + <kbd>tab</kbd> to complete.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Is this a cobra bug, then, or something we have control over?

@vilmibm I think so, too. So I opened Issue to Cobra. spf13/cobra/issues/1229

Fortunately, this bug is avoidable if only it is known to exist. Until this bug is fixed, I think it’s reasonable to write the following in the Profile.

Invoke-Expression (@(gh completion -s powershell) -replace " ''\)$"," ' ')" -join "`n")

# After Bug Fixed
Invoke-Expression (@(gh completion -s powershell)  -join "`n")

If the pipeline is to be used, write this

gh completion -s powershell | Join-String {$_ -replace " ''\)$"," ' ')"} -Separator "`n" | Invoke-Expression

# After Bug Fixed
gh completion -s powershell | Join-String $_  "`n" | Invoke-Expression

I got the following to work:

Invoke-Expression (@(gh completion -s powershell) -replace " ''\)$"," ' ')" -join "`n")

I cannot reproduce with latest GitHub CLI version. We’ve upgraded Cobra in the meantime; this must have fixed it!

This thread has dragged on a bit with various recommendations. Currently, what should we add to our powershell profile to enable completions?

There are a few ways, if you want to grab the gist I posted you can use this - Invoke-Expression -Command ([System.Net.WebClient]::new().DownloadString('https://gist.githubusercontent.com/WozNet/fcf17f71452ab9efeeb17765694b1797/raw'))

I would avoid using the code generated from - gh completion --shell powershell until they fix the issues I mentioned above. The error with ‘version’ means that entire grouping of commands (alias, auth, config, etc…) won’t work, and the backticks in single quotes causes there own issues.

Thank you for reporting!

I made a few edits which made this a lot more workable for me, I posted the changes here - https://gist.github.com/Woznet/fcf17f71452ab9efeeb17765694b1797

I found 2 errors in the powershell completion script generated from gh completion --shell powershell - gh version 1.4.0 The first with [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, ''), the tooltip part cannot be empty, The second occurring when single quotes are used around backticks, as of v1.4.0 I found 31 lines with single quotes around backticks. One example is ( " - is substituted for the backticks) - [CompletionResult]::new('-a', 'a', [CompletionResultType]::ParameterName, 'Assign people by their "login"')

@strickdd I was able to reproduce it in my environment. After a lot of testing, I also found that I could avoid the error by doing this.

gh completion -s powershell | Join-String {
    $_ -replace " ''\)$"," ' ')" -replace "(^\s+\)\s-join\s';')",'$1 -replace ";$wordToComplete$"' -replace "(\[CompletionResult\]::new\('[\w-]+)'",'$1 '''
} -Separator "`n" | Invoke-Expression

However, a side-effect of the code is that it causes a bug in the tail completion.

Expected

gh repo view -w <TAB>

(Not complete )

Actual

image

image

I was unsure, but I decided to put this code in $profile to see which would be more inconvenient.

Is there a way to get partially typed words to be tab-completed?

For example: gh pr ch<tab>

Should cycle through the following as <tab> is pressed multiple times: gh pr checkout and gh pr checks

Right now, it doesn’t complete anything.

Is this a cobra bug, then, or something we have control over?

@SilkyFowl Thank you for looking into all of this! ❤️

Our PowerShell completions are being generated by Cobra https://github.com/spf13/cobra/blob/master/powershell_completions.go