diff options
author | Takashi Kokubun <[email protected]> | 2022-11-18 10:03:29 -0800 |
---|---|---|
committer | git <[email protected]> | 2022-11-18 18:03:33 +0000 |
commit | ba3b40a9aeb752d1b3e4c87748c977bfd3cf6f2c (patch) | |
tree | 878f70ab0d7510034b517ebf3e3535ab70ae0918 | |
parent | b1cbc883f2add06479113b61005f4cdfa90ff266 (diff) |
[ruby/irb] Discover and load debug.gem even if it's not in Gemfile
(https://github.com/ruby/irb/pull/448)
* Minor fixes on debug command
* Discover and load debug.gem even if it's not in Gemfile
* Eliminate else for rescue
* Discover the latest one from all gem paths
-rw-r--r-- | lib/irb/cmd/debug.rb | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/irb/cmd/debug.rb b/lib/irb/cmd/debug.rb index 369c112257..d43e060c67 100644 --- a/lib/irb/cmd/debug.rb +++ b/lib/irb/cmd/debug.rb @@ -48,8 +48,8 @@ module IRB unless defined?(DEBUGGER__::SESSION) begin require "debug/session" - rescue LoadError - return false + rescue LoadError # debug.gem is not written in Gemfile + return false unless load_bundled_debug_gem end DEBUGGER__.start(nonstop: true) end @@ -68,6 +68,29 @@ module IRB true end + + # This is used when debug.gem is not written in Gemfile. Even if it's not + # installed by `bundle install`, debug.gem is installed by default because + # it's a bundled gem. This method tries to activate and load that. + def load_bundled_debug_gem + # Discover latest debug.gem under GEM_PATH + debug_gem = Gem.paths.path.map { |path| Dir.glob("#{path}/gems/debug-*") }.flatten.select do |path| + File.basename(path).match?(/\Adebug-\d+\.\d+\.\d+\z/) + end.sort_by do |path| + Gem::Version.new(File.basename(path).delete_prefix('debug-')) + end.last + return false unless debug_gem + + # Attempt to forcibly load the bundled gem + $LOAD_PATH << "#{debug_gem}/lib" + begin + require "debug/session" + puts "Loaded #{File.basename(debug_gem)}" + true + rescue LoadError + false + end + end end end end |