RailsアプリのHerokuデプロイでハマった

Rails アプリの Heroku デプロイでハマったので解決法を残しておく。

## TL;DR
Bundler 2.0.1 を入れ直し、Gemfile.lock を再生成し、commit & push heroku。
Gemfile.lock に `BUNDLED WITH 2.0.2` と書かれているとダメっぽい。


## 環境
- macOS Mojave 10.14.5
- Ruby 2.6.3
- Bundler 2.0.2
- Rails 5.1.6


## 問題
`$ git push heroku master`

で、以下のエラーが出てデプロイ失敗。

```terminal
 !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
			Detected buildpacks: Ruby,Node.js
			See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Ruby app detected
-----> Compiling Ruby/Rails
 !
 !     There was an error parsing your Gemfile, we cannot continue
 !     /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/vendor/ruby/heroku-18/lib/ruby/2.5.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
 !     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/vendor/ruby/heroku-18/lib/ruby/2.5.0/rubygems.rb:308:in `activate_bin_path'
 !     from /tmp/d20190615-117-1464bpn/bundler-2.0.1/bin/bundle:23:in `<main>'
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed
```

Bundler に問題がありそうなので、Heroku の Ruby Buildpack での Bundler の扱いを調べた。

[Heroku Ruby Support | Heroku Dev Center](https://devcenter.heroku.com/articles/ruby-support#libraries)

>The following libraries are used by the platform for managing and running Ruby applications and cannot be specified. For application dependency resolution and management, bundler is installed based on the contents of your `Gemfile.lock`. If you have a `BUNDLED WITH` in your `Gemfile.lock` then you will receive a different version of Bundler:
>
>- Applications specifying Bundler 2.x in their `Gemfile.lock` will receive bundler: 2.0.1
>- Applications specifying Bundler 1.x in their `Gemfile.lock` will receive bundler: 1.15.2
>- Applications with no `BUNDLED WITH` in their `Gemfile.lock` will default to bundler: 1.15.2

どうやら、Gemfile.lock 内の `BUNDLED WITH` に書かれた Bundler のバージョンによって Heroku で使われる Bundler のバージョンが決まるらしい。

ローカルの Bundler のバージョンは 2.0.2。

```terminal
$ bundle -v
Bundler version 2.0.2
```

そして Gemfile.lock を見ると末尾に以下の記述がある。

```:Gemfile.lock
BUNDLED WITH
   2.0.2
```

この場合、`2.x` の指定なので、Heroku では Bundler 2.0.1 が使われて特に問題無さそうだが、2.0.1 と 2.0.2 の違いがデプロイ失敗の原因かも知れない。


## 解決
Bundler 2.0.1 に入れ替え、Gemfile.lock を再生成してみる。

```terminal
$ gem uninstall bundler

Remove executables:
	bundler

in addition to the gem? [Yn]  Y
Removing bundler
Successfully uninstalled bundler-2.0.2
```

```terminal
$ gem install bundler -v 2.0.1
$ bundle -v  # Bundler version 2.0.1
$ cd [project directory]
$ rm Gemfile.lock
$ bundle --without production
```

差分は `BUNDLED WITH` の部分だけ。

```terminal
$ git diff

diff --git a/Gemfile.lock b/Gemfile.lock
index c2077c0..e3287c7 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -180,4 +180,4 @@ DEPENDENCIES
   web-console (= 3.5.1)

 BUNDLED WITH
-   2.0.2
+   2.0.1
```

```terminal
$ git commit -am 'Change Bundler ver. (2.0.2 -> 2.0.1)'
$ git push heroku master
```

成功。