Heroku 部署 - 无法加载此类文件 -- rake (LoadError)

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

我收到错误:

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>'

当 运行 来自 Codeship 的 Heroku 部署管道时

/bin/rake 看起来像这样

#!/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

所以第 8 行是必需的 'rake'

我尝试将 gem rake 显式添加到我的 gem 文件

然后因为提到生产组我试过了

group :production do
  gem 'rake'
end

None 有效,因此我的代码推送被 Heroku 拒绝

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

任何人有什么想法吗?我在 Whosebug rails cannot load such file -- rake (LoadError) 上看到类似的 post 但没有解决方案?

[我编辑了这个原始回复以包含一个解决方案]

我也有这个确切的问题,有一张 Heroku 的公开票。 还没有回复,很遗憾,但我在 Google.

中幸运地找到了答案

上下文,供将来参考和搜索引擎使用

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>'

正如您无疑已经发现的那样,上面建议的 bundle exec rake -P 命令适用于本地的任何 Rails.env 设置。我也尝试将 rake 显式添加到 Gemfile 但这没有任何区别,预编译资产也没有。其他注意事项:

  • 所有宝石构建成功
  • 此部署会提示并从 Heroku-18 更新到 Heroku-20 平台;你的吗?
  • Rails 5 个最新补丁,但还没有 Rails 6 个
  • Ruby 2.7.2

我不使用 Spring 所以我的 bin/rake 更简单:

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

...对于更复杂的代码,肯定是 require 'rake' 失败了。

解决方案

我不能相信 - 就是这个答案:

...解决了,只需要从 2.1.2 更改为 2.1.4。复制粘贴上面的解决方案 - 同样这不是我的工作,功劳属于上面的 OP:

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

这已经解决了我的问题。似乎是一个非常奇怪的解决方案,但你去吧。

如果你不觉得无法降级Bundler

在对此答案的评论中,OP 指出:

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

Whosebug 更喜欢内联答案而不是外部链接,因为外部链接可能会中断,因此再次指出,感谢上面链接的文章:

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

...所以希望如果您将其保存为 .rb 文件方便的地方,chmod u+x foo.rb./foo.rb - 这可能会解决问题。

我刚遇到这个问题,Heroku 似乎在 official documentation 中添加了一个对我有用的部分:

Bundler 2.2.3+

An app’s Gemfile.lock that is generated with Bundler 2.2.3 locally may not work on Heroku unless the Linux platform is explicitly “locked”:

$ bundle lock --add-platform ruby
$ bundle lock --add-platform x86_64-linux
$ bundle install
$ git add . ; git commit -m "Bundler fix"