Skip to content

Commit

Permalink
Upgrade the irb command to use irb:debug integration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
st0012 committed Oct 14, 2023
1 parent 4e70b17 commit c270cd0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
2 changes: 1 addition & 1 deletion debug.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
spec.extensions = ['ext/debug/extconf.rb']

spec.add_dependency "irb", ">= 1.5.0" # for binding.irb(show_code: false)
spec.add_dependency "irb", ">= 1.8.3" # for irb:debug integration
spec.add_dependency "reline", ">= 0.3.8"
end
27 changes: 27 additions & 0 deletions lib/debug/irb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'irb'

module DEBUGGER__
module IrbPatch
def evaluate(line, line_no)
SESSION.send(:restart_all_threads)
super
# This is to communicate with the test framework so it can feed the next input
puts "INTERNAL_INFO: {}" if ENV['RUBY_DEBUG_TEST_UI'] == 'terminal'
ensure
SESSION.send(:stop_all_threads)
end
end

class ThreadClient
def activate_irb_integration
IRB.setup(location, argv: [])
workspace = IRB::WorkSpace.new(current_frame&.binding || TOPLEVEL_BINDING)
irb = IRB::Irb.new(workspace)
IRB.conf[:MAIN_CONTEXT] = irb.context
IRB::Debug.setup(irb)
IRB::Context.prepend(IrbPatch)
end
end
end
9 changes: 2 additions & 7 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,13 +1048,8 @@ def wait_next_action_
when :call
result = frame_eval(eval_src)
when :irb
require 'irb' # prelude's binding.irb doesn't have show_code option
begin
result = frame_eval('binding.irb(show_code: false)', binding_location: true)
ensure
# workaround: https://github.com/ruby/debug/issues/308
Reline.prompt_proc = nil if defined? Reline
end
require_relative "irb"
activate_irb_integration
when :display, :try_display
failed_results = []
eval_src.each_with_index{|src, i|
Expand Down
28 changes: 28 additions & 0 deletions test/console/irb_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative '../support/console_test_case'

module DEBUGGER__
class IrbTest < ConsoleTestCase
def program
<<~RUBY
1| a = 1
2| b = 2
RUBY
end

def test_irb_command_switches_console_to_irb
debug_code(program, remote: false) do
type 'irb'
type '123'
assert_line_text 'irb:rdbg(main):002> 123'
type 'irb_info'
assert_line_text('IRB version:')
type 'next'
type 'info'
assert_line_text([/a = 1/, /b = nil/])
type 'q!'
end
end
end
end
1 change: 1 addition & 0 deletions test/support/console_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def prepare_test_environment(program, test_steps, &block)
ENV['RUBY_DEBUG_TEST_UI'] = 'terminal'
ENV['RUBY_DEBUG_NO_RELINE'] = 'true'
ENV['RUBY_DEBUG_HISTORY_FILE'] = ''
ENV['TERM'] = 'dumb'

write_temp_file(strip_line_num(program))
@scenario = []
Expand Down

0 comments on commit c270cd0

Please sign in to comment.