From c0caf1cc1aaa3ba2e9d09977ee2a51e35114c70f Mon Sep 17 00:00:00 2001 From: James Reid-Smith Date: Thu, 12 Dec 2024 12:26:03 -0500 Subject: [PATCH] [ruby/irb] Load history when starting a direct debug session (https://github.com/ruby/irb/pull/1046) * Load history when starting a direct debug session When starting a debug session directly with RUBY_DEBUG_IRB_CONSOLE=1 and `require 'debug'; debugger`, IRB's history wasn't loaded. This commit ensures history is loaded in this case by calling `load_history` when configuring IRB for the debugger. Fixes ruby/irb#975 * Update test/irb/test_history.rb * Update lib/irb/debug.rb --------- https://github.com/ruby/irb/commit/7f851b5353 Co-authored-by: Stan Lo --- lib/irb/debug.rb | 1 + test/irb/test_history.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/irb/debug.rb b/lib/irb/debug.rb index cd64b77ad7..59be1365bd 100644 --- a/lib/irb/debug.rb +++ b/lib/irb/debug.rb @@ -81,6 +81,7 @@ module IRB IRB.instance_variable_set(:@debugger_irb, irb) irb.context.with_debugger = true irb.context.irb_name += ":rdbg" + irb.context.io.load_history if irb.context.io.class < HistorySavingAbility end module SkipPathHelperForIRB diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb index 15c16ed897..021bb682c1 100644 --- a/test/irb/test_history.rb +++ b/test/irb/test_history.rb @@ -488,6 +488,36 @@ module TestIRB HISTORY end + def test_direct_debug_session_loads_history + @envs['RUBY_DEBUG_IRB_CONSOLE'] = "1" + write_history <<~HISTORY + old_history_1 + old_history_2 + old_history_3 + HISTORY + + write_ruby <<~'RUBY' + require 'debug' + debugger + binding.irb # needed to satisfy run_ruby_file + RUBY + + output = run_ruby_file do + type "history" + type "puts 'foo'" + type "history" + type "exit!" + end + + assert_include(output, "irb:rdbg(main):002") # assert that we're in an irb:rdbg session + assert_include(output, "5: history") + assert_include(output, "4: puts 'foo'") + assert_include(output, "3: history") + assert_include(output, "2: old_history_3") + assert_include(output, "1: old_history_2") + assert_include(output, "0: old_history_1") + end + private def write_history(history)