Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No completions from programs incl gh, hugo and helm until z4h update #265

Closed
urwrstkn8mare opened this issue Apr 11, 2023 · 20 comments
Closed

Comments

@urwrstkn8mare
Copy link

urwrstkn8mare commented Apr 11, 2023

See latest comment for an up to date description of what the actual issue is

Hi I'm not sure what's going on but for some reason the gh completions are not working. It might be related to this? cli/cli#716

Please let me know what information I should I should supply.

# Personal Zsh configuration file. It is strongly recommended to keep all
# shell customization and configuration (including exported environment
# variables such as PATH) in this file or in files sourced from it.
#
# Documentation: https://github.com/romkatv/zsh4humans/blob/v5/README.md.

# Periodic auto-update on Zsh startup: 'ask' or 'no'.
# You can manually run `z4h update` to update everything.
zstyle ':z4h:' auto-update      'ask'
# Ask whether to auto-update this often; has no effect if auto-update is 'no'.
zstyle ':z4h:' auto-update-days '28'

# Move prompt to the bottom when zsh starts and on Ctrl+L.
zstyle ':z4h:' prompt-at-bottom 'yes'

# Keyboard type: 'mac' or 'pc'.
zstyle ':z4h:bindkey' keyboard  'mac'

# Don't start tmux.
zstyle ':z4h:' start-tmux       no

# Mark up shell's output with semantic information.
zstyle ':z4h:' term-shell-integration 'yes'

# Right-arrow key accepts one character ('partial-accept') from
# command autosuggestions or the whole thing ('accept')?
zstyle ':z4h:autosuggestions' forward-char 'accept'

# Recursively traverse directories when TAB-completing files.
zstyle ':z4h:fzf-complete' recurse-dirs 'yes'
zstyle ':z4h:fzf-complete' fzf-bindings tab:repeat

# Enable direnv to automatically source .envrc files.
zstyle ':z4h:direnv'         enable 'no'
# Show "loading" and "unloading" notifications from direnv.
zstyle ':z4h:direnv:success' notify 'yes'

# Enable ('yes') or disable ('no') automatic teleportation of z4h over
# SSH when connecting to these hosts.
zstyle ':z4h:ssh:example-hostname1'   enable 'yes'
zstyle ':z4h:ssh:*.example-hostname2' enable 'no'
# The default value if none of the overrides above match the hostname.
zstyle ':z4h:ssh:*'                   enable 'no'

# Send these files over to the remote host when connecting over SSH to the
# enabled hosts.
zstyle ':z4h:ssh:*' send-extra-files '~/.nanorc' '~/.env.zsh'

# Clone additional Git repositories from GitHub.
#
# This doesn't do anything apart from cloning the repository and keeping it
# up-to-date. Cloned files can be used after `z4h init`. This is just an
# example. If you don't plan to use Oh My Zsh, delete this line.
# z4h install ohmyzsh/ohmyzsh || return

# Install or update core components (fzf, zsh-autosuggestions, etc.) and
# initialize Zsh. After this point console I/O is unavailable until Zsh
# is fully initialized. Everything that requires user interaction or can
# perform network I/O must be done above. Everything else is best done below.
z4h init || return

# Extend PATH.
path=(~/bin $path)

# Export environment variables.
export GPG_TTY=$TTY

# Source additional local files if they exist.
z4h source ~/.config/op/plugins.sh 

# Use additional Git repositories pulled in with `z4h install`.
#
# This is just an example that you should delete. It does nothing useful.
#z4h source ohmyzsh/ohmyzsh/lib/diagnostics.zsh  # source an individual file
#z4h load   ohmyzsh/ohmyzsh/plugins/emoji-clock  # load a plugin

# Define key bindings.
z4h bindkey undo Ctrl+/   Shift+Tab  # undo the last command line change
z4h bindkey redo Option+/            # redo the last undone command line change

z4h bindkey z4h-cd-back    Shift+Left   # cd into the previous directory
z4h bindkey z4h-cd-forward Shift+Right  # cd into the next directory
z4h bindkey z4h-cd-up      Shift+Up     # cd into the parent directory
z4h bindkey z4h-cd-down    Shift+Down   # cd into a child directory

# https://github.com/romkatv/zsh4humans/blob/v5/tips.md#prompt
z4h bindkey z4h-eof Ctrl+D
setopt ignore_eof
POSTEDIT=$'\n\e[A'

# https://github.com/romkatv/zsh4humans/blob/v5/tips.md#current-directory
zstyle ':z4h:fzf-dir-history' fzf-bindings tab:repeat
zstyle ':z4h:cd-down'         fzf-bindings tab:repeat

z4h bindkey z4h-fzf-dir-history Shift+Down

# Autoload functions.
autoload -Uz zmv

# Define functions and completions.
function md() { [[ $# == 1 ]] && mkdir -p -- "$1" && cd -- "$1" }
compdef _directories md

# Define named directories: ~w <=> Windows home directory on WSL.
[[ -z $z4h_win_home ]] || hash -d w=$z4h_win_home

# Define aliases.
alias tree='tree -a -I .git'
alias clear=z4h-clear-screen-soft-bottom

# Add flags to existing aliases.
alias ls="${aliases[ls]:-ls} -A"

# Set shell options: http://zsh.sourceforge.net/Doc/Release/Options.html.
setopt glob_dots     # no special treatment for file names with a leading dot
setopt no_auto_menu  # require an extra TAB press to open the completion menu
@urwrstkn8mare urwrstkn8mare changed the title Can't use completions from homebrew site-functions Can't use completions from gh Apr 12, 2023
@urwrstkn8mare urwrstkn8mare changed the title Can't use completions from gh Can't use completions from programs using Cobra Apr 12, 2023
@urwrstkn8mare
Copy link
Author

Hi I just realised that this problem has something to do with programs built with Cobra. This is because I have only observed this in gh, kubernetes, Hugo, and the 1password cli. All of which use the cobra cli.

@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

What do I need to do to reproduce this problem? Please describe all steps.

@urwrstkn8mare
Copy link
Author

urwrstkn8mare commented Apr 12, 2023

Mb for forgetting:

Reproduce steps (for Mac):

  1. brew install gh hugo fish
  2. gh <TAB> and hugo <TAB>
  3. Observe directories are only being listed (no completions)
  4. To confirm that there are actually completions somewhere run fish to go into the fish shell and try steps 2 and 3, this time you should get completions.

I'm not sure but this might possibly be related: spf13/cobra#1534. I tried combing through the zsh4humans source code but I wasn't able to tell when the syntax-highlighting plugin was being sourced so no idea if that's the issue or why it would even be so.

@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

Please post the minimal .zshrc with which you DO have completions for gh. Make sure to have no .zshenv.

@urwrstkn8mare
Copy link
Author

urwrstkn8mare commented Apr 12, 2023

🤦‍♂️ I apologise @romkatv it turns out all it took was a quick z4h update after brew install <some tools, not all> (diy++fysh combined with the .zprofile recommended by HomeBrew does not have the same requirement). I'm not sure if this is something that most people know as it is only required for some tools (thinking tools built with Cobra). I don't know why though because I deleted the .cache directory to get z4h to reinstall itself and even that didn't fix it. Something about the z4h update command.

Thanks for your patience, it'd probably be a good idea to put a note in the tips.md about this.

btw if you want any help in diagnosing why this is happening and possibly fixing the issue I'll still be here

Edit: z4h update isn't required if you just create .zprofile with eval "$(/opt/homebrew/bin/brew shellenv)" but I assumed this has a performance penalty.

@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

Sometimes you need to restart zsh after installing a new tool in order for the completions for the new command to be enabled. This is true whether you are using zsh4humans or not. You can restart zsh with exec zsh.

I'm not aware of any tool requiring z4h update for its completions to get enabled. If you believe there are cases of this nature, please provide detailed instructions how I can reproduce this.

create .zprofile with eval "$(/opt/homebrew/bin/brew shellenv)"

This will make shell slower with no advantages. Don't do this. In general, avoid adding anything to your zsh startup files unless you can clearly see the positive effect of it. Many of the edits I see that people perform on their zsh4humans configs simply make shell slower. I guess they do those edits because they believe the edits do something while in fact they have no observable effects but still take long time to execute.

@urwrstkn8mare
Copy link
Author

urwrstkn8mare commented Apr 12, 2023

Sometimes you need to restart zsh after installing a new tool in order for the completions for the new command to be enabled. This is true whether you are using zsh4humans or not. You can restart zsh with exec zsh.

I see, however I can definitely confirm that exec zsh does not enable the completions for the new command.

This will make shell slower with no advantages.

Assumed correctly then, I guess I'll just stick to running z4h update if I find a new tool's completions aren't working.

If you believe there are cases of this nature, please provide detailed instructions how I can reproduce this.

Steps to reproduce on Mac with fresh install of zsh4humans (just an example):

  1. brew install hugo
  2. hugo <TAB> only shows directories and stuff
  3. exec zsh
  4. hugo <TAB> behaviour remains unchanged.
  5. z4h update
  6. hugo <TAB> now shows completions
    Notes:
  • Will also work with gh and helm. (my theory is it has something to do with Cobra)
  • If redoing the steps make sure to begin with z4h update as you will find the the completions are 'remembered' after uninstalling then installing Hugo.

Edit: Not very confident anymore that it has something to do with Cobra as rclone has completions.

@urwrstkn8mare urwrstkn8mare changed the title Can't use completions from programs using Cobra No completions from programs incl gh, hugo and helm until z4h update Apr 12, 2023
@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

Steps to reproduce on Mac with fresh install of zsh4humans (just an example):

  1. brew install hugo
  2. hugo <TAB> only shows directories and stuff
  3. exec zsh
  4. hugo <TAB> behaviour remains unchanged.
  5. z4h update
  6. hugo <TAB> now shows completions

Thanks! I reproduced this and fixed a bug in zsh4humans (bf252e6). Now completions will be enabled after restarting zsh (e.g., with exec zsh or by restating the terminal).

@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

In the future I would recommend abstaining from updating the issue title and description so many times. In this particular case it would've been best if you didn't edit it at all. If you change your mind about something, simply post it as a comment.

@urwrstkn8mare
Copy link
Author

Thanks! I reproduced this and fixed a bug in zsh4humans (bf252e6). Now completions will be enabled after restarting zsh (e.g., with exec zsh or by restating the terminal).

Oh wow what a quick fix! How did you find it so fast? I can confirm that my examples are fixed. Thanks!

Could you try these steps? Is it a related issue or something else entirely?

  1. brew install rust fish
  2. cargo <TAB> only shows directories and stuff
  3. exec zsh
  4. cargo <TAB> behaviour remains unchanged.
  5. z4h update
  6. cargo <TAB> does not show completions
  7. fish
  8. cargo <TAB> completions!

In the future I would recommend abstaining from updating the issue title and description so many times. In this particular case it would've been best if you didn't edit it at all. If you change your mind about something, simply post it as a comment.

Ah sorry about that. I thought I may have been confusing at the beginning.

@romkatv
Copy link
Owner

romkatv commented Apr 12, 2023

How did you find it so fast?

Knowing the code helps. At least 90% of my time spent on issues is trying to induce the reporters to post instructions that would allow me to reproduce their problems. If I can reproduce something, fixing is often easy.

Is it a related issue or something else entirely?

  1. brew install rust fish
  2. cargo <TAB> only shows directories and stuff
  3. exec zsh
  4. cargo <TAB> behaviour remains unchanged.
  5. z4h update
  6. cargo <TAB> does not show completions
  7. fish
  8. cargo <TAB> completions!

This was "something else entirely". Thanks for the report. Fixed.

@urwrstkn8mare
Copy link
Author

Thanks for all the help!

@urwrstkn8mare
Copy link
Author

This was "something else entirely". Thanks for the report. Fixed.

Sorry I found another one that I think might be related to the one that was "something else entirely". Try the steps for rust with mas.

@urwrstkn8mare
Copy link
Author

Is there a way to make them all work out of the box?

@urwrstkn8mare urwrstkn8mare reopened this Apr 13, 2023
@romkatv
Copy link
Owner

romkatv commented Apr 13, 2023

Sorry I found another one that I think might be related to the one that was "something else entirely". Try the steps for rust with mas.

I've ran brew install mas. This command didn't install completions for mas.

% ls -1 $HOMEBREW_PREFIX/share/zsh/site-functions
_brew
_cargo
_docker
_git
git-completion.bash

Is there a way to make them all work out of the box?

In zsh, like in bash, for a command to support custom completions, somebody has to write a completion function for it. If there is no completion function for a command, the default completion function is used, which completes files.

Specifically for mas: mas-cli/mas#222

@urwrstkn8mare
Copy link
Author

Oh sorry my bad I didn't realise the completions were available only for the other shells. This isn't really an issue anymore but what about using bashcompinit to support programs that only have bash completions?

@romkatv
Copy link
Owner

romkatv commented Apr 13, 2023

what about using bashcompinit to support programs that only have bash completions?

Indeed, sometimes it's possible to create a completion function for zsh by piggybacking on the existing completion function for bash. If this is something you are interested in doing for mas, see mas-cli/mas#222.

@urwrstkn8mare
Copy link
Author

Ah ok thank you!

@dmuiX
Copy link

dmuiX commented Mar 6, 2024

what about using bashcompinit to support programs that only have bash completions?

Indeed, sometimes it's possible to create a completion function for zsh by piggybacking on the existing completion function for bash. If this is something you are interested in doing for mas, see mas-cli/mas#222.

@romkatv
How would I do that? is there any tutorial??
Thanks!!

@romkatv
Copy link
Owner

romkatv commented Mar 7, 2024

I don't know if there is a tutorial for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants