Skip to content

Commit

Permalink
Only require irb if console is unconfigured
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
skipkayhil committed Feb 22, 2023
1 parent 1797e5b commit dccb336
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions railties/lib/rails/commands/console/console_command.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "irb"
require "irb/completion"

require "rails/command/environment_argument"

module Rails
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions railties/test/commands/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dccb336

Please sign in to comment.