3

I’m using Rails 4.2. I start Rails using foreman, and I would like to attach a debugger when starting Rails. This question details the process for Rails 3.2 — How to debug a rails (3.2) app started by foreman?, however I believe this file is outdated

$ cat config/initializers/start_debugger.rb
# Enabled debugger with foreman, see https://github.com/ddollar/foreman/issues/58
if Rails.env.development?
  require 'debugger'
  Debugger.wait_connection = true

  def find_available_port
    server = TCPServer.new(nil, 0)
    server.addr[1]
  ensure
    server.close if server
  end

  port = find_available_port
  puts "Remote debugger on port #{port}"
  Debugger.start_remote(nil, port)
end

Since I don’t think the “debugger” gem is supported by Rails 4.2. How would I start Rails using foreman on a dedicated debug port using Rails 4.2?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • have you tried byebug? https://github.com/deivid-rodriguez/byebug/blob/master/GUIDE.md#debugging-remote-programs – Saiqul Haq Oct 06 '21 at 13:59
  • I'd be open to this, but the link doesn't talk about how I would integrate this so that I could start Rails via foreman and enable the debug port. Starting Rails via foreman is a requirement of our project. – Dave Oct 08 '21 at 01:51

1 Answers1

1

based on your comment, you said that you open to use byebug so this is the guide to set it up for Foreman

edit config/environments/development.rb

require "byebug/core"
port = ENV['DEBUGGER_PORT'] # any port that you like
Byebug.wait_connection = true
Byebug.start_server("localhost", port)

let say you would like to debug your controller

def index
  @posts = Post.all
end

and you want to see @posts value then just add byebug after the @posts

def index
  @posts = Post.all
  byebug
end

and do request to your #index endpoint rails server will be paused on byebug line you need to execute byebug -R localhost:8899 in another terminal


if you see this one https://github.com/deivid-rodriguez/byebug/blob/master/lib/byebug/remote/server.rb#L30 . it uses TCPServer too. so it's similar to your own implementation.

your foreman setting can read ENV['DEBUGGER_PORT'] too

Saiqul Haq
  • 2,287
  • 16
  • 18
  • Thanks, but again this doesn't tell me how to expose a debug port when Rails starts with foreman. Also I intend to debug using VSCode extensions and would prefer not to add code into Rails specifically for debugging, as I understand VS Code would allow me to set breakpoints and debug directly in the IDE – Dave Oct 08 '21 at 18:28
  • see my updated post – Saiqul Haq Oct 09 '21 at 01:12