Skip to content

Commit 4afc98c

Browse files
authored
Reset history counter even when @loaded_history_lines is not defined (#853)
The issue (https://github.com/ruby/debug/issues/1064) is caused by a combination of factors: 1. When user starts an IRB session without a history file, the `@loaded_history_lines` ivar is not defined. 2. If the user then starts the `irb:rdbg` session, the history counter is not set, because the `@loaded_history_lines` is not defined. 3. Because `irb:rdbg` saves the history before passing Ruby expression to the debugger, it saves the history with duplicated lines. The number grows in exponential order. 4. When the user exits the `irb:rdbg` session, the history file could be bloated with duplicated lines. This commit fixes the issue by resetting the history counter even when `@loaded_history_lines` is not defined.
1 parent 06b2d00 commit 4afc98c

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

lib/irb/history.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def support_history_saving?
55
end
66

77
def reset_history_counter
8-
@loaded_history_lines = self.class::HISTORY.size if defined? @loaded_history_lines
8+
@loaded_history_lines = self.class::HISTORY.size
99
end
1010

1111
def load_history

test/irb/test_history.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ def with_temp_stdio
251251

252252
class IRBHistoryIntegrationTest < IntegrationTestCase
253253
def test_history_saving_with_debug
254-
if ruby_core?
255-
omit "This test works only under ruby/irb"
256-
end
257-
258254
write_history ""
259255

260256
write_ruby <<~'RUBY'
@@ -293,6 +289,53 @@ def foo
293289
HISTORY
294290
end
295291

292+
def test_history_saving_with_debug_without_prior_history
293+
tmpdir = Dir.mktmpdir("test_irb_history_")
294+
# Intentionally not creating the file so we test the reset counter logic
295+
history_file = File.join(tmpdir, "irb_history")
296+
297+
write_rc <<~RUBY
298+
IRB.conf[:HISTORY_FILE] = "#{history_file}"
299+
RUBY
300+
301+
write_ruby <<~'RUBY'
302+
def foo
303+
end
304+
305+
binding.irb
306+
307+
foo
308+
RUBY
309+
310+
output = run_ruby_file do
311+
type "'irb session'"
312+
type "next"
313+
type "'irb:debug session'"
314+
type "step"
315+
type "irb_info"
316+
type "puts Reline::HISTORY.to_a.to_s"
317+
type "q!"
318+
end
319+
320+
assert_include(output, "InputMethod: RelineInputMethod")
321+
# check that in-memory history is preserved across sessions
322+
assert_include output, %q(
323+
["'irb session'", "next", "'irb:debug session'", "step", "irb_info", "puts Reline::HISTORY.to_a.to_s"]
324+
).strip
325+
326+
assert_equal <<~HISTORY, File.read(history_file)
327+
'irb session'
328+
next
329+
'irb:debug session'
330+
step
331+
irb_info
332+
puts Reline::HISTORY.to_a.to_s
333+
q!
334+
HISTORY
335+
ensure
336+
FileUtils.rm_rf(tmpdir)
337+
end
338+
296339
def test_history_saving_with_nested_sessions
297340
write_history ""
298341

0 commit comments

Comments
 (0)