Zsh, coc.vim, coc-solargraph and rbenv

2020/04/04

Categories: it Tags: zsh vim coc.vim solargraph

The problem

[coc.nvim]Failed to start Solargraph:
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:283:in
`find_spec_for_exe': Could not find 'bundler' (2.1.4) required by your %path%/Gemfile.lock.
(Gem::GemNotFoundException)

Yes, this is how the problem looks.

You decided to work in VIM. And ruby is on your list of langauges you are writing in. Most likely that you will end up using a great tool called coc.vim - an intellisense engine from VIM and Neovim.

For coc.vim to be of use to you - it has to rely on an external langauge server.

As per coc.vim’s docs your best bet is coc-solargraph which is a ruby langauge server [extension] which uses Solargraph under the hood.

The installation is straightforward but if you are using zsh and decide to use solargraph.useBundler option with you have good chances of seeing the error above.

What’s happening?

Well turns out the extension runs commands in a non-interactive login shell.

This means your .zshrc file is not loaded and whatever you’ve put there too. For example you rbenv that requires to be sourced in .zshrc by default.

As the result - rbenv is never loaded and coc-solargraph can’t find bundler because it looks for it you system’s default ruby path instead of using the one provided by rbenv.

The solution!

Well, if .zshrc is not used at this time you will have to put your rbenv string somewhere else.

And this ‘somewhere’ is your .zprofile1.

First use rbenv-doctor to check if your rbenv installation is ok. You will also be prompted your current rbenv PATH:

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

Result will look something like this:

Checking for `rbenv' in PATH: /home/user/.rbenv/bin
...
...
...

Just add those lines2 to this file (create one if it does not exist):

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

  1. More info on Zsh startup files: http://zsh.sourceforge.net/Intro/intro_3.html ↩︎

  2. Note that you don’t need the first line if your rbenv installation comes from Homebrew on macOS. In this case rbenv is linked to /usr/local/bin/rbenv and will be in the PATH anyway. ↩︎

>> Home