Herokuへpushするときに「Failed to install gems via Bundler.」と「NoMethodError: undefined method `factory_bot’ for #<Rails::Application::Configuration:0x0000556cf580a310>」が出た
まさかこんなにハマるとは思いませんでした...。
備忘録としてハマったエラーの対処法をまとめます。
流れと結論
Herokuへpushする際に2つのエラーに遭遇しました。
1.「Failed to install gems via Bundler.」
→ ローカルのbundlerのバージョンをherokuと一致させる
→ Gemfile.lockにplatform の追加
2.「NoMethodError: undefined method `factory_bot’ for #
→ config/application.rb の config.factory_bot.definition_file_paths = [“spec/factories”] を削除
これによって無事Herokuにpushできました。
1つ目のエラー解決方法
$git push heroku main を実行したところ以下のようなエラーが発生。
Enumerating objects: 839, done. Counting objects: 100% (839/839), done. Delta compression using up to 8 threads Compressing objects: 100% (780/780), done. Writing objects: 100% (839/839), 405.51 KiB | 7.37 MiB/s, done. Total 839 (delta 438), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-20 stack remote: -----> Determining which buildpack to use for this app remote: ! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used. remote: Detected buildpacks: Ruby,Node.j remote: See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order remote: -----> Ruby app detected remote: -----> Installing bundler 2.2.11 remote: -----> Removing BUNDLED WITH version in the Gemfile.lock remote: -----> Compiling Ruby/Rails remote: -----> Using Ruby version: ruby-2.7.2 remote: -----> Installing dependencies using bundler 2.2.11 remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4 remote: Fetching gem metadata from https://rubygems.org/............ remote: Your bundle is locked to msgpack (1.4.1), but that version could not be found in remote: any of the sources listed in your Gemfile. If you haven't changed sources, that remote: means the author of msgpack (1.4.1) has removed it. You'll need to update your remote: bundle to a version other than msgpack (1.4.1) that hasn't been removed in order remote: to install. remote: Bundler Output: Fetching gem metadata from https://rubygems.org/............ remote: Your bundle is locked to msgpack (1.4.1), but that version could not be found in remote: any of the sources listed in your Gemfile. If you haven't changed sources, that remote: means the author of msgpack (1.4.1) has removed it. You'll need to update your remote: bundle to a version other than msgpack (1.4.1) that hasn't been removed in order remote: to install. remote: remote: ! remote: ! Failed to install gems via Bundler. remote: ! remote: ! Push rejected, failed to compile Ruby app. remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to score-log. remote: To https://git.heroku.com/score-log.git ! [remote rejected] HEAD -> main (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/score-log.git'
Herokuでのbundlerが2.2.11となっているがローカルのbundlerは2.1.4となっていました。
ローカルのbundlerのバージョンを2.2.11とするために以下の記事に記載されていることをそのまま実行しました。
これでエラー解決と思いきや、またも同じ「Failed to install gems via Bundler.」のエラー。
いろいろ調べてみると、一部の Mac では Gemfile.lock の PLATFORM が不足しているためにHerokuへのpush時にエラーが出るとのこと。
$ bundle lock --add-platform ruby $ bundle lock --add-platform x86_64-linux $ bundle install
ここに関してはあまり理解できていません......
2つ目のエラー解決方法
これでやっとHerokuへpushできると思ったのですが、またエラーに遭遇しました。
remote: ! Could not detect rake tasks remote: ! ensure you can run `$ bundle exec rake -P` against your app remote: ! and using the production group of your Gemfile. remote: ! rake aborted! remote: ! NoMethodError: undefined method `factory_bot' for #<Rails::Application::Configuration:0x0000559ccfd5f860>
factory_botが未定義と出ています。
Gemfileやfactory_bot周辺を調べましたが、原因が分からずメンターさんに相談しました。
config/application.rbに
config.factory_bot.definition_file_paths = [“spec/factories”]
が追加されていることが原因ではないかとのこと。
以前factory_bot導入時のエラー対処で上記をconfig/application.rbに追加していました。
gem 'factory_bot_rails' をテスト環境にのみ入れているにも関わらず、config/appcliation.rbに config.factory_bot.definition_file_paths = [“spec/factories”] を追加したことで本番環境にもfactory_botを入ってしまっていたようです。
そのためpushする際にfactory_botが未定義となっていました。
上記の記述を削除することで無事Herokuへpushすることができました。
config.factory_bot.definition_file_paths = [“spec/factories”]
私の場合はこの記述は不要でしたが、必要であれば
・config/environments/development.rb
・config/environments/test.rb
の2箇所に追加すればいいようです。