Heroku installs Bundler then throws error Bundler 2.0.1

I fixed our builds on Heroku by upgrading to Ruby 2.6.3 and Bundler 2.0.2.

Our builds on Heroku started breaking in the last couple of days with a similar error message:

Could not find 'bundler' (2.0.1) required by your ... Gemfile.lock.

We were on Ruby 2.6.1 and Bundler 2.0.1 on our local builds which worked fine. This combo worked fine on Heroku up until a couple of days ago, too.

While sleuthing around the Heroku docs, I noticed this Dev Center article, which was updated on 26 June, 2019. It states:

Bundler 2.0.2 See issues for 2.0.1.

Many people are hitting issue 4 which is only triggered when a new Bundler version is released. It happens because of a bug in Rubygems, and can be fixed by upgrading to 2.5.5+ or 2.6.3+.

Issue 4 under 2.0.1 states:

[You may get an error] due to a bug in the Rubygems bundler version checking code. To avoid this issue, upgrade your Ruby version. It is fixed in 2.5.5+ and 2.6.3+. If you do not update, your Ruby version then every new release of Bundler 2.x will trigger this issue.

Bundler 2.0.2 was released 13 June, 2019 and it looks like Heroku has started using it.

I notice you are using Ruby 2.7 from your logs. I would suggest specifying ruby '2.6.3' in your Gemfile, installing Bundler 2.0.2, and bundling/generating your Gemfile.lock with that combination. You should then see this at the bottom of your lock file and hopefully your app builds successfully on Heroku:

RUBY VERSION
   ruby 2.6.3p62

BUNDLED WITH
   2.0.2

Most likely, all you need to run to fix this is

gem install bundler -v 2.0.2
bundle update --bundler

Commit those changes that it made in your Gemfile.lock, and then your next Heroku push will go through without a problem.