Skip to content

Commit e38ff49

Browse files
committed
Disable Rails console IRB's autocompletion in production
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`
1 parent ade27e5 commit e38ff49

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

railties/lib/rails/commands/console/console_command.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def initialize(app, options = {})
3838

3939
if @console == IRB
4040
IRB::WorkSpace.prepend(BacktraceCleaner)
41+
42+
if Rails.env == "production"
43+
ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
44+
end
4145
end
4246
end
4347

railties/test/commands/console_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,45 @@ def test_console_defaults_to_IRB
5858
assert_equal IRB, Rails::Console.new(app).console
5959
end
6060

61+
def test_console_disables_IRB_auto_completion_in_production
62+
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
63+
ENV["IRB_USE_AUTOCOMPLETE"] = nil
64+
65+
with_rack_env "production" do
66+
app = build_app(nil)
67+
assert_equal IRB, Rails::Console.new(app).console
68+
assert_equal "false", ENV["IRB_USE_AUTOCOMPLETE"]
69+
end
70+
ensure
71+
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
72+
end
73+
74+
def test_console_accepts_override_on_IRB_auto_completion_flag
75+
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
76+
ENV["IRB_USE_AUTOCOMPLETE"] = "true"
77+
78+
with_rack_env "production" do
79+
app = build_app(nil)
80+
assert_equal IRB, Rails::Console.new(app).console
81+
assert_equal "true", ENV["IRB_USE_AUTOCOMPLETE"]
82+
end
83+
ensure
84+
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
85+
end
86+
87+
def test_console_doesnt_disable_IRB_auto_completion_in_non_production
88+
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
89+
ENV["IRB_USE_AUTOCOMPLETE"] = nil
90+
91+
with_rails_env nil do
92+
app = build_app(nil)
93+
assert_equal IRB, Rails::Console.new(app).console
94+
assert_nil ENV["IRB_USE_AUTOCOMPLETE"]
95+
end
96+
ensure
97+
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
98+
end
99+
61100
def test_default_environment_with_no_rails_env
62101
with_rails_env nil do
63102
start

0 commit comments

Comments
 (0)