ruby / debug

Debugging functionality for Ruby
BSD 2-Clause "Simplified" License
1.14k stars 127 forks source link

Broken command history when I execute debugger on irb #994

Closed takatea closed 1 year ago

takatea commented 1 year ago

Your environment

Describe the bug

Broken command histories (both .irb_history and .rdbg_history), when I execute debugger on irb.

To Reproduce

  1. Prepare a ruby script test.rb like this:

    require "debug"
    
    class Test
       def run
           debugger
       end
    end
  2. Remove .irb_history and .rdbg_history to clean up

  3. Run irb

  4. Run the following commands on irb

    require "./test"
    puts "irb 1"
    puts "irb 2"
    puts "irb 3"
    Test.new.run
  5. Debugger started

  6. Run the following commands on debugger console

    puts "debug 4"
    puts "debug 5"
    puts "debug 6"
    continue
  7. Debugger finished (then I return to irb console)

  8. Run quit to exit irb

  9. Run irb again

  10. Run the following commands on irb

    puts "irb 7"
    puts "irb 8"
    puts "irb 9"
    require "./test"
    Test.new.run
  11. Debugger started

  12. Run the following commands on debugger console

    puts "debug 10"
    puts "debug 11"
    puts "debug 12"
    continue
  13. Run quit to exit irb

  14. Look at .irb_history and .rdbg_history

Actual behavior

I got:

[.irb_history]

``` require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue quit puts "irb 7" puts "irb 8" puts "irb 9" require "./test" Test.new.run # Today's OMIKUJI: CHU-KICHI require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue quit puts "debug 10" puts "debug 11" puts "debug 12" continue quit ```

[.rdbg_history]

``` require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue quit puts "irb 8" puts "irb 9" require "./test" Test.new.run require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue quit puts "debug 10" puts "debug 11" puts "debug 12" continue quit ```

Expected behavior

Both history .irb_history and .rdbg_history should have same contents:

require "./test"
puts "irb 1"
puts "irb 2"
puts "irb 3"
Test.new.run
puts "debug 4"
puts "debug 5"
puts "debug 6"
continue
quit
puts "irb 7"
puts "irb 8"
puts "irb 9"
require "./test"
Test.new.run
puts "debug 10"
puts "debug 11"
puts "debug 12"
continue
quit

I do not know where Today's OMIKUJI should appear at.

Additional context

This problem dose not appear with:

  1. Remove .irb_history and .rdbg_history to clean up

  2. Run irb

  3. Run the following commands on irb

    require "./test"
    puts "irb 1"
    puts "irb 2"
    puts "irb 3"
    Test.new.run
  4. Debugger started

  5. Run the following commands on debugger console

    puts "debug 4"
    puts "debug 5"
    puts "debug 6"
    continue
  6. Debugger finished (then I return to irb console)

  7. Do not exit irb console here!!!

  8. Run the following commands on irb

    puts "irb 7"
    puts "irb 8"
    puts "irb 9"
    require "./test"
    Test.new.run
  9. Debugger started

  10. Run the following commands on debugger console

    puts "debug 10"
    puts "debug 11"
    puts "debug 12"
    continue
  11. Run quit to exit irb

  12. Look at .irb_history and .rdbg_history

I got:

[.irb_history]

``` require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue puts "irb 7" puts "irb 8" puts "irb 9" Test.new.run puts "debug 10" puts "debug 11" puts "debug 12" continue quit ```

[.rdbg_history]

``` require "./test" puts "irb 1" puts "irb 2" puts "irb 3" Test.new.run puts "debug 4" puts "debug 5" puts "debug 6" continue puts "irb 7" puts "irb 8" puts "irb 9" Test.new.run puts "debug 10" puts "debug 11" puts "debug 12" continue quit ```
takatea commented 1 year ago

I asked the issue in the ruby-jp slack channel (#debugger) and found that the problem caused by the shared using Reline::HISTORY between irb and debug. (thanks to @ima1zumi and @tompng for their helpful insights!)

Therefore, the following steps are carried out to confirm that both irb and debug share the same Reline::HISTORY.

  1. Remove .irb_history and .rdbg_history to clean up

  2. Run irb

  3. Run the following commands on irb

    require "./test"
    puts "irb 1"
    puts "irb 2"
    puts "irb 3"
    Test.new.run
  4. Debugger started

  5. Puts Reline::HISTORY, and then I got:

    ["require \"./test\"",
    "puts \"irb 1\"",
    "puts \"irb 2\"",
    "puts \"irb 3\"",
    "Test.new.run",
    "Reline::HISTORY"]

When the debugger loads (appends) histories while running on irb, duplicate histories are currently stored in Reline::HISTORY because both the irb and debug history files contain the same saved content.

As a result, repeatedly saving and loading the same content leads to the history looks broken and bloating of the files.

ko1 commented 1 year ago

997

jon-sully commented 12 months ago

Looking forward to this one being released 😅 ❤️