github / gh-copilot

Ask for assistance right in your terminal.
https://docs.github.com/en/copilot/github-copilot-in-the-cli
738 stars 31 forks source link

[BUG]: aliases not working for PowerShell as command is wrong is .ps1 #47

Closed ifalatiksetlog closed 7 months ago

ifalatiksetlog commented 7 months ago

Thanks for taking the time to fill out this out! :bow: By submitting this issue, you agree to follow our Code of Conduct.

What happened?

What did you do? What happened? What did you expect to happen? I ran these commands as provided by the docs:

$GH_COPILOT_PROFILE = Join-Path -Path $(Split-Path -Path $PROFILE -Parent) -ChildPath "gh-copilot.ps1"
gh copilot alias -- pwsh | Out-File ( New-Item -Path $GH_COPILOT_PROFILE -Force )
echo ". $GH_COPILOT_PROFILE" >> $PROFILE

I expected the aliases ghce and ghcs to work after reloading the profile.

However this does not work, as the command that is executed is gh copilot.exe ... instead of gh copilot .... This results in unknown command "copilot.exe" for "gh"

Versions

What versions of the relevant software are you running? For example: gh v2.37.0, gh-copilot v0.5.2-beta PowerShell 7.4.1 Windows Terminal Preview 1.20.10572.0 gh version 2.46.0 (2024-03-20) gh-copilot.exe version 1.0.0 (2024-03-18)

andyfeller commented 7 months ago

@ifalatiksetlog : thank you for opening up this issue! 🙇

Could you share the resulting of Get-Command ghcs | Select -ExpandProperty ScriptBlock please?

gh copilot alias -- powershell should emit the following, which doesn't reference .exe at all. 🤔

    # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
    # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
    param(
        [ValidateSet('gh', 'git', 'shell')]
        [Alias('t')]
        [String]$Target = 'shell',

        [Parameter(Position=0, ValueFromRemainingArguments)]
        [string]$Prompt
    )
    begin {
        # Create temporary file to store potential command user wants to execute when exiting
        $executeCommandFile = New-TemporaryFile

        # Store original value of GH_DEBUG environment variable
        $envGhDebug = $Env:GH_DEBUG
    }
    process {
        if ($PSBoundParameters['Debug']) {
            $Env:GH_DEBUG = 'api'
        }

        gh copilot suggest -t $Target -s "$executeCommandFile" $Prompt
    }
    end {
        # Execute command contained within temporary file if it is not empty
        if ($executeCommandFile.Length -gt 0) {
            # Extract command to execute from temporary file
            $executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()

            # Insert command into PowerShell up/down arrow key history
            [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)

            # Insert command into PowerShell history
            $now = Get-Date
            $executeCommandHistoryItem = [PSCustomObject]@{
                CommandLine = $executeCommand
                ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
                StartExecutionTime = $now
                EndExecutionTime = $now.AddSeconds(1)
            }
            Add-History -InputObject $executeCommandHistoryItem

            # Execute command
            Write-Host "`n"
            Invoke-Expression $executeCommand
        }
    }
    clean {
        # Clean up temporary file used to store potential command user wants to execute when exiting
        Remove-Item -Path $executeCommandFile

        # Restore GH_DEBUG environment variable to its original value
        $Env:GH_DEBUG = $envGhDebug
    }
ifalatiksetlog commented 7 months ago

gh copilot alias -- powershell doesn't work at all for me, fails with ✗ Error: unsupported shell: powershell is not one of bash, pwsh, zsh. The command with pwsh results in:

gh copilot alias -- pwsh
function ghcs {
        # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
        # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
        param(
                [ValidateSet('gh', 'git', 'shell')]
                [Alias('t')]
                [String]$Target = 'shell',

                [Parameter(Position=0, ValueFromRemainingArguments)]
                [string]$Prompt
        )
        begin {
                # Create temporary file to store potential command user wants to execute when exiting
                $executeCommandFile = New-TemporaryFile

                # Store original value of GH_DEBUG environment variable
                $envGhDebug = $Env:GH_DEBUG
        }
        process {
                if ($PSBoundParameters['Debug']) {
                        $Env:GH_DEBUG = 'api'
                }

                gh copilot.exe suggest -t $Target -s "$executeCommandFile" $Prompt
        }
        end {
                # Execute command contained within temporary file if it is not empty
                if ($executeCommandFile.Length -gt 0) {
                        # Extract command to execute from temporary file
                        $executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()

                        # Insert command into PowerShell up/down arrow key history
                        [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)

                        # Insert command into PowerShell history
                        $now = Get-Date
                        $executeCommandHistoryItem = [PSCustomObject]@{
                                CommandLine = $executeCommand
                                ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
                                StartExecutionTime = $now
                                EndExecutionTime = $now.AddSeconds(1)
                        }
                        Add-History -InputObject $executeCommandHistoryItem

                        # Execute command
                        Write-Host "`n"
                        Invoke-Expression $executeCommand
                }
        }
        clean {
                # Clean up temporary file used to store potential command user wants to execute when exiting
                Remove-Item -Path $executeCommandFile

                # Restore GH_DEBUG environment variable to its original value
                $Env:GH_DEBUG = $envGhDebug
        }
}

function ghce {
        # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
        # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
        param(
                [Parameter(Position=0, ValueFromRemainingArguments)]
                [string[]]$Prompt
        )
        begin {
                # Store original value of GH_DEBUG environment variable
                $envGhDebug = $Env:GH_DEBUG
        }
        process {
                if ($PSBoundParameters['Debug']) {
                        $Env:GH_DEBUG = 'api'
                }

                gh copilot.exe explain $Prompt
        }
        clean {
                # Restore GH_DEBUG environment variable to its original value
                $Env:GH_DEBUG = $envGhDebug
        }
}

I already manually removed the .exe from the .ps1 file, so the Get-Command ghcs | Select -ExpandProperty ScriptBlock doesn't contain .exe anymore.

andyfeller commented 7 months ago

@ifalatiksetlog : thanks for the follow up! taking a break during lunch to check this on my personal laptop, I see what you are describing:

PowerShell 7.4.1
PS C:\Users\andre> gh ext list
NAME        REPO               VERSION
gh copilot  github/gh-copilot  v0.5.4-beta

PS C:\Users\andre> gh ext upgrade gh-copilot
[copilot]: upgraded from v0.5.4-beta to v1.0.0
✓ Successfully upgraded extension

PS C:\Users\andre> gh copilot alias -- pwsh
function ghcs {
        # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
        # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
        param(
                [ValidateSet('gh', 'git', 'shell')]
                [Alias('t')]
                [String]$Target = 'shell',

                [Parameter(Position=0, ValueFromRemainingArguments)]
                [string]$Prompt
        )
        begin {
                # Create temporary file to store potential command user wants to execute when exiting
                $executeCommandFile = New-TemporaryFile

                # Store original value of GH_DEBUG environment variable
                $envGhDebug = $Env:GH_DEBUG
        }
        process {
                if ($PSBoundParameters['Debug']) {
                        $Env:GH_DEBUG = 'api'
                }

                gh copilot.exe suggest -t $Target -s "$executeCommandFile" $Prompt
        }
        end {
                # Execute command contained within temporary file if it is not empty
                if ($executeCommandFile.Length -gt 0) {
                        # Extract command to execute from temporary file
                        $executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()

                        # Insert command into PowerShell up/down arrow key history
                        [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)

                        # Insert command into PowerShell history
                        $now = Get-Date
                        $executeCommandHistoryItem = [PSCustomObject]@{
                                CommandLine = $executeCommand
                                ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
                                StartExecutionTime = $now
                                EndExecutionTime = $now.AddSeconds(1)
                        }
                        Add-History -InputObject $executeCommandHistoryItem

                        # Execute command
                        Write-Host "`n"
                        Invoke-Expression $executeCommand
                }
        }
        clean {
                # Clean up temporary file used to store potential command user wants to execute when exiting
                Remove-Item -Path $executeCommandFile

                # Restore GH_DEBUG environment variable to its original value
                $Env:GH_DEBUG = $envGhDebug
        }
}

I think I understand why this is happening and will work to get this fixed, thank you for your patience 🙇

To elaborate, the alias definition uses information about the extension itself to render the shell configuration and Windows extension wasn't accounted for fully

PS C:\Users\andre\AppData\Local\GitHub CLI\extensions\gh-copilot> ls

    Directory: C:\Users\andre\AppData\Local\GitHub CLI\extensions\gh-copilot

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           3/22/2024 12:17 PM       24070656 gh-copilot.exe
-a---           3/22/2024 12:17 PM            159 manifest.yml
dubois commented 7 months ago

There is a similar issue for bash, which I work around with

eval "$(gh copilot alias -- bash | sed s/\.exe//g)"
andyfeller commented 7 months ago

Appreciate everyone's patience as https://github.com/github/gh-copilot/releases/tag/v1.0.1 has been release, which I hope addresses both #47 or #38! 🙇

Big thanks for help from our user community including @ifalatiksetlog, @xt0rted, @rlove, @masewo, @dubois as well as out of office help from @colinshum! ✨

felickz commented 7 months ago

Confirming 1.0.1 good on windows 🎉

Ali-thepro commented 7 months ago

I get a different error,

unknown command "copilot-internal" for "gh"

Usage: gh [flags]

Available commands: actions alias api auth browse codespace completion config extension gist gpg-key help issue pr release repo run secret ssh-key workflow

This is the error I get. I am using zsh on ubuntu wsl.

andyfeller commented 7 months ago

@Ali-thepro : my apologies, there was a typo in the v1.0.0 release notes that has been fixed. Please change the command you are running to gh copilot. Sincere apologies for confusion 🙇

Ali-thepro commented 7 months ago

@andyfeller I get this error after adding the updated version (eval):1: defining function based on alias ghcs' (eval):1: parse error near()'

andyfeller commented 7 months ago

@Ali-thepro : I think we should create a new issue including the information the issue requests such as version info, the commands you are executing, what is in your shell config, etc.

Could I trouble you for that please? 🙇

Ali-thepro commented 7 months ago

@andyfeller I have created a new issue now. https://github.com/github/gh-copilot/issues/54

ifalatik commented 7 months ago

@andyfeller just a heads up: I installed the extension on my personal PC, and the following two issues occured:

  1. My $PROFILE location contains a space, so echo ". $GH_COPILOT_PROFILE" >> $PROFILE results in . C:\...\... ...\... in the $PROFILE, which of course fails. Should be . "C:\...\... ...\..."
  2. If you don't execute the initial gh copilot setup/configuration before running the alias commands, the configuration questions will be present in the .ps1 file as well.

If you'd prefer, I can create a new issue for each :D

andyfeller commented 7 months ago

@andyfeller just a heads up:

I installed the extension on my personal PC, and the following two issues occured:

  1. My $PROFILE location contains a space, so echo ". $GH_COPILOT_PROFILE" >> $PROFILE results in

. C:\...\... ...\... in the $PROFILE, which of course failes. Should be

. "C:\...\... ...\..."

  1. If you don't execute the initial gh copilot setup/configuration before running the alias commands, the configuration questions will be present in the .ps1 file as well.

If you'd prefer, I can create a new issue for each :D

Thanks for sharing that, @ifalatik! I think there might be issues for both, but will follow up on them regardless. Thank you again for your patience!