Feature Request: Push debugger activation to first breakpoint hit instead of `require "debug"`

オープン
#797 コメント 18 件 リアクション 9 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
ruby
領域
devtools

調査の方向性

Start by reading lib/debug.rb and the Kernel#debugger implementation in lib/debug/session.rb, then trace how require "debug", require "debug/prelude", and the open variants initialize sessions. Verify the proposed behavior against activation, TracePoint, thread, UI, and forking-message concerns described in the issue. Done means requiring debug is passive while the first breakpoint activates the debugger without changing the open variants.

索引モデルが issue の本文から書いたものです。

説明

Proposal

  • require "debug" won't activate the debugger. Debugger will only be activated when the first breakpoint is triggered.
    • require "debug" will be the same as require "debug/prelude"
  • require "debug/open" and require "debug/open_nonstop"'s behaviour will remain the same.

Why

Side-effects

In apps that have Bundler.require, like most Rails apps, having gem "debug" in Gemfile means the debug gem is always activated when the app is booted.

However, with the debugger's activation, many things will happen in the background:

  • Additional TracePoints would be enabled (example)
  • A new thread will be spawned
  • Additional computation and object allocation that come with the above

To most apps, having them in the background doesn't make a huge difference. But to some, having the debugger activated causes tests to fail.

For example, I tested 5 mid-to-large Rails apps in Shopify, and 3 of them have test failures that only happen when debug is activated:

  • Some tests mock File.readlines and could break when this line is triggered in the background.
  • There's also weird NoMethodError exceptions that only happens with the debugger activated.

Therefore, we always have require: false after gem "debug".

Problem Solved?

However, having require: false means users need to manually type require "debug" before start debugging with console. But in the meantime, byebug can be required by default without the same side-effects. So byebug doesn't need that manual require.

This extra require step is quite inconvenient for people considering switching from byebug to debug.

Other Smaller Issues
  • The activation and forking messages are noisy and disabling them project-by-project is not trivial.
  • require "debug" locks the UI to console, so if users want to use the remote UI by requiring debug/open after the app is booted, require: false is also needed.
Possible Change
diff --git a/lib/debug.rb b/lib/debug.rb
index 15ebccb..3489268 100644
--- a/lib/debug.rb
+++ b/lib/debug.rb
@@ -1,5 +1,3 @@
 # frozen_string_literal: true

 require_relative 'debug/session'
-return unless defined?(DEBUGGER__)
-DEBUGGER__::start no_sigint_hook: true, nonstop: true
diff --git a/lib/debug/session.rb b/lib/debug/session.rb
index a900f89..ffb2604 100644
--- a/lib/debug/session.rb
+++ b/lib/debug/session.rb
@@ -2486,7 +2486,9 @@ end

 module Kernel
   def debugger pre: nil, do: nil, up_level: 0
-    return if !defined?(::DEBUGGER__::SESSION) || !::DEBUGGER__::SESSION.active?
+    if !defined?(::DEBUGGER__::SESSION) || !::DEBUGGER__::SESSION.active?
+      DEBUGGER__::start no_sigint_hook: true, nonstop: true
+    end

     if pre || (do_expr = binding.local_variable_get(:do))
       cmds = ['binding.break', pre, do_expr]
主要言語
Ruby
スター
1.3k
フォーク
146
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/debug のほかの issue

ruby/debug の issue をすべて見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。