From dccb336c12a8642439addb3a4b3ff7fb081b2e37 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Wed, 22 Feb 2023 18:04:22 -0500 Subject: [PATCH] Only require irb if console is unconfigured Previously, irb would be required by the console whether or not another console was configured. While this is not a huge issue at the moment, it will become an issue if irb is removed as a default gem (which has been proposed for Ruby 3.3). This commit changes the console command to only require irb if it was not previously configured, preventing potential issues with it not being included in an application's dependencies. There is currently an open discussion about whether irb should be added to generated Gemfiles. This change is beneficial whether or not that PR is merged: - if irb is added to Gemfiles, then this change will prevent errors running the rails console for apps that use other consoles - if irb is added to railties dependencies, then this change will result in less libraries required for apps that use other consoles The tests needed to be updated because of the ordering of the assertion parameters. Since irb is now only required when the command is run, the constant can not be referenced first. --- railties/lib/rails/commands/console/console_command.rb | 10 +++++----- railties/test/commands/console_test.rb | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/railties/lib/rails/commands/console/console_command.rb b/railties/lib/rails/commands/console/console_command.rb index d5720ff5fbdf3..e95b270860582 100644 --- a/railties/lib/rails/commands/console/console_command.rb +++ b/railties/lib/rails/commands/console/console_command.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require "irb" -require "irb/completion" - require "rails/command/environment_argument" module Rails @@ -34,14 +31,17 @@ def initialize(app, options = {}) app.load_console - @console = app.config.console || IRB + @console = app.config.console || begin + require "irb" + require "irb/completion" - if @console == IRB IRB::WorkSpace.prepend(BacktraceCleaner) if Rails.env.production? ENV["IRB_USE_AUTOCOMPLETE"] ||= "false" end + + IRB end end diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index ef65f15cc3cef..bd2c6c461b145 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -55,7 +55,7 @@ def test_console_with_environment def test_console_defaults_to_IRB app = build_app(nil) - assert_equal IRB, Rails::Console.new(app).console + assert_equal "IRB", Rails::Console.new(app).console.name end def test_console_disables_IRB_auto_completion_in_production @@ -64,7 +64,7 @@ def test_console_disables_IRB_auto_completion_in_production with_rack_env "production" do app = build_app(nil) - assert_equal IRB, Rails::Console.new(app).console + assert_equal "IRB", Rails::Console.new(app).console.name assert_equal "false", ENV["IRB_USE_AUTOCOMPLETE"] end ensure @@ -77,7 +77,7 @@ def test_console_accepts_override_on_IRB_auto_completion_flag with_rack_env "production" do app = build_app(nil) - assert_equal IRB, Rails::Console.new(app).console + assert_equal "IRB", Rails::Console.new(app).console.name assert_equal "true", ENV["IRB_USE_AUTOCOMPLETE"] end ensure @@ -90,7 +90,7 @@ def test_console_doesnt_disable_IRB_auto_completion_in_non_production with_rails_env nil do app = build_app(nil) - assert_equal IRB, Rails::Console.new(app).console + assert_equal "IRB", Rails::Console.new(app).console.name assert_nil ENV["IRB_USE_AUTOCOMPLETE"] end ensure