github / gh-copilot

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

[BUG]: zsh hangs forever after setting up aliases #40

Open masewo opened 3 months ago

masewo commented 3 months ago

What happened?

echo 'eval "$(gh copilot alias -- zsh)"' >> ~/.zshrc After executing this command the zsh does not start anymore. It hangs forever but it can be aborted with CTRL + C. Expectation: zsh works with aliases setup.

Can be healed by running one time manually gh copilot alias -- zsh and accepting the data collecting.

Versions

gh v2.46.0, gh-copilot v1.0.0

Relevant terminal output

none

andyfeller commented 3 months ago

@masewo : thank you for opening this issue! 🙇 Being a Zsh user, I'd like to figure out how to replicate this and what might be causing this.

Showing my simplified setup

~ $ zsh --version

zsh 5.9 (x86_64-apple-darwin23.0)

~ $ cat ~/.zshrc

export EDITOR=vim
export PATH=/usr/local/opt/go/libexec/bin:$HOME/go/bin:$HOME/.dotnet:$HOME/bin:$PATH
export ZSH="$HOME/.oh-my-zsh"

ZSH_THEME="risto"
plugins=(git)
source $ZSH/oh-my-zsh.sh

alias vim="vim -o"
alias mvim="mvim -o"

export GH_PAGER="cat"

eval "$(gh copilot alias -- zsh)"

~ $ declare -f ghcs

ghcs () {
    FUNCNAME="$funcstack[1]" 
    TARGET="shell" 
    local GH_DEBUG="$GH_DEBUG" 
    read -r -d '' __USAGE <<EOF
Wrapper around \`gh copilot suggest\` to suggest a command based on a natural language description of the desired output effort.
Supports executing suggested commands if applicable.

USAGE
  $FUNCNAME [flags] <prompt>

FLAGS
  -d, --debug              Enable debugging
  -h, --help               Display help usage
  -t, --target target      Target for suggestion; must be shell, gh, git
                           default: "$TARGET"

EXAMPLES

- Guided experience
  $ $FUNCNAME

- Git use cases
  $ $FUNCNAME -t git "Undo the most recent local commits"
  $ $FUNCNAME -t git "Clean up local branches"
  $ $FUNCNAME -t git "Setup LFS for images"

- Working with the GitHub CLI in the terminal
  $ $FUNCNAME -t gh "Create pull request"
  $ $FUNCNAME -t gh "List pull requests waiting for my review"
  $ $FUNCNAME -t gh "Summarize work I have done in issues and pull requests for promotion"

- General use cases
  $ $FUNCNAME "Kill processes holding onto deleted files"
  $ $FUNCNAME "Test whether there are SSL/TLS issues with github.com"
  $ $FUNCNAME "Convert SVG to PNG and resize"
  $ $FUNCNAME "Convert MOV to animated PNG"
EOF
    local OPT OPTARG OPTIND
    while getopts "dht:-:" OPT
    do
        if [ "$OPT" = "-" ]
        then
            OPT="${OPTARG%%=*}" 
            OPTARG="${OPTARG#"$OPT"}" 
            OPTARG="${OPTARG#=}" 
        fi
        case "$OPT" in
            (debug | d) GH_DEBUG=api  ;;
            (help | h) echo "$__USAGE"
                return 0 ;;
            (target | t) TARGET="$OPTARG"  ;;
        esac
    done
    shift "$((OPTIND-1))"
    TMPFILE="$(mktemp -t gh-copilotXXX)" 
    trap 'rm -f "$TMPFILE"' EXIT
    if GH_DEBUG="$GH_DEBUG" gh copilot suggest -t "$TARGET" "$@" --shell-out "$TMPFILE"
    then
        if [ -s "$TMPFILE" ]
        then
            FIXED_CMD="$(cat $TMPFILE)" 
            print -s "$FIXED_CMD"
            echo
            eval "$FIXED_CMD"
        fi
    else
        return 1
    fi
}
masewo commented 3 months ago

@andyfeller zsh 5.9 (x86_64-apple-darwin23.0) on Sonoma 14.4.

I installed it on two nearly identical Macs. On one it worked fine out of the box (it already had preinstalled an earlier version of GitHub Copilot CLI and was upgraded today) the other problematic machine never had GitHub Copilot CLI installed before.

I cannot reproduce the error now anymore because I "fixed" it by accepting data collection manually. Do you know how I can revert this?

your-diary commented 3 months ago

@masewo

I "fixed" it by accepting data collection manually. Do you know how I can revert this?

$ gh copilot config

? What would you like to configure?
> Optional Usage Analytics

? Allow GitHub to collect optional usage data to help us improve? This data does not include your queries.
> No
masewo commented 3 months ago

@your-diary Thanks. I mean how can the setting be reverted to "unset"? Maybe deleting some internal config file?

your-diary commented 3 months ago

@masewo

$ cat ~/.config/gh-copilot/config.yml #Check the content before removing it.
$ rm ~/.config/gh-copilot/config.yml
masewo commented 3 months ago

Thanks. Executing these commands:

➜  ~ cat .config/gh-copilot/config.yml
optional_analytics: true
➜  ~ rm .config/gh-copilot/config.yml

Now I have again the following behaviour: Open a new iTerm2 tab results in a hanging shell:

Last login: Fri Mar 22 19:04:22 on ttys001

If I cancel it with CTRL + C I get:

(eval):1: bad pattern: ^[7^[[?25l^[8^[[0G^[[2K

@andyfeller Having eval "$(gh copilot alias -- zsh)" by itself or moving at the top of .zshrc does not change anything.

andyfeller commented 3 months ago

@masewo @your-diary : Okay, I think I understand what's going on here. 🤦 Thank you again for patience

In the case you have never used gh copilot, it will ask you to opt in to sending anonymized telemetry before continuing, which is only a problem if the first time you use the extension is eval "$(gh copilot alias -- zsh)".

As a short term workaround, either of the following should workaround the issue until a patch can be issued:

  1. Use either gh copilot suggest or gh copilot explain and opt in or out of sending anonymized telemetry
  2. Use gh copilot config to opt in or out of sending anonymized telemetry
RealKai42 commented 3 months ago

similar problem zsh 5.9 (x86_64-apple-darwin23.0) on Sonoma 14.4.

I add

echo 'eval "$(gh copilot alias -- zsh)"' >> ~/.zshrc

to my .zshrc file, then

source ~/.zshrc
/Users/myname/.zshrc:export:155: not valid in this context: ghcs() {\n\tFUNCNAME

I have open anonymized telemetry

PhoenixmitX commented 6 days ago

This also happens in bash, when you run eval "$(gh copilot alias -- bash)" it freezes the shell without any hint why. After debugging my .bashrc I found out it was this command.

If you press the down (or up) arrow key before pressing Strl+C, it will execute the `yes' command, which spams the letter y into the console.... otherwise it will just give you two cryptic error messages:

command ? not found
command No not found.

If you need this telemetry check, please generate valid bash code like

echo aliases not generated, please ...