diff --git a/lib/irb.rb b/lib/irb.rb index c76c3f569..01f4c7697 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -77,6 +77,15 @@ class Irb PROMPT_MAIN_TRUNCATE_OMISSION = '...' CONTROL_CHARACTERS_PATTERN = "\x00-\x1F" + # Track the nesting depth of run loops. This is used for history management + # only the outermost run loop should load/save history. + @run_nesting_depth = 0 + + class << self + # TODO: When refactoring to v2.0, find a better way to manage and track nesting sessions. + attr_accessor :run_nesting_depth + end + # Returns the current context of this irb session attr_reader :context # The lexer used by this irb session @@ -148,7 +157,10 @@ def debug_readline(binding) end def run(conf = IRB.conf) - in_nested_session = !!conf[:MAIN_CONTEXT] + # Use run_nesting_depth to determine if we're in a nested session. + in_nested_session = Irb.run_nesting_depth > 0 + Irb.run_nesting_depth += 1 + conf[:IRB_RC].call(context) if conf[:IRB_RC] prev_context = conf[:MAIN_CONTEXT] conf[:MAIN_CONTEXT] = context @@ -174,6 +186,8 @@ def run(conf = IRB.conf) eval_input end ensure + Irb.run_nesting_depth -= 1 + # Do not restore to nil. It will cause IRB crash when used with threads. IRB.conf[:MAIN_CONTEXT] = prev_context if prev_context diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb index 77b680d91..c6e931e80 100644 --- a/test/irb/test_history.rb +++ b/test/irb/test_history.rb @@ -565,6 +565,29 @@ def test_direct_debug_session_loads_history assert_include(output, "0: old_history_1") end + def test_history_saving_with_irb_start_after_debug_console_setup + # this makes debug.gem run activate_irb_integration, which sets up IRB context for debug.gem + @envs['RUBY_DEBUG_IRB_CONSOLE'] = "1" + write_history "" + + write_ruby <<~'RUBY' + require 'debug' # this starts the debug.gem session, which runs activate_irb_integration + require 'irb' + puts 'binding.irb' # integration test uses binding.irb to identify the start of the IRB session + IRB.start + RUBY + + run_ruby_file do + type "puts 'from_irb_start'" + type "exit" + end + + assert_equal <<~HISTORY, @history_file.open.read + puts 'from_irb_start' + exit + HISTORY + end + private def write_history(history)