Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tab-completions for powershell don't work on the root command #1775

Closed
binyamin opened this issue Sep 18, 2020 · 18 comments · Fixed by #2237
Closed

tab-completions for powershell don't work on the root command #1775

binyamin opened this issue Sep 18, 2020 · 18 comments · Fixed by #2237
Assignees
Labels
bug Something isn't working needs-investigation CLI team needs to investigate p3 Affects a small number of users or is largely cosmetic windows

Comments

@binyamin
Copy link

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 gh + tab to complete.

@binyamin binyamin added the bug Something isn't working label Sep 18, 2020
@mislav
Copy link
Contributor

mislav commented Sep 18, 2020

Thank you for reporting!

@mislav mislav added the windows label Sep 18, 2020
@SilkyFowl
Copy link

[CompletionResult]::new tooltip param can't use empty string.

~
❯ gh  --version
gh version 1.0.0 (2020-09-16)
https://github.com/cli/cli/releases/tag/v1.0.0

~
❯ $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.7
PSEdition                      Core
GitCommitId                    7.1.0-preview.7
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


~
❯ using namespace System.Management.Automation
>> using namespace System.Management.Automation.Language
>>
>>             [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Show help for command')
>>             [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Show gh version')
>>             [CompletionResult]::new('alias', 'alias', [CompletionResultType]::ParameterValue, 'Create command shortcuts')
>>             [CompletionResult]::new('api', 'api', [CompletionResultType]::ParameterValue, 'Make an authenticated GitHub API request')
>>             [CompletionResult]::new('auth', 'auth', [CompletionResultType]::ParameterValue, 'Login, logout, and refresh your authentication')
>>             [CompletionResult]::new('completion', 'completion', [CompletionResultType]::ParameterValue, 'Generate shell completion scripts')
>>             [CompletionResult]::new('config', 'config', [CompletionResultType]::ParameterValue, 'Manage configuration for gh')
>>             [CompletionResult]::new('credits', 'credits', [CompletionResultType]::ParameterValue, 'View credits for this tool')
>>             [CompletionResult]::new('environment', 'environment', [CompletionResultType]::ParameterValue, '')
>>             [CompletionResult]::new('gist', 'gist', [CompletionResultType]::ParameterValue, 'Create gists')
>>             [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Help about any command')
>>             [CompletionResult]::new('issue', 'issue', [CompletionResultType]::ParameterValue, 'Manage issues')
>>             [CompletionResult]::new('pr', 'pr', [CompletionResultType]::ParameterValue, 'Manage pull requests')
>>             [CompletionResult]::new('release', 'release', [CompletionResultType]::ParameterValue, 'Manage GitHub releases')
>>             [CompletionResult]::new('repo', 'repo', [CompletionResultType]::ParameterValue, 'Create, clone, fork, and view repositories')
>>             [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, '')

CompletionText ListItemText     ResultType ToolTip
-------------- ------------     ---------- -------
--help         help          ParameterName Show help for command
--version      version       ParameterName Show gh version
alias          alias        ParameterValue Create command shortcuts
api            api          ParameterValue Make an authenticated GitHub API request
auth           auth         ParameterValue Login, logout, and refresh your authentication
completion     completion   ParameterValue Generate shell completion scripts
config         config       ParameterValue Manage configuration for gh
credits        credits      ParameterValue View credits for this tool
MethodInvocationException:
Line |
  12 |              [CompletionResult]::new('environment', 'environment', [Co|              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "4" argument(s): "Cannot process argument because the value of argument "toolTip" is null. Change the value of argument "toolTip" to a non-null value."
gist           gist         ParameterValue Create gists
help           help         ParameterValue Help about any command
issue          issue        ParameterValue Manage issues
pr             pr           ParameterValue Manage pull requests
release        release      ParameterValue Manage GitHub releases
repo           repo         ParameterValue Create, clone, fork, and view repositories
MethodInvocationException:
Line |
  19 |              [CompletionResult]::new('version', 'version', [Completion|              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "4" argument(s): "Cannot process argument because the value of argument "toolTip" is null. Change the value of argument "toolTip" to a non-null value."

If set whitespace, ti works.

~
❯ using namespace System.Management.Automation
>> using namespace System.Management.Automation.Language
>>
>> [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, '')
>> [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, ' ')
MethodInvocationException:
Line |
   4 |  [CompletionResult]::new('version', 'version', [CompletionResultType]: …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "4" argument(s): "Cannot process argument because the value of argument "toolTip" is null. Change the value of argument "toolTip" to a non-null value."

CompletionText ListItemText     ResultType ToolTip
-------------- ------------     ---------- -------
version        version      ParameterValue

This bug occurs on Linux as well.
Probably the same on MacOS.

PS /root> using namespace System.Management.Automation
>> using namespace System.Management.Automation.Language
>> 
>> $PSVersionTable         
>> 
>> [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, '')
>> [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, ' ')


Name                           Value
----                           -----
PSVersion                      7.1.0-preview.5
PSEdition                      Core
GitCommitId                    7.1.0-preview.5
OS                             Linux 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
MethodInvocationException: 
Line |
   6 |  [CompletionResult]::new('version', 'version', [CompletionResultType]: …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling ".ctor" with "4" argument(s): "Cannot process argument because the value of argument "toolTip" is null. Change the value of argument "toolTip" to a non-null value."

CompletionText : version
ListItemText   : version
ResultType     : ParameterValue
ToolTip        :  

In my case, Fix environment and version 's CompletionResult constructor, it works!!

~
❯ gh completion -s powershell > temp:/GitHubCliCompletion.ps1

~
❯ # Fixed Empty stirng to Whitepace script
>> . Temp:\GitHubCliCompletion.ps1

~
❯ # works!!
>> gh issue
alias        api          auth         completion   config       credits      environment  gist         help         issue        pr           release      repo         version

Manage issues                                                                                                                                                                                                          

@SilkyFowl
Copy link

I got the following to work:

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

@mislav
Copy link
Contributor

mislav commented Sep 18, 2020

@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

@vilmibm
Copy link
Contributor

vilmibm commented Sep 18, 2020

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

@SilkyFowl
Copy link

Should I open Issue to Cobra?
I'll have to run some errands, so I'll probably be back in half a day or so...

@SilkyFowl
Copy link

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

@vilmibm vilmibm added the p3 Affects a small number of users or is largely cosmetic label Sep 30, 2020
@strickdd
Copy link

strickdd commented Oct 9, 2020

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 is pressed multiple times:
gh pr checkout
and
gh pr checks

Right now, it doesn't complete anything.

@SilkyFowl
Copy link

@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.

@SilkyFowl
Copy link

This Issue's bug will be fixed in spf13/cobra#1208 (comment)
However, it's not yet tested whether you will be able to complete the tabs for partial words.
If there is someone who is familiar with Powershell and Go lang, please check it and it may help me to solve the bug.

@Luap99
Copy link

Luap99 commented Oct 23, 2020

Note: This is not fixed in cobra v1.1.1

@mislav
Copy link
Contributor

mislav commented Oct 23, 2020

@Luap99 Thanks for the heads-up!

@mislav mislav reopened this Oct 23, 2020
@Woznet
Copy link

Woznet commented Dec 8, 2020

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"')

@musm
Copy link

musm commented Dec 23, 2020

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

@just1a-person
Copy link
Contributor

nit: This issue is labeled "Windows". But PowerShell is cross-platform

@Woznet
Copy link

Woznet commented Dec 24, 2020

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.

@vilmibm vilmibm mentioned this issue Apr 27, 2021
18 tasks
@samcoe samcoe added the discuss Feature changes that require discussion primarily among the GitHub CLI team label Apr 7, 2022
@mislav mislav removed the discuss Feature changes that require discussion primarily among the GitHub CLI team label Apr 13, 2022
@mislav mislav added the needs-investigation CLI team needs to investigate label Apr 13, 2022
@mislav mislav self-assigned this Apr 13, 2022
@mislav
Copy link
Contributor

mislav commented May 10, 2022

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

@mislav mislav closed this as completed May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-investigation CLI team needs to investigate p3 Affects a small number of users or is largely cosmetic windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

11 participants