Heroku deplpy 失败并显示“..bin/bundle: line 11: require: command not found... Failed to install gems via

Posted

技术标签:

【中文标题】Heroku deplpy 失败并显示“..bin/bundle: line 11: require: command not found... Failed to install gems via Bundler.”【英文标题】:Heroku deplpy fails with '..bin/bundle: line 11: require: command not found... Failed to install gems via Bundler.' 【发布时间】:2021-12-12 08:47:17 【问题描述】:

我试图将我的 Rails api-only 应用程序部署到 Heroku,当我运行 git push heroku main 时,我收到了这个错误:

Counting objects: 100% (1198/1198), done.
Delta compression using up to 8 threads
Compressing objects: 100% (417/417), done.
Writing objects: 100% (1198/1198), 171.98 KiB | 21.50 MiB/s, done.
Total 1198 (delta 715), reused 1188 (delta 711), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpacks:
remote:        1. heroku/ruby
remote:        2. https://github.com/heroku/heroku-buildpack-ruby.git
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.2.21
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.2
remote: -----> Installing dependencies using bundler 2.2.21
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        /tmp/build_814ed8d0/bin/bundle: line 11: require: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 13: m: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 14: module_function: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 16: def: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 17: syntax error near unexpected token `$PROGRAM_NAME'
remote:        /tmp/build_814ed8d0/bin/bundle: line 17: `    File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)'
remote:        Bundler Output: /tmp/build_814ed8d0/bin/bundle: line 11: require: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 13: m: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 14: module_function: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 16: def: command not found
remote:        /tmp/build_814ed8d0/bin/bundle: line 17: syntax error near unexpected token `$PROGRAM_NAME'
remote:        /tmp/build_814ed8d0/bin/bundle: line 17: `    File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)'
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to murmuring-forest-17162.
remote: 
To https://git.heroku.com/murmuring-forest-17162.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/murmuring-forest-17162.git'

我的本​​地文件看起来像 bin/bundle

# frozen_string_literal: true

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'bundle' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

m = Module.new do
  module_function

  def invoked_as_script?
    File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)
  end

  def env_var_version
    ENV['BUNDLER_VERSION']
  end

  def cli_arg_version # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity
    return unless invoked_as_script? # don't want to hijack other binstubs
    return unless 'update'.start_with?(ARGV.first || ' ') # must be running `bundle update`

    bundler_version = nil
    update_index = nil
    ARGV.each_with_index do |a, i|
      bundler_version = a if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
      next unless a =~ /\A--bundler(?:[= ](#Gem::Version::VERSION_PATTERN))?\z/

      bundler_version = Regexp.last_match(1)
      update_index = i
    end
    bundler_version
  end

  def gemfile
    gemfile = ENV['BUNDLE_GEMFILE']
    return gemfile if gemfile && !gemfile.empty?

    File.expand_path('../Gemfile', __dir__)
  end

  def lockfile
    lockfile =
      case File.basename(gemfile)
      when 'gems.rb' then gemfile.sub(/\.rb$/, gemfile)
      else "#gemfile.lock"
      end
    File.expand_path(lockfile)
  end

  def lockfile_version
    return unless File.file?(lockfile)

    lockfile_contents = File.read(lockfile)
    return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s2,(#Gem::Version::VERSION_PATTERN)\n/

    Regexp.last_match(1)
  end

  def bundler_version
    @bundler_version ||=
      env_var_version || cli_arg_version ||
      lockfile_version
  end

  def bundler_requirement
    return "#Gem::Requirement.default.a" unless bundler_version

    bundler_gem_version = Gem::Version.new(bundler_version)

    requirement = bundler_gem_version.approximate_recommendation

    return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new('2.7.0')

    requirement += '.a' if bundler_gem_version.prerelease?

    requirement
  end

  def load_bundler!
    ENV['BUNDLE_GEMFILE'] ||= gemfile

    activate_bundler
  end

  def activate_bundler # rubocop:disable Metrics/MethodLength
    gem_error = activation_error_handling do
      gem 'bundler', bundler_requirement
    end
    return if gem_error.nil?

    require_error = activation_error_handling do
      require 'bundler/version'
    end
    if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
      return
    end

    warn "Activating bundler (#bundler_requirement) failed:\n#gem_error.message\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#bundler_requirement'`" # rubocop:disable Layout/LineLength
    exit 42
  end

  def activation_error_handling
    yield
    nil
  rescue StandardError, LoadError => e
    e
  end
end

m.load_bundler!

load Gem.bin_path('bundler', 'bundle') if m.invoked_as_script?

我尝试将我的 ruby​​ 和 bundler- 版本更改为与 Heroku 使用的相同,但结果没有改变:

bundler -v
Bundler version 2.2.21

ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin20]

rbenv -v
rbenv 1.1.2

我的Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github)  |repo| "https://github.com/#repo.git" 

ruby '3.0.2'

gem 'bootsnap', '>= 1.4.4', require: false
gem 'bcrypt'
gem 'dry-monads'
gem 'grape-swagger'
gem 'grape-swagger-entity'
gem 'jwt'
gem 'net-http'
gem 'pg', '~> 1.1'
gem 'puma', '~> 5.0'
gem 'pundit'
gem 'rack-cors'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'rails', '~> 6.1.4.1'
gem 'redis'
gem 'require_all'
gem 'strong_migrations'
gem 'uri'

group :development do
  gem 'database_consistency', require: false
  gem 'listen', '~> 3.3'
  gem 'rubocop', require: false
  gem 'rubocop-discourse', require: false
  gem 'rubocop-rails', require: false
  gem 'spring'
end

group :development, :test do
  gem 'bullet'
  gem 'bundler-audit'
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  gem 'dotenv-rails'
  gem 'factory_bot_rails'
  gem 'ffaker'
  gem 'rspec-rails', '~> 5.0.0'
end

group :test do
  gem 'rspec-sqlimit'
  gem 'shoulda-matchers', '~> 5.0'
  gem 'webmock'
end

gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

在我的 Gemfile.lock 末尾:

...


RUBY VERSION
   ruby 3.0.2p107

BUNDLED WITH
   2.2.21

我尝试过(如您在 heroku 日志中所见)连接不同的 ruby​​ buildpack,但没有帮助。

我尝试删除 Gemfile.lock,重新安装 gems 并将更新的文件推送回主分支,但没有帮助。

我尝试使用与 Heroku 相同的命令 (BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4) 在本地安装 gems 并推送到 git vendor/bundle 文件夹,但没有帮助。

我尝试按照日志的建议运行 git push heroku main:main,但没有帮助。

每次我看到同样的错误。

任何人都有想法,我做错了什么以及如何解决?提前非常感谢您!

【问题讨论】:

【参考方案1】:

原因在文件 bin/bundle 中。因为这是可执行的 shell 文件,所以这里的第一行必须是 #!/usr/bin/env ruby。当我删除前几行多余的行时,Heroku 会按预期读取 ruby​​ 文件。

这是 bin/bundle 的工作版本:

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
...

【讨论】:

以上是关于Heroku deplpy 失败并显示“..bin/bundle: line 11: require: command not found... Failed to install gems via 的主要内容,如果未能解决你的问题,请参考以下文章

Heroku 部署错误;预编译资产失败(注意 devise.rb 中的行)

远程图像处理本地反应实例在 Heroku 上失败

Heroku Node.js 和 uWebsockets.js R10 错误,绑定端口失败

Ansible 失败并显示 /bin/sh: 1: /usr/bin/python: not found

Heroku推送失败并出现关于sqlite3的错误[重复]

Heroku CLI 登录失败并出现错误“错误:ENOENT:没有这样的文件或目录,打开 'H:/_netrc'”