Skip to content

Commit

Permalink
Disable Rails console IRB's autocompletion in production
Browse files Browse the repository at this point in the history
Reasons behind this change:

1. Autocompletion increases data transmission significantly. This could
   cause noticeable slowdown when connecting to remote servers, which is
   usually the case in production.
2. The autocompletion feature still has many issues, as listed in
   ruby/irb#445. They may be just annoying
   when happened locally, but in production they could cause real
   issues (e.g. typos caused by slow backspacing).

Due to these reasons, I think it's safer to disable this feature in
production.

About the implementation:

Because `IRB.start` doesn't take configuration arguments and rebuilds
the `IRB.conf` object, the only way we can turn off autocompletion is
through the `IRB_USE_AUTOCOMPLETE` env var.

The env var was added in ruby/irb#469 and will
be available for IRB 1.6+ and Ruby 3.2+.

The name wasn't used before so it won't cause issues with older IRB
versions.

Note: Users can still turn it back on with `IRB_USE_AUTOCOMPLETE=true`
  • Loading branch information
st0012 committed Dec 6, 2022
1 parent ade27e5 commit e38ff49
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions railties/lib/rails/commands/console/console_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def initialize(app, options = {})

if @console == IRB
IRB::WorkSpace.prepend(BacktraceCleaner)

if Rails.env == "production"
ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
end
end
end

Expand Down
39 changes: 39 additions & 0 deletions railties/test/commands/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ def test_console_defaults_to_IRB
assert_equal IRB, Rails::Console.new(app).console
end

def test_console_disables_IRB_auto_completion_in_production
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = nil

with_rack_env "production" do
app = build_app(nil)
assert_equal IRB, Rails::Console.new(app).console
assert_equal "false", ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end

def test_console_accepts_override_on_IRB_auto_completion_flag
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = "true"

with_rack_env "production" do
app = build_app(nil)
assert_equal IRB, Rails::Console.new(app).console
assert_equal "true", ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end

def test_console_doesnt_disable_IRB_auto_completion_in_non_production
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = nil

with_rails_env nil do
app = build_app(nil)
assert_equal IRB, Rails::Console.new(app).console
assert_nil ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end

def test_default_environment_with_no_rails_env
with_rails_env nil do
start
Expand Down

0 comments on commit e38ff49

Please sign in to comment.