File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,6 +78,15 @@ class Irb
7878 PROMPT_MAIN_TRUNCATE_OMISSION = '...'
7979 CONTROL_CHARACTERS_PATTERN = "\x00 -\x1F "
8080
81+ # Track the nesting depth of run loops. This is used for history management
82+ # only the outermost run loop should load/save history.
83+ @run_nesting_depth = 0
84+
85+ class << self
86+ # TODO: When refactoring to v2.0, find a better way to manage and track nesting sessions.
87+ attr_accessor :run_nesting_depth
88+ end
89+
8190 # Returns the current context of this irb session
8291 attr_reader :context
8392 # The lexer used by this irb session
@@ -149,7 +158,10 @@ def debug_readline(binding)
149158 end
150159
151160 def run ( conf = IRB . conf )
152- in_nested_session = !!conf [ :MAIN_CONTEXT ]
161+ # Use run_nesting_depth to determine if we're in a nested session.
162+ in_nested_session = Irb . run_nesting_depth > 0
163+ Irb . run_nesting_depth += 1
164+
153165 conf [ :IRB_RC ] . call ( context ) if conf [ :IRB_RC ]
154166 prev_context = conf [ :MAIN_CONTEXT ]
155167 conf [ :MAIN_CONTEXT ] = context
@@ -175,6 +187,8 @@ def run(conf = IRB.conf)
175187 eval_input
176188 end
177189 ensure
190+ Irb . run_nesting_depth -= 1
191+
178192 # Do not restore to nil. It will cause IRB crash when used with threads.
179193 IRB . conf [ :MAIN_CONTEXT ] = prev_context if prev_context
180194
Original file line number Diff line number Diff line change @@ -565,6 +565,29 @@ def test_direct_debug_session_loads_history
565565 assert_include ( output , "0: old_history_1" )
566566 end
567567
568+ def test_history_saving_with_irb_start_after_debug_console_setup
569+ # this makes debug.gem run activate_irb_integration, which sets up IRB context for debug.gem
570+ @envs [ 'RUBY_DEBUG_IRB_CONSOLE' ] = "1"
571+ write_history ""
572+
573+ write_ruby <<~'RUBY'
574+ require 'debug' # this starts the debug.gem session, which runs activate_irb_integration
575+ require 'irb'
576+ puts 'binding.irb' # integration test uses binding.irb to identify the start of the IRB session
577+ IRB.start
578+ RUBY
579+
580+ run_ruby_file do
581+ type "puts 'from_irb_start'"
582+ type "exit"
583+ end
584+
585+ assert_equal <<~HISTORY , @history_file . open . read
586+ puts 'from_irb_start'
587+ exit
588+ HISTORY
589+ end
590+
568591 private
569592
570593 def write_history ( history )
You can’t perform that action at this time.
0 commit comments