Heroku部署-不能加载这样的文件——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行是require 'rake'

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

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

group :production do
gem 'rake'
end

这些都不起作用,所以我的代码推送被Heroku拒绝了

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

04:36

remote: 

04:36

remote:  !     Push failed

有什么想法吗?我看到一个类似的帖子StackOverflow rails不能加载这样的文件- rake (LoadError),但没有解决方案?

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

我也有这个确切的问题,与Heroku的开放式机票。还没有回应,很遗憾但我在谷歌上幸运地找到了一个答案。

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

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,但这没有区别,预编译资产也没有。其他注意事项:

  • 所有gem构建成功
  • 这个部署将提示并从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'失败了。

解决方案这不是我的功劳——是这个答案:

  • https://stackoverflow.com/a/65604896

…解决了这个问题,只需要从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注意到:

因为为我安装的Bundler版本2.2.10是"默认的";我没有意识到Gem bundler-2.1.4不能通过Gem uninstall bundler卸载。使用这里记录的cleanup_bundler脚本使我能够正确降级到v2.1.4。这就解决了rake问题。

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

StackOverflow更喜欢内联而不是外部链接的答案,因为外部链接可能会断开,所以再次注意,功劳归于上面链接的文章:

从我能找到的所有引用中,删除它的唯一方法是从gem路径中删除bundler-2.1.4.gemspec文件。下面的命令帮了我的忙[…]我为你写了一个脚本:

#!/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似乎在官方文档中添加了一节为我工作:

打包机2.2.3 +

应用的Gemfile。由Bundler 2.2.3本地生成的lock可能无法在Heroku上工作,除非Linux平台显式地"锁定":

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