Skip to content

Commit

Permalink
Use TracePoint.allow_reentry when available
Browse files Browse the repository at this point in the history
This fixes compatibility with TracePoint users such as Zeitwerk

Test script:

```ruby
require 'fileutils'
FileUtils.mkdir_p('/tmp/lib/foo')
File.write('/tmp/lib/foo.rb', 'module Foo; end')
File.write('/tmp/lib/foo/bar.rb', 'Foo::Bar = 1')

require 'zeitwerk'

loader = Zeitwerk::Loader.new
loader.push_dir('/tmp/lib')
loader.setup

require 'debug'
binding.break
p Foo::Bar
```

before:
```
[8, 16] in /tmp/debug-zeitwerk.rb
     8| loader = Zeitwerk::Loader.new
     9| loader.push_dir('/tmp/lib')
    10| loader.setup
    11|
    12| require 'debug'
=>  13| binding.break
    14| p Foo::Bar
    15|
    16| p :done
=>#0    <main> at /tmp/debug-zeitwerk.rb:13
(rdbg) p Foo::Bar    # command
eval error: uninitialized constant Foo::Bar
  (rdbg)//tmp/debug-zeitwerk.rb:1:in `<main>'
=> nil
```

after:
```
[8, 16] in /tmp/debug-zeitwerk.rb
     8| loader = Zeitwerk::Loader.new
     9| loader.push_dir('/tmp/lib')
    10| loader.setup
    11|
    12| require 'debug'
=>  13| binding.break
    14| p Foo::Bar
    15|
    16| p :done
=>#0    <main> at /tmp/debug-zeitwerk.rb:13
(rdbg) p Foo::Bar    # command
=> 1
```
  • Loading branch information
byroot committed Feb 16, 2022
1 parent a21742d commit b945a93
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ def instance_eval_for_cmethod frame_self, src
[:return_value, "_return"],
]

def eval_with_binding(binding, src)
if binding
f, _l = binding.source_location
binding.eval(src, "(rdbg)/#{f}")
else
frame_self = current_frame.self
instance_eval_for_cmethod(frame_self, src)
end
end

def frame_eval src, re_raise: false
@success_last_eval = false

Expand All @@ -365,13 +375,12 @@ def frame_eval src, re_raise: false
b.local_variable_set(name, var) if /\%/ !~ name
end

result = if b
f, _l = b.source_location
b.eval(src, "(rdbg)/#{f}")
else
frame_self = current_frame.self
instance_eval_for_cmethod(frame_self, src)
end
result = if TracePoint.respond_to?(:allow_reentry)
TracePoint.allow_reentry { eval_with_binding(b, src) }
else
eval_with_binding(b, src)
end

@success_last_eval = true
result

Expand Down

0 comments on commit b945a93

Please sign in to comment.