Skip to content

Commit fd4a4a9

Browse files
committed
Upgrade the irb command to use irb:debug integration
This means that user will get an IRB session that has access to the debug commands too by activating IRB's debug integration automatically: https://github.com/ruby/irb#debugging-with-irb
1 parent cbd0f71 commit fd4a4a9

File tree

6 files changed

+79
-10
lines changed

6 files changed

+79
-10
lines changed

debug.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
2727
spec.require_paths = ["lib"]
2828
spec.extensions = ['ext/debug/extconf.rb']
2929

30-
spec.add_dependency "irb", ">= 1.5.0" # for binding.irb(show_code: false)
30+
spec.add_dependency "irb", "~> 1.10" # for irb:debug integration
3131
spec.add_dependency "reline", ">= 0.3.8"
3232
end

lib/debug/irb_integration.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require 'irb'
4+
5+
module DEBUGGER__
6+
module IrbPatch
7+
def evaluate(line, line_no)
8+
SESSION.send(:restart_all_threads)
9+
super
10+
# This is to communicate with the test framework so it can feed the next input
11+
puts "INTERNAL_INFO: {}" if ENV['RUBY_DEBUG_TEST_UI'] == 'terminal'
12+
ensure
13+
SESSION.send(:stop_all_threads)
14+
end
15+
end
16+
17+
class ThreadClient
18+
def activate_irb_integration
19+
IRB.setup(location, argv: [])
20+
workspace = IRB::WorkSpace.new(current_frame&.binding || TOPLEVEL_BINDING)
21+
irb = IRB::Irb.new(workspace)
22+
IRB.conf[:MAIN_CONTEXT] = irb.context
23+
IRB::Debug.setup(irb)
24+
IRB::Context.prepend(IrbPatch)
25+
end
26+
end
27+
end

lib/debug/session.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,11 @@ def register_default_command
936936
# * Invoke `irb` on the current frame.
937937
register_command 'irb' do |arg|
938938
if @ui.remote?
939-
@ui.puts "not supported on the remote console."
939+
@ui.puts "\nIRB is supported on the remote console."
940940
:retry
941+
else
942+
request_eval :irb, nil
941943
end
942-
request_eval :irb, nil
943944
end
944945

945946
### Trace

lib/debug/thread_client.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,13 +1048,8 @@ def wait_next_action_
10481048
when :call
10491049
result = frame_eval(eval_src)
10501050
when :irb
1051-
require 'irb' # prelude's binding.irb doesn't have show_code option
1052-
begin
1053-
result = frame_eval('binding.irb(show_code: false)', binding_location: true)
1054-
ensure
1055-
# workaround: https://github.com/ruby/debug/issues/308
1056-
Reline.prompt_proc = nil if defined? Reline
1057-
end
1051+
require_relative "irb_integration"
1052+
activate_irb_integration
10581053
when :display, :try_display
10591054
failed_results = []
10601055
eval_src.each_with_index{|src, i|

test/console/irb_test.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../support/console_test_case'
4+
5+
module DEBUGGER__
6+
class IrbTest < ConsoleTestCase
7+
def setup
8+
@original_pager = ENV["PAGER"]
9+
ENV["PAGER"] = "cat"
10+
end
11+
12+
def teardown
13+
ENV["PAGER"] = @original_pager
14+
end
15+
16+
def program
17+
<<~RUBY
18+
1| a = 1
19+
2| b = 2
20+
RUBY
21+
end
22+
23+
def test_irb_command_is_disabled_in_remote_mode
24+
debug_code(program, remote: :remote_only) do
25+
type 'irb'
26+
assert_line_text 'IRB is supported on the remote console.'
27+
type 'q!'
28+
end
29+
end
30+
31+
def test_irb_command_switches_console_to_irb
32+
debug_code(program, remote: false) do
33+
type 'irb'
34+
type '123'
35+
assert_line_text 'irb:rdbg(main):002> 123'
36+
type 'irb_info'
37+
assert_line_text('IRB version:')
38+
type 'next'
39+
type 'info'
40+
assert_line_text([/a = 1/, /b = nil/])
41+
type 'q!'
42+
end
43+
end
44+
end
45+
end

test/support/console_test_case.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def prepare_test_environment(program, test_steps, &block)
217217
ENV['RUBY_DEBUG_TEST_UI'] = 'terminal'
218218
ENV['RUBY_DEBUG_NO_RELINE'] = 'true'
219219
ENV['RUBY_DEBUG_HISTORY_FILE'] = ''
220+
ENV['TERM'] = 'dumb'
220221

221222
write_temp_file(strip_line_num(program))
222223
@scenario = []

0 commit comments

Comments
 (0)