ZedでRailsのコードジャンプを復旧させる

Zedのコードジャンプ(Go to code definition)があるときから使えなくなったので、メモ。

以下のようなエラーを吐いてコードジャンプができない。地味にストレスだった。

solargraphが起動しないのが原因。

Language server solargraph:
initializing server solargraph, id 2: Server reset the connection
-- stderr --
bundler: failed to load command: solargraph (/Users/user/.rbenv/versions/3.4.2/bin/solargraph)
/Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/rubygems_integration.rb:253:in 'block in Gem.replace_bin_path': can't find executable solargraph for gem solargraph. solargraph is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/rubygems.rb:281:in 'Gem.activate_bin_path'
    from /Users/user/.rbenv/versions/3.4.2/bin/solargraph:25:in '<top (required)>'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli/exec.rb:59:in 'Kernel.load'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli/exec.rb:59:in 'Bundler::CLI::Exec#kernel_load'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli/exec.rb:23:in 'Bundler::CLI::Exec#run'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli.rb:452:in 'Bundler::CLI#exec'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/command.rb:28:in 'Bundler::Thor::Command#run'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in 'Bundler::Thor::Invocation#invoke_command'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor.rb:538:in 'Bundler::Thor.dispatch'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli.rb:35:in 'Bundler::CLI.dispatch'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/vendor/thor/lib/thor/base.rb:584:in 'Bundler::Thor::Base::ClassMethods#start'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/cli.rb:29:in 'Bundler::CLI.start'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/bundler-2.6.2/exe/bundle:28:in 'block in <top (required)>'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/3.4.0/bundler/friendly_errors.rb:117:in 'Bundler.with_friendly_errors'
    from /Users/user/.rbenv/versions/3.4.2/lib/ruby/gems/3.4.0/gems/bundler-2.6.2/exe/bundle:20:in '<top (required)>'
    from /Users/user/.rbenv/versions/3.4.2/bin/bundle:25:in 'Kernel#load'
    from /Users/user/.rbenv/versions/3.4.2/bin/bundle:25:in '<main>'

自分の場合、プロジェクトを複数運用しているので、solargraphはグローバルのものを使いたい。

solargraph単体で起動してみると、Rubyのどのバージョンのものを使うか聞かれた。

$ solargraph --version
rbenv: solargraph: command not found
The `solargraph' command exists in these Ruby versions:
  3.1.2
  3.2.4
  3.4.2

まずはここを治す。ということで3.4.2をデフォルトにする。

rbenv global 3.4.2
rbenv rehash

これだけではだめ。あとは、Zedのsettings.jsonに以下を記載する。bundlerでリポジトリの設定を見にいくのではなく、自分のグローバルを最初に参照するようにする。

},
"lsp": {
  "solargraph": {
    "settings": {
      "use_bundler": false
    }
  }
}

あとは、Zedを再起動すればOK。これで無事コードジャンプが使えるようになった。

みんなでsolargraphを使っているなら、Gemfileでインストールするようにして、リポジトリ内のsolargraphを見にいくようにするのも良いと思う。

バージョンも固定できるしね。