class Rake::Application

def load_debug_at_stop_feature

def load_debug_at_stop_feature
  return unless ENV["RAKE_DEBUG"]
  require "debug/session"
  DEBUGGER__::start no_sigint_hook: true, nonstop: true
  Rake::Task.prepend Module.new {
    def execute(*)
      exception = DEBUGGER__::SESSION.capture_exception_frames(/(exe|bin|lib)\/rake/) do
        super
      end
      if exception
        STDERR.puts exception.message
        DEBUGGER__::SESSION.enter_postmortem_session exception
        raise exception
      end
    end
  }
rescue LoadError
end