binding.pry not works with command bin/dev

2.1k Views Asked by At

binding.pry not works(console input not available) if i start the server with bin/dev command. It only works with bin/rails s command. I understand it has something to do with foreman and Procfile.dev, but I don't know how. Is this a bug or is it supposed to be like this?

3

There are 3 best solutions below

1
small aaron On BEST ANSWER

With bin/dev, the Procfile.dev file is run with foreman. The pry issue is caused by the CSS and JS watchers:these just listen to changes in your CSS and JS files.

What you can do is remove the web: unset PORT && bin/rails server command from your Procfile, so it will only have the CSS and JS watchers and look like this:

js: yarn build --watch
css: yarn build:css --watch

Now you'll have to open two terminals, one with bin/rails s and the other with foreman start -f Procfile.dev. This way your pry works in the server terminal as normal and the watchers are watching as normal.

2
JP Silvashy On

A popular approach is to use pry-remote which is useful in some ways I didn't expect: https://github.com/Mon-Ouie/pry-remote.

pry-remote opens a remote terminal, so where you drop your debugger line (binding.remote_pry in the case of pry-remote), you'll open another terminal and enter the debugging session by calling pry-remote in your shell.

I get this is a bit more complicated, I got used to it, and was able to get away from the noise logged by other process with Foreman.

1
suupic On

I use rdbg + vscode remote debug and it works fine.

Update Procfile.dev like this to run rdbg with rails server and set a socket file

web: rdbg --command --nonstop --open=vscode --sock_path=tmp/sockets/rdbg.socket -- bundle exec bin/rails server

Install VSCode Ruby rdbg Debugger, configure launch.json like this

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to rdbg Process",
      "type": "rdbg",
      "request": "attach",
      "debugPort": "${workspaceRoot}/tmp/sockets/rdbg.socket",
    },
  ]
}

Now, VSCode will launching with debugger enable while you run bin/dev, or you can run debugger in vscode by F5 anytime