Problem
cargo tab completion is not working. I installed it the same way I did rustup, which works.
Steps
rustup completions zsh cargo > /usr/local/share/zsh-completions/_cargo.zshrc I havefpath=(/usr/local/share/zsh-completions $fpath)
compinit
.zshrcNotes
Output of rustup --version: rustup 1.18.1 (462683b3a 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)
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.
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.
@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
If anyone here can confirm the work on #1995 then I'd appreciate it.
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.
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?
@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.
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
...
@burjui thanks, it worked! :+1:
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 :point_down:
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
Thank you @murlakatamenka for your input. I'll close this then.
To make this a oneliner, use source <(rustup completions zsh cargo) in your .zshrc i.e.
@drahnr thanks for the tip.
Actually rustup package on Arch takes care of that, completion scripts are generated by rustup binary:
Most helpful comment
@murlakatamenka No, you just add the
#compdef cargoline at the beginning of the completion file generated by rustup:If you haven't already, you should put this line in your
~/.zshrcbefore thecompinitcall:Also remove the
compinitdumped configuration files so that next call tocompinitdiscovers the new Cargo completions (may take a few seconds):It will dump the new configuration into
~/.zcompdumpand after thatcompinitcalls will be fast again.