Skip to content

Commit 7549ba7

Browse files
committed
Stop generating bundler binstub:
- Fix rails#54563 - ### Context In a future version of Bundler, running `bundle binstub bundler` will no longer generate a `bin/bundle` binstub. The reason for this change upstream is well explained in rubygems/rubygems#8345. ### Problem When the change in Bundler is cut into a release, generating new rails application will throw a Bundler warning, and the AuthenticationGenerator which makes use of `bin/bundle` will stop working. ### Solution Stop generating a bundler binstub and modify the generator to use the same bundler command as the rest of the codebase.
1 parent cad2196 commit 7549ba7

File tree

8 files changed

+54
-49
lines changed

8 files changed

+54
-49
lines changed

guides/source/working_with_javascript_in_rails.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ When using import maps, no separate build process is required, just start your s
3737
Importmap for Rails is automatically included in Rails 7+ for new applications, but you can also install it manually in existing applications:
3838

3939
```bash
40-
$ bin/bundle add importmap-rails
40+
$ bundle add importmap-rails
4141
```
4242

4343
Run the install task:

railties/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Rails no longer generates a `bin/bundle` binstub when creating new applications.
2+
3+
The `bin/bundle` binstub used to help activate the right version of bundler.
4+
This is no longer necessary as this mechanism is now part of Rubygem itself.
5+
6+
*Edouard Chin*
7+
18
* Add a `SessionTestHelper` module with `sign_in_as(user)` and `sign_out` test helpers when
29
running `rails g authentication`. Simplifies authentication in integration tests.
310

railties/lib/rails/generators/app_base.rb

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
require "tsort"
88
require "uri"
99
require "rails/generators"
10+
require "rails/generators/bundle_helper"
1011
require "active_support/core_ext/array/extract_options"
1112

1213
module Rails
1314
module Generators
1415
class AppBase < Base # :nodoc:
1516
include AppName
17+
include BundleHelper
1618

1719
NODE_LTS_VERSION = "20.11.1"
1820
BUN_VERSION = "1.0.1"
@@ -643,32 +645,6 @@ def cable_gemfile_entry
643645
end
644646
end
645647

646-
def bundle_command(command, env = {})
647-
say_status :run, "bundle #{command}"
648-
649-
# We are going to shell out rather than invoking Bundler::CLI.new(command)
650-
# because `rails new` loads the Thor gem and on the other hand bundler uses
651-
# its own vendored Thor, which could be a different version. Running both
652-
# things in the same process is a recipe for a night with paracetamol.
653-
#
654-
# Thanks to James Tucker for the Gem tricks involved in this call.
655-
_bundle_command = Gem.bin_path("bundler", "bundle")
656-
657-
require "bundler"
658-
Bundler.with_original_env do
659-
exec_bundle_command(_bundle_command, command, env)
660-
end
661-
end
662-
663-
def exec_bundle_command(bundle_command, command, env)
664-
full_command = %Q["#{Gem.ruby}" "#{bundle_command}" #{command}]
665-
if options[:quiet]
666-
system(env, full_command, out: File::NULL)
667-
else
668-
system(env, full_command)
669-
end
670-
end
671-
672648
def bundle_install?
673649
!(options[:skip_bundle] || options[:pretend])
674650
end
@@ -763,12 +739,6 @@ def add_bundler_platforms
763739
end
764740
end
765741

766-
def generate_bundler_binstub
767-
if bundle_install?
768-
bundle_command("binstubs bundler")
769-
end
770-
end
771-
772742
def empty_directory_with_keep_file(destination, config = {})
773743
empty_directory(destination, config)
774744
keep_file(destination)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module Rails
4+
module Generators
5+
module BundleHelper # :nodoc:
6+
def bundle_command(command, env = {}, params = {})
7+
say_status :run, "bundle #{command}"
8+
9+
# We are going to shell out rather than invoking Bundler::CLI.new(command)
10+
# because `rails new` loads the Thor gem and on the other hand bundler uses
11+
# its own vendored Thor, which could be a different version. Running both
12+
# things in the same process is a recipe for a night with paracetamol.
13+
#
14+
# Thanks to James Tucker for the Gem tricks involved in this call.
15+
_bundle_command = Gem.bin_path("bundler", "bundle")
16+
17+
require "bundler"
18+
Bundler.with_original_env do
19+
exec_bundle_command(_bundle_command, command, env, params)
20+
end
21+
end
22+
23+
private
24+
def exec_bundle_command(bundle_command, command, env, params)
25+
full_command = %Q["#{Gem.ruby}" "#{bundle_command}" #{command}]
26+
if options[:quiet] || params[:quiet]
27+
system(env, full_command, out: File::NULL)
28+
else
29+
system(env, full_command)
30+
end
31+
end
32+
end
33+
end
34+
end

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ def finish_template
570570
public_task :apply_rails_template
571571
public_task :run_bundle
572572
public_task :add_bundler_platforms
573-
public_task :generate_bundler_binstub
574573
public_task :run_javascript
575574
public_task :run_hotwire
576575
public_task :run_css

railties/lib/rails/generators/rails/authentication/authentication_generator.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# frozen_string_literal: true
22

3+
require "rails/generators/bundle_helper"
4+
35
module Rails
46
module Generators
57
class AuthenticationGenerator < Base # :nodoc:
8+
include BundleHelper
9+
610
class_option :api, type: :boolean,
711
desc: "Generate API-only controllers and models, with no view templates"
812

@@ -42,9 +46,9 @@ def configure_authentication_routes
4246
def enable_bcrypt
4347
if File.read(File.expand_path("Gemfile", destination_root)).include?('gem "bcrypt"')
4448
uncomment_lines "Gemfile", /gem "bcrypt"/
45-
Bundler.with_original_env { execute_command :bundle, "install --quiet" }
49+
bundle_command("install --quiet")
4650
else
47-
Bundler.with_original_env { execute_command :bundle, "add bcrypt", capture: true }
51+
bundle_command("add bcrypt", {}, quiet: true)
4852
end
4953
end
5054

railties/test/generators/app_generator_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,6 @@ def test_generation_use_original_bundle_environment
829829
end
830830
end
831831

832-
def test_bundler_binstub
833-
generator([destination_root])
834-
run_generator_instance
835-
836-
assert_equal 1, @bundle_commands.count("binstubs bundler")
837-
end
838-
839832
def test_skip_active_job_option
840833
run_generator [destination_root, "--skip-active-job"]
841834

railties/test/generators/authentication_generator_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_authentication_generator_without_bcrypt_in_gemfile
7575

7676
run_generator_instance
7777

78-
assert_includes @bundle_commands, [:bundle, "add bcrypt", { capture: true }]
78+
assert_includes @bundle_commands, ["add bcrypt", {}, { quiet: true }]
7979
end
8080

8181
def test_authentication_generator_with_api_flag
@@ -139,21 +139,19 @@ def test_connection_class_skipped_without_action_cable
139139

140140
private
141141
def run_generator_instance
142-
commands = []
143-
command_stub ||= -> (command, *args) { commands << [command, *args] }
142+
@bundle_commands = []
143+
command_stub ||= -> (command, *args) { @bundle_commands << [command, *args] }
144144

145145
@rails_commands = []
146146
@rails_command_stub ||= -> (command, *_) { @rails_commands << command }
147147

148148
content = nil
149-
generator.stub(:execute_command, command_stub) do
149+
generator.stub(:bundle_command, command_stub) do
150150
generator.stub(:rails_command, @rails_command_stub) do
151151
content = super
152152
end
153153
end
154154

155-
@bundle_commands = commands.filter { |command, _| command == :bundle }
156-
157155
content
158156
end
159157
end

0 commit comments

Comments
 (0)