LoginSignup
2
1

VSCodeのRuby LSPが正常に動作しないときの対処法

Posted at

概要

VSCodeのRuby LSPの拡張機能をインストールした際に繰り返しエラーが発生しました
安定して動作するように修正する方法がつかめたので共有です

環境

  • WSLのUbuntu 20.04
  • rbenv
  • Ruby

環境準備

WSLのUbuntu 20.04にrbenvを入れる

$ sudo apt update && sudo apt upgrade -y
$ sudo apt install -y gcc g++ make git libssl-dev libreadline-dev zlib1g-dev
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ ~/.rbenv/bin/rbenv init
# 1度入りなおす
$ which rbenv
/home/username/.rbenv/bin/rbenv
$ rbenv --version
rbenv 1.2.0-91-gc3ba994

Ruby 3.1.6をインストール(バージョンは適当です)

$ rbenv install 3.1.6

適当なディレクトリを作成する

$ cd ~
$ mkdir ruby-app

作成したディレクトリをVSCodeで開く

事象再現

ワークスペース内のRubyのバージョンを先程インストールしたバージョンに設定

$ rbenv local 3.1.6
$ ls -a
.  ..  .ruby-version
$ ruby --version
ruby 3.1.6p260 (2024-05-29 revision a777087be6) [x86_64-linux]

Ruby LSPの拡張機能を入れる

適当なrbファイルを作成する

sample.rb
num = "Hello"
puts num

Gemfileを作成する

source 'https://rubygems.org'

gem 'ruby-lsp'
gem 'rubocop'

Gemのインストール先を設定

$ bundle config path "vendor/bundle"
$ mkdir -p vendor/cache
$ tree -a
.
├── .bundle
│   └── config
├── .ruby-version
├── Gemfile
├── sample.rb
└── vendor
    └── cache

gemをインストール

$ bundle install
$ tree -a -L 2
.
├── .bundle
│   └── config
├── .ruby-version
├── Gemfile
├── Gemfile.lock
├── sample.rb
└── vendor
    ├── bundle
    └── cache

ここでVSCodeの接続を切って開き直すと以下のようなエラーメッセージが繰り返し表示されるようになる

image.png

原因調査

「Go to output」をクリックしてログを見ると大量のエラーが・・・
その中で気になったのは以下のログ

2024-06-28 00:17:50.992 [info] (ruby-app) bundler: failed to load command: ruby-lsp (/home/username/.rbenv/versions/3.1.6/bin/ruby-lsp)

2024-06-28 00:17:50.992 [info] (ruby-app) /home/username/.rbenv/versions/3.1.6/lib/ruby/3.1.0/bundler/resolver.rb:235:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'debug mri' in locally installed gems. (Bundler::GemNotFound)

(一部省略)

2024-06-28 00:18:07.172 [info] (ruby-app) [Error - 12:18:07 AM] Server initialization failed.
2024-06-28 00:18:07.172 [info] (ruby-app)   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-06-28 00:18:07.172 [info] (ruby-app) [Info  - 12:18:07 AM] Connection to server got closed. Server will restart.
2024-06-28 00:18:07.172 [info] (ruby-app) true
2024-06-28 00:18:07.173 [info] (ruby-app) [Error - 12:18:07 AM] Ruby LSP client: couldn't create connection to server.
2024-06-28 00:18:07.173 [info] (ruby-app)   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-06-28 00:18:07.175 [info] (ruby-app) [Error - 12:18:07 AM] Server process exited with code 1.
2024-06-28 00:18:07.333 [info] (ruby-app) Ruby LSP> Skipping custom bundle setup since /home/username/ruby-app/.ruby-lsp/Gemfile.lock already exists and is up to date

2024-06-28 00:18:07.334 [info] (ruby-app) Ruby LSP> Running bundle install for the custom bundle. This may take a while...
Ruby LSP> Command: (bundle check || bundle install) 1>&2

2024-06-28 00:18:07.462 [info] (ruby-app) Bundler can't satisfy your Gemfile's dependencies.
Install missing gems with `bundle install`.

これを見るとruby-lspや関連するgemをユーザーのホームディレクトリから参照しようとしていそう.
そこでワークスペースの設定で何かできないか探した.

対応

VSCodeの設定を見てみると以下のような項目があり,ワークスペースで参照するGemfileのパスを設定することができそう.

image.png

設定を試みる.

image.png

これでワークスペースを開き直すと・・・

2024-06-28 00:34:20.357 [info] (ruby-app) Checking if chruby is available on the path with command: /bin/bash -i -c 'chruby --version'
2024-06-28 00:34:20.752 [info] (ruby-app) Checking if rbenv is available on the path with command: /bin/bash -i -c 'rbenv --version'
2024-06-28 00:34:21.112 [info] (ruby-app) Discovered version manager rbenv
2024-06-28 00:34:21.112 [info] (ruby-app) Running command: `rbenv exec ruby -W0 -rjson -e 'STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)'` in /home/username/ruby-app using shell: /bin/bash
2024-06-28 00:34:22.662 [info] (ruby-app) Initializing Ruby LSP v0.17.4...

2024-06-28 00:34:22.719 [info] (ruby-app) Finished initializing Ruby LSP!

動いた!
Rubyファイルは・・・

image.png

rubocopメッセージが表示されている!

image.png

変更もOK!

まとめ

Ruby LSPのgemの設定をワークスペースで行いたい場合は「Ruby Lsp: Bundle Gemfile」にワークスペースのGemfileのpathを設定すれば動作してくれそう.
また,接続先のマシン全体で設定したい場合は,ワークスペースではなくマシン側の設定を変えてあげれば良さそうです.
なお,Ruby LSPの拡張機能のソースはちゃんと読めてないのでGemfile読み込みのロジックは未調査ですm(_ _)m

余談

num = "hello"は変でしたねw

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1