getappmap / appmap-ruby

AppMap client agent for Ruby
https://appland.org
Other
100 stars 13 forks source link

feat: Hook kernel eval #228

Closed kgilpin closed 2 years ago

kgilpin commented 2 years ago

The hook of Kernel.eval does not happen in the right module/class context. Therefore if you try and eval a string like this:

module MyModule
  eval "class Foo; end"
end

The constant Foo is defined on AppMap::Hook::Method rather than on MyModule. We would like to hook eval so that any new constants are defined on the right module.

PS Is there any other way to dynamically evaluate a language string in Ruby? If so, we would need to fix this there as well. I think so : class_eval and class_exec - https://ruby-doc.org/core-2.6/Module.html#method-i-class_eval

This could also be the root cause of the note that I left in ruby.yml:

# These methods cannot be hooked as far as I can tell.
# Why? When calling one of these functions, the context at the point of 
# definition is used. It's not possible to bind class_eval to a new context.
#  - Module#class_eval
#  - Module#module_eval