github / gh-copilot

Ask for assistance right in your terminal.

Home Page:https://docs.github.com/en/copilot/github-copilot-in-the-cli

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ifalatiksetlog opened this issue Β· comments

Thanks for taking the time to fill out this out! πŸ™‡
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)

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

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.

@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

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

eval "$(gh copilot alias -- bash | sed s/\.exe//g)"

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! ✨

Confirming 1.0.1 good on windows πŸŽ‰

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.

@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 πŸ™‡

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

@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? πŸ™‡

@andyfeller I have created a new issue now.
#54

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