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

ZSH Cargo autocompletion not working #1821

Closed
andradei opened this issue Apr 29, 2019 · 14 comments
Closed

ZSH Cargo autocompletion not working #1821

andradei opened this issue Apr 29, 2019 · 14 comments
Labels

Comments

@andradei
Copy link

Problem
cargo tab completion is not working. I installed it the same way I did rustup, which works.

Steps

  1. rustup completions zsh cargo > /usr/local/share/zsh-completions/_cargo
  2. On my .zshrc I have
fpath=(/usr/local/share/zsh-completions $fpath)
compinit
  1. Open a new terminal or reload .zshrc

Notes

Output of rustup --version: rustup 1.18.1 (462683b 2019-04-25)
Output of rustup show:

Default host: x86_64-apple-darwin

installed toolchains
--------------------

stable-x86_64-apple-darwin
nightly-x86_64-apple-darwin

active toolchain
----------------

stable-x86_64-apple-darwin (default)
rustc 1.34.0 (91856ed52 2019-04-10)
@andradei andradei added the bug label Apr 29, 2019
@kinnison
Copy link
Contributor

I imagine this is an issue with the zsh completions provided for cargo, rather than rustup itself.

I have just tried manually installing the cargo completions into my zsh setup and can confirm they seem to do nothing.

I'd suggest filing an issue against https://github.com/rust-lang/cargo referencing this issue so that we can discuss it usefully across the projects.

@ehuss
Copy link
Contributor

ehuss commented Sep 12, 2019

I'm not sure, but it seems to be an issue with how rustup is creating a completion file using source. If I copy the actual source into ~/.zfunc/ and run compinit -D, it seems to work fine.

burjui added a commit to burjui/rustup.rs that referenced this issue Sep 15, 2019
@burjui
Copy link
Contributor

burjui commented Sep 15, 2019

@ehuss I didn't even know about the completion dump file and that _cargo that rustup produces does contain the actual completions, just sourceing them instead. Indeed, copying the sourced file instead works.

Why doesn't sourcework though? It is supposed to be the equivalent of copying the contents of the sourced file into the script. Then it hit me: the actual completion file contains the #compdef cargo line at the start, so when you source it, it is ignored. I've always hated magic comments, and this issue is a perfect example why. Anyway, if you take _cargo generated by rustup

source $(rustc --print sysroot)/share/zsh/site-functions/_cargo

and add the line to it:

#compdef cargo
source $(rustc --print sysroot)/share/zsh/site-functions/_cargo

it works just fine. I feel dumb now for not going all the way with this from the start 🤦‍♂️

Anyway, submitted a PR for this: #1995

@kinnison
Copy link
Contributor

If anyone here can confirm the work on #1995 then I'd appreciate it.

burjui added a commit to burjui/rustup.rs that referenced this issue Sep 16, 2019
kinnison added a commit that referenced this issue Sep 17, 2019
Fix generation of Cargo completions for ZSH (issue #1821)
@rschmitt
Copy link

LGTM, but the autocompletion for Cargo only appears to load if I run compinit -D, whereas all my other autocompletions (including for rustup) load with compinit.

@murlakatamenka
Copy link

murlakatamenka commented Oct 9, 2019

So, how does one get cargo completion working at this point?

Via adding

#compdef cargo
source $(rustc --print sysroot)/share/zsh/site-functions/_cargo

to your .zshrc?

@burjui
Copy link
Contributor

burjui commented Oct 9, 2019

@murlakatamenka No, you just add the #compdef cargo line at the beginning of the completion file generated by rustup:

echo `#compdef cargo` > ~/.zfunc/_cargo
rustup completions zsh cargo >> ~/.zfunc/_cargo

If you haven't already, you should put this line in your ~/.zshrc before the compinit call:

fpath+=~/.zfunc

Also remove the compinit dumped configuration files so that next call to compinit discovers the new Cargo completions (may take a few seconds):

rm ~/.zcompdump

It will dump the new configuration into ~/.zcompdump and after that compinit calls will be fast again.

@burjui
Copy link
Contributor

burjui commented Oct 9, 2019

Just to be clear, I am using oh my zsh, so in my case things are a little bit different:

echo `#compdef cargo` > ~/.oh-my-zsh/completions/_cargo
rustup completions zsh cargo >> ~/.oh-my-zsh/completions/_cargo

My ~/.zshrc:

export ZSH=$HOME/.oh-my-zsh
ZSH_THEME="juanghurtado"
source $ZSH/oh-my-zsh.sh
autoload -U compinit && compinit
...

@murlakatamenka
Copy link

@burjui thanks, it worked! 👍

@murlakatamenka
Copy link

This is already fixed in rustup 1.20+, the issue can be closed, right?

@burjui btw oh-my-zsh has its own cargo plugin but it's not up-to-date.

I've stopped on this 👇

rustup completions zsh cargo > ~/.zsh/completions/_cargo

.zshrc (oh-my-zsh is installd via a package manager in my case):

export ZSH=/usr/share/oh-my-zsh
plugins=(...)
fpath+=~/.zsh/completions
source $ZSH/oh-my-zsh.sh
autoload -U compinit && compinit

@kinnison
Copy link
Contributor

Thank you @murlakatamenka for your input. I'll close this then.

@drahnr
Copy link

drahnr commented Mar 11, 2021

To make this a oneliner, use source <(rustup completions zsh cargo) in your .zshrc i.e.

@murlakatamenka
Copy link

@drahnr thanks for the tip.

Actually rustup package on Arch takes care of that, completion scripts are generated by rustup binary:

https://github.com/archlinux/svntogit-community/blob/a57fda98a448bc39cf2c4f5ea6023fc747855da9/trunk/PKGBUILD#L42-L43

@xxidbr9
Copy link

xxidbr9 commented Oct 2, 2022

if you using oh-my-zhs add rust in plugins inside .zshrc

https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/rust

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

No branches or pull requests

8 participants