Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading