Skip to content

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

Description

@binyamin

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.

Activity

  1. mislav commented on Sep 18, 2020

    @mislav
    Contributor

    Thank you for reporting!

  2. added
    windowsRelated to Windows hosts or runners
    on Sep 18, 2020
  3. SilkyFowl commented on Sep 18, 2020

    @SilkyFowl

    [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                                                                                                                                                                                                          
    
    
  4. SilkyFowl commented on Sep 18, 2020

    @SilkyFowl

    I got the following to work:

    Invoke-Expression (@(gh completion -s powershell) -replace " ''\)$"," ' ')" -join "`n")
  5. mislav commented on Sep 18, 2020

    @mislav
    Contributor

    @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

  6. vilmibm commented on Sep 18, 2020

    @vilmibm
    Contributor

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

  7. SilkyFowl commented on Sep 19, 2020

    @SilkyFowl

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

  8. SilkyFowl commented on Sep 20, 2020

    @SilkyFowl

    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
  9. added
    priority-3Affects a small number of users or is largely cosmetic
    on Sep 30, 2020
  10. strickdd commented on Oct 9, 2020

    @strickdd

    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.

  11. SilkyFowl commented on Oct 10, 2020

    @SilkyFowl

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

  12. SilkyFowl commented on Oct 10, 2020

    @SilkyFowl

    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.

  13. Luap99 commented on Oct 23, 2020

    @Luap99

    Note: This is not fixed in cobra v1.1.1

  14. mislav commented on Oct 23, 2020

    @mislav
    Contributor

    @Luap99 Thanks for the heads-up!

  15. reopened this on Oct 23, 2020
  16. woznet commented on Dec 8, 2020

    @woznet

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

  17. musm commented on Dec 23, 2020

    @musm

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

  18. just1a-person commented on Dec 23, 2020

    @just1a-person
    Contributor

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

  19. woznet commented on Dec 24, 2020

    @woznet

    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.

  20. added
    discussFeature changes that require discussion primarily among the GitHub CLI team
    on Apr 7, 2022
  21. added and removed
    discussFeature changes that require discussion primarily among the GitHub CLI team
    on Apr 13, 2022
  22. self-assigned this
    on Apr 13, 2022
  23. mislav commented on May 10, 2022

    @mislav
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneeds-investigationCLI team needs to investigatepriority-3Affects a small number of users or is largely cosmeticwindowsRelated to Windows hosts or runners

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions