Skip to content

Commit

Permalink
Make irb_console toggleable with config update
Browse files Browse the repository at this point in the history
1. When the user sets `irb_console` to `true` through the command, the
   `irb:rdbg` will now be enabled the same way as typing the `irb` command.
2. When the user sets `irb_console` to `false` through the command, the
   `irb:rdbg` will now be disabled.
3. Users can now enable `irb:rdbg` by setting `irb_console` to `true` in
   their `.rdbgrc` file.
  • Loading branch information
st0012 committed Jan 2, 2024
1 parent 9de0ff4 commit 2e6c807
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
20 changes: 20 additions & 0 deletions lib/debug/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ def update conf
SESSION.set_no_sigint_hook old, new
end
end

if_updated old_conf, conf, :irb_console do |old, new|
if defined?(SESSION) && SESSION.active?
# irb_console is switched from true to false
if old
Reline.completion_proc = nil
Reline.output_modifier_proc = nil
Reline.autocompletion = false
Reline.dig_perfect_match_proc = nil
SESSION.reset_ui UI_LocalConsole.new
# irb_console is switched from false to true
else
if CONFIG[:open]
SESSION.instance_variable_get(:@ui).puts "\nIRB is not supported on the remote console."
else
SESSION.activate_irb_integration
end
end
end
end
end

private def if_updated old_conf, new_conf, key
Expand Down
22 changes: 15 additions & 7 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,19 @@ def activate ui = nil, on_fork: false
end
@tp_thread_end.enable

if CONFIG[:irb_console] && !CONFIG[:open]
require_relative "irb_integration"
thc.activate_irb_integration
end

# session start
q << true
session_server_main
end
first_q << :ok

q.pop

# For activating irb:rdbg with startup config like `RUBY_DEBUG_IRB_CONSOLE=1`
# Because in that case the `Config#if_updated` callback would not be triggered
if CONFIG[:irb_console] && !CONFIG[:open]
activate_irb_integration
end
end

def deactivate
Expand Down Expand Up @@ -942,10 +943,11 @@ def register_default_command
register_command 'irb' do |arg|
if @ui.remote?
@ui.puts "\nIRB is not supported on the remote console."
:retry
else
request_eval :irb, nil
config_set :irb_console, true
end

:retry
end

### Trace
Expand Down Expand Up @@ -1876,6 +1878,12 @@ def check_unsafe
end
end

def activate_irb_integration
require_relative "irb_integration"
thc = get_thread_client(@session_server)
thc.activate_irb_integration
end

def enter_postmortem_session exc
return unless exc.instance_variable_defined? :@__debugger_postmortem_frames

Expand Down
3 changes: 0 additions & 3 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,6 @@ def wait_next_action_
end
when :call
result = frame_eval(eval_src)
when :irb
require_relative "irb_integration"
activate_irb_integration
when :display, :try_display
failed_results = []
eval_src.each_with_index{|src, i|
Expand Down
35 changes: 26 additions & 9 deletions test/console/irb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

module DEBUGGER__
class IrbTest < ConsoleTestCase
def setup
@original_pager = ENV["PAGER"]
ENV["PAGER"] = "cat"
end

def teardown
ENV["PAGER"] = @original_pager
end

def program
<<~RUBY
1| a = 1
Expand All @@ -26,6 +17,12 @@ def test_irb_command_is_disabled_in_remote_mode
assert_line_text 'IRB is not supported on the remote console.'
type 'q!'
end

debug_code(program, remote: :remote_only) do
type 'config set irb_console true'
assert_line_text 'IRB is not supported on the remote console.'
type 'q!'
end
end

def test_irb_command_switches_console_to_irb
Expand All @@ -38,6 +35,11 @@ def test_irb_command_switches_console_to_irb
type 'next'
type 'info'
assert_line_text([/a = 1/, /b = nil/])

# disable irb console
type 'config set irb_console false'
type '456'
assert_raw_line_text '(rdbg) 456'
type 'q!'
end
end
Expand All @@ -53,10 +55,25 @@ def test_irb_console_config_activates_irb
type 'next'
type 'info'
assert_line_text([/a = 1/, /b = nil/])

# disable irb console
type 'config set irb_console false'
type '456'
assert_raw_line_text '(rdbg) 456'
type 'q!'
end
ensure
ENV["RUBY_DEBUG_IRB_CONSOLE"] = nil
end

private

# assert_line_text ignores the prompt line, so we can't use it to assert the prompt transition
# assert_raw_line_text is a workaround for that
def assert_raw_line_text(expectation)
@scenario.push(Proc.new do |test_info|
assert_include(test_info.last_backlog.join, expectation)
end)
end
end
end

0 comments on commit 2e6c807

Please sign in to comment.