diff --git a/lib/bundler/templates/Executable.bundler b/lib/bundler/templates/Executable.bundler index 5b49648a1b..b5008d7c63 100644 --- a/lib/bundler/templates/Executable.bundler +++ b/lib/bundler/templates/Executable.bundler @@ -31,7 +31,7 @@ m = Module.new do bundler_version = a end next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ - bundler_version = $1 || ">= 0.a" + bundler_version = $1 update_index = i end bundler_version @@ -63,27 +63,30 @@ m = Module.new do def bundler_version @bundler_version ||= env_var_version || cli_arg_version || - lockfile_version || "#{Gem::Requirement.default}.a" + lockfile_version + end + + def bundler_requirement + return "#{Gem::Requirement.default}.a" unless bundler_version + + Gem::Version.new(bundler_version).approximate_recommendation end def load_bundler! ENV["BUNDLE_GEMFILE"] ||= gemfile - activate_bundler(bundler_version) + activate_bundler(bundler_requirement) end - def activate_bundler(bundler_version) - if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0") - bundler_version = "< 2" - end + def activate_bundler(bundler_requirement) gem_error = activation_error_handling do - gem "bundler", bundler_version + gem "bundler", bundler_requirement end return if gem_error.nil? require_error = activation_error_handling do require "bundler/version" end - return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION)) + return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`" exit 42 end diff --git a/spec/bundler/commands/binstubs_spec.rb b/spec/bundler/commands/binstubs_spec.rb index 3e2f7a53de..f4602c67c2 100644 --- a/spec/bundler/commands/binstubs_spec.rb +++ b/spec/bundler/commands/binstubs_spec.rb @@ -149,7 +149,7 @@ RSpec.describe "bundle binstubs " do and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`") end - it "runs the correct version of bundler when the version is older" do + it "runs the correct version of bundler when the version is older and a different major" do simulate_bundler_version "55" lockfile lockfile.gsub(system_bundler_version, "44.0") sys_exec "#{bundled_app("bin/bundle")} install" @@ -158,6 +158,14 @@ RSpec.describe "bundle binstubs " do and include("To install the version of bundler this project requires, run `gem install bundler -v '44.0'`") end + it "runs the available version of bundler when the version is older and the same major" do + simulate_bundler_version "55.1" + lockfile lockfile.gsub(system_bundler_version, "55.0") + sys_exec "#{bundled_app("bin/bundle")} install" + expect(exitstatus).not_to eq(42) if exitstatus + expect(err).not_to include("Activating bundler (55.0) failed:") + end + it "runs the correct version of bundler when the version is a pre-release" do simulate_bundler_version "55" lockfile lockfile.gsub(system_bundler_version, "2.12.0.a")