Skip to content

Commit 076aba8

Browse files
Auto switch to locked bundler version even when using binstubs
1 parent 3b40b2d commit 076aba8

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

bundler/lib/bundler.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ def setup(*groups)
167167
end
168168
end
169169

170+
def auto_switch
171+
self_manager.restart_with_locked_bundler_if_needed
172+
end
173+
170174
# Automatically install dependencies if Bundler.settings[:auto_install] exists.
171175
# This is set through config cmd `bundle config set --global auto_install 1`.
172176
#

bundler/lib/bundler/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def initialize(*args)
6565
Bundler.reset_settings_and_root!
6666
end
6767

68-
Bundler.self_manager.restart_with_locked_bundler_if_needed
68+
Bundler.auto_switch
6969

7070
Bundler.settings.set_command_option_if_given :retry, options[:retry]
7171

bundler/lib/bundler/self_manager.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def needs_switching?
9292
def autoswitching_applies?
9393
ENV["BUNDLER_VERSION"].nil? &&
9494
Bundler.rubygems.supports_bundler_trampolining? &&
95+
ruby_can_restart_with_same_arguments? &&
9596
SharedHelpers.in_bundle? &&
9697
lockfile_version
9798
end
@@ -151,6 +152,10 @@ def released?(version)
151152
!version.to_s.end_with?(".dev")
152153
end
153154

155+
def ruby_can_restart_with_same_arguments?
156+
$PROGRAM_NAME != "-e"
157+
end
158+
154159
def updating?
155160
"update".start_with?(ARGV.first || " ") && ARGV[1..-1].any? {|a| a.start_with?("--bundler") }
156161
end

bundler/lib/bundler/setup.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
if Bundler::SharedHelpers.in_bundle?
66
require_relative "../bundler"
77

8+
# autoswitch to locked Bundler version if available
9+
Bundler.auto_switch
10+
811
# try to auto_install first before we get to the `Bundler.ui.silence`, so user knows what is happening
912
Bundler.auto_install
1013

bundler/spec/runtime/self_management_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
bundle "-v", artifice: nil
3636
expect(out).to end_with(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)
3737

38+
# App now uses locked version, even when not using the CLI directly
39+
file = bundled_app("bin/bundle_version.rb")
40+
create_file file, <<-RUBY
41+
#!#{Gem.ruby}
42+
require 'bundler/setup'
43+
puts Bundler::VERSION
44+
RUBY
45+
file.chmod(0o777)
46+
sys_exec "bin/bundle_version.rb", artifice: nil
47+
expect(out).to eq(previous_minor)
48+
3849
# Subsequent installs use the locked version without reinstalling
3950
bundle "install --verbose", artifice: nil
4051
expect(out).to include("Using bundler #{previous_minor}")
@@ -57,6 +68,17 @@
5768
bundle "-v"
5869
expect(out).to end_with(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)
5970

71+
# App now uses locked version, even when not using the CLI directly
72+
file = bundled_app("bin/bundle_version.rb")
73+
create_file file, <<-RUBY
74+
#!#{Gem.ruby}
75+
require 'bundler/setup'
76+
puts Bundler::VERSION
77+
RUBY
78+
file.chmod(0o777)
79+
sys_exec "bin/bundle_version.rb", artifice: nil
80+
expect(out).to eq(previous_minor)
81+
6082
# Subsequent installs use the locked version without reinstalling
6183
bundle "install --verbose"
6284
expect(out).to include("Using bundler #{previous_minor}")

0 commit comments

Comments
 (0)