Rails

You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).

2020/12/16

この記事の対象者

  • Railsアプリケーションで発生している
  • Sidekiq (Redis)を利用している
  • Railsコマンドを実行した際に, 以下のエラーが出ている
You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).

This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system.
To solve this, upgrade your bundle to the latest Spring version and then run `bundle exec spring binstub --all` to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.

解決策

  1. rails, redis, sidekiqを全て落とす
  2. railsアプリケーションのルートディレクトリに移動
  3. bin/spring status でstatusを確認
  4. Sprint is running: の場合, bin/spring stop を実行
  5. rm -rf bin でRailsアプリケーションのbinディレクトリを削除
  6. bundle exec rake app:update:bin を実行

これでrailsコマンドが正常に動くようになる.

  1. 上記を実行した後にrailsコマンドを実行してエラーが発生する場合は, モジュールを再インストールする.

※僕はvendor/bundleにインストールしているため, 下記のコマンドになる

$ rm -rf vendor/bundle
$ bundle install --path vendor/bundle

LoadError: cannot load such file -- bootsnap/setup

以下のようなエラーが出た場合は環境をきれいにしてから, 再度解決策を実行する.

$ bundle exec rake app:update:bin
rake aborted!
LoadError: cannot load such file -- bootsnap/setup
  1. を実行した際に, Using bootsnap のような表示が出る場合は, Railsアプリケーションにbootsnapが入っていない可能性が高い.

僕の場合はrbenvでRubyのバージョンを管理していたため, 利用しているバージョンを再インストールしてから, 1 ~ 7まで実行する.

$ rbenv uninstall 2.6.5
$ rbenv install 2.6.5

Twitterフォロー待ってます!