Search code examples
ruby-on-railsherokudeploymentrake

Heroku deployment - cannot load such file -- rake (LoadError)


I get the error:

remote:  !     Could not detect rake tasks
 
04:36
 
remote:  !     ensure you can run `$ bundle exec rake -P` against your app
 
04:36
 
remote:  !     and using the production group of your Gemfile.
 
04:36
 
remote:  !     /tmp/build_aa020f9e/bin/rake:8:in `require': cannot load such file -- rake (LoadError)
 
04:36
 
remote:  !     from /tmp/build_aa020f9e/bin/rake:8:in `<main>'

when running a Heroku deployment pipeline from Codeship

/bin/rake looks like this

#!/usr/bin/env ruby
begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end
require_relative '../config/boot'
require 'rake'
Rake.application.run

so line 8 is require 'rake'

I tried adding gem rake explicitly to my gemfile

Then because production group is mentioned I tried

group :production do
  gem 'rake'
end

None of this works and so my code push is rejected by Heroku

remote:  !     Push rejected, failed to compile Ruby app.
 
04:36
 
remote: 
 
04:36
 
remote:  !     Push failed

Any ideas anyone?I see a similar post on StackOverflow rails cannot load such file -- rake (LoadError) but without a solution?


Solution

  • [I've edited this original response to include a solution]

    I have this exact issue too, with an open ticket with Heroku. No response yet, sadly but I found an answer elsewhere via a lucky hit in Google.

    Context, for future reference and search engines

    remote: -----> Detecting rake tasks
    remote: 
    remote:  !
    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:  !     /tmp/build_b8f358ab/bin/rake:3:in `require': cannot load such file -- rake (LoadError)
    remote:  !     from /tmp/build_b8f358ab/bin/rake:3:in `<main>'
    remote:  !
    remote: /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/helpers/rake_runner.rb:106:in `load_rake_tasks!': Could not detect rake tasks (LanguagePack::Helpers::RakeRunner::CannotLoadRakefileError)
    remote: ensure you can run `$ bundle exec rake -P` against your app
    remote: and using the production group of your Gemfile.
    remote: /tmp/build_b8f358ab/bin/rake:3:in `require': cannot load such file -- rake (LoadError)
    remote:     from /tmp/build_b8f358ab/bin/rake:3:in `<main>'
    

    As you've doubtless already found, the suggested bundle exec rake -P command above works fine with any Rails.env setting locally. I, too, tried adding rake to Gemfile explicitly but this made no difference, nor did precompiling assets. Other things to note:

    • All gems build successfully
    • This deploy would prompt and update from Heroku-18 to the Heroku-20 platform; does yours?
    • Rails 5 latest patch, but not Rails 6 yet
    • Ruby 2.7.2

    I don't use Spring so my bin/rake is simpler:

    #!/usr/bin/env ruby
    require_relative '../config/boot'
    require 'rake'
    Rake.application.run
    

    ...and as with your more complex code, it's definitely the require 'rake' that fails.

    Solution

    I can't take credit - it's this answer:

    ...that solved it, only you need to change from 2.1.2 to 2.1.4. Copy-pasting the above solution - again this is not my work, the credit belongs to the OP above:

    gem uninstall bundler
    gem install bundler --version '2.1.4' # If this doesn't work - see below
    
    rm Gemfile.lock
    bundle install
    
    git add Gemfile.lock
    git commit -m "Downgrade Bundler to match current Heroku version, so that deployments succeed"
    git push heroku
    

    This has solved the issue in my case. Seems like an utterly bizarre solution, but there you go.

    If you don't seem to be unable to downgrade Bundler

    In the comments on this answer, the OP notes:

    Because Bundler version 2.2.10 was installed for me as the "default" I hadnt realised Gem bundler-2.1.4 cannot be uninstalled via gem uninstall bundler Using the cleanup_bundler script documented here enabled me to downgrade to v2.1.4 properly. This then did fix the rake problem.

    $ gem uninstall bundler
    => Gem bundler-2.2.10 cannot be uninstalled because it is a default gem
    

    StackOverflow prefer answers to be inline rather than linked externally as external links can break, so once again noting that credit goes to the article linked above:

    From all the references I can find, the only way to remove it is to delete the bundler-2.1.4.gemspec file from the gem path. The following commands did the trick for me [...] I wrote a script for you:

    #!/usr/bin/env ruby
    
    gempaths = `gem env gempath`.split(":")
    gempaths.each do |gempath|
      # lookup bundler-*.gemspec files and delete them
      # this is the only way to completely cleanup default bundler
      # Note: the bundler gemspecs' paths are different for CRuby and JRuby
      Dir.glob(gempath.strip + "/specifications/**/bundler-*.gemspec").each { |p| File.delete(p) }
    end
    
    # Remember to make this file executable
    

    ...so hopefully if you save that as a .rb file somewhere convenient, chmod u+x foo.rb and ./foo.rb - that might solve the problem.