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]: `gh copilot alias` command breaks with zsh on macOS Sonoma 14.4

guibracco opened this issue · comments

What happened?

I've update gh with brew update and brew upgrade.
I've installed gh-copilot with gh extension install github/gh-copilot.
I've tried to set the aliases with gh copilot alias, but the script breaks.

Versions

gh version 2.46.0 (2024-03-20)
macOS Sonoma 14.4
zsh 5.9 (x86_64-apple-darwin23.0)

Relevant terminal output

$ gh copilot alias                        

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

? Which shell to generate aliases for?
> Zsh
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     # long option: reformulate OPT and OPTARG
			OPT="${OPTARG%%=*}"       # extract long option name
			OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
			OPTARG="${OPTARG#=}"      # if long option argument, remove assigning `=`
		fi

		case "$OPT" in
			debug | d)
				GH_DEBUG=api
				;;

			help | h)
				echo "$__USAGE"
				return 0
				;;

			target | t)
				TARGET="$OPTARG"
				;;
		esac
	done

	# shift so that $@, $1, etc. refer to the non-option arguments
	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
}

ghce() {
	FUNCNAME="$funcstack[1]"
	local GH_DEBUG="$GH_DEBUG"

	read -r -d '' __USAGE <<-EOF
	Wrapper around \`gh copilot explain\` to explain a given input command in natural language.

	USAGE
	  $FUNCNAME [flags] <command>

	FLAGS
	  -d, --debug   Enable debugging
	  -h, --help    Display help usage

	EXAMPLES

	# View disk usage, sorted by size
	$ $FUNCNAME 'du -sh | sort -h'

	# View git repository history as text graphical representation
	$ $FUNCNAME 'git log --oneline --graph --decorate --all'

	# Remove binary objects larger than 50 megabytes from git history
	$ $FUNCNAME 'bfg --strip-blobs-bigger-than 50M'
	EOF

	local OPT OPTARG OPTIND
	while getopts "dh-:" OPT; do
		if [ "$OPT" = "-" ]; then     # long option: reformulate OPT and OPTARG
			OPT="${OPTARG%%=*}"       # extract long option name
			OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
			OPTARG="${OPTARG#=}"      # if long option argument, remove assigning `=`
		fi

		case "$OPT" in
			debug | d)
				GH_DEBUG=api
				;;

			help | h)
				echo "$__USAGE"
				return 0
				;;
		esac
	done

	# shift so that $@, $1, etc. refer to the non-option arguments
	shift "$((OPTIND-1))"

	GH_DEBUG="$GH_DEBUG" gh copilot explain "$@"
}

I was facing the same issue and came across this report. The command I ran was

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

But I didn't have an empty line at the end of my .zshrc file so this command actually pasted eval "$(gh copilot alias -- zsh)" next to my previous command and thus breaking my zshrc because of the syntax error. I fixed it by making a new line for this command in my .zshrc
Look for any such syntax errors in your .zshrc
Hopefully this helps.

Thanks, @arpitdalal. I didn't append it to my .zshrc file like you did. I see now that I was mislead by the GitHub Blog post.

It's kinda weird to have an interactive menu in a command that is not supposed to be run that way though.

Thanks, @arpitdalal. I didn't append it to my .zshrc file like you did. I see now that I was mislead by the GitHub Blog post.

It's kinda weird to have an interactive menu in a command that is not supposed to be run that way though.

@guibracco : thank you for opening up this issue and apologies for the confusion! 🙇

Could you share what part of the changelog was misleading?

I ask in order to reflect on the commands design and how to improve instructions. One of the GitHub CLI's Design primers is to fall back to interactive prompts if arguments or flags are not present. That said, I was intending after GA to compare this with other common Unix utilities with similar features for consideration.

@arpitdalal : thank you for supporting fellow users and our community! ✨

@guibracco : thank you for opening up this issue and apologies for the confusion! 🙇

Could you share what part of the changelog was misleading?

I ask in order to reflect on the commands design and how to improve instructions. One of the GitHub CLI's Design primers is to fall back to interactive prompts if arguments or flags are not present. That said, I was intending after GA to compare this with other common Unix utilities with similar features for consideration.

Hi, @andyfeller!

When I read "The new gh copilot alias command generates shell-specific configuration for ghcs and ghce aliases", I run gh copilot alias straight away. And since the command gives an interactive answer back, asking for permisison to send anonymous data and then asking which shell to generate aliases for, it seemed the right way to use it for me. It was only later that I saw I should call it on the .zshrc file to set the functions/aliases for the session — which makes total sense now, but took me a while to get what that output was intended for.

BTW, that's an amazing tool! I've already recommended it to a bunch of data students that are afraid of using shell commands. 👏