Do you know Ruby's official debugger ruby/debug
provides out-of-box integration with VSCode? If you haven't tried it yet or having difficulty making it work, I hope this short post will help you set it up.
Basic Setup
- Install the VSCode rdbg extension in VSCode
- Create the
launch.json
file- Click
Run and Debug
button on the left side - Click
create a launch.json file
- this is quite small and underTo customize Run and Debug
- Save the created
launch.json
- Click
- Put
gem "debug", require: false
in yourGemfile
and runbundle install
Debug Simple Ruby Script
If you want to debug a simple Ruby script, you can follow these steps.
Debug Rails/Web Applications
If you want to debug a Rails/web application, do these instead:
- Open the files in VSCode and add some breakpoints.
- Start your program with the
rdbg
executable -bundle exec rdbg --open -n -c -- bundle exec rails s
-
--open
or-O
means starting the debugger in server mode -
-n
means don't stop at the beginning of the program, which is usually somewhere at rubygems, not helpful -
-c
means you'll be running a Ruby-based command
-
- Go back to VSCode's
Run and Debug
panel, you should see a grean play button - Click the dropdown besides the button and select
Attach with rdbg
- Click the play button
- It should now connect the VSCode to the debugger
- If it stops at somewhere in your web server (like puma). Hit
continue
orF5
. This will be resolved in the next1.6.0
release.
- If it stops at somewhere in your web server (like puma). Hit
- Send some requests and it should stop at your breakpoints
Video
I also have built this repl.it
to let you try out ruby/debug
's console commands in your browser (it'll only take you 5 minutes).
If you want to see more articles/tips about the powerful features ruby/debug
has, you can follow me on Twitter 🙂
Top comments (4)
Unfortunately it's not working for me. I'm always getting this error message:
connect ECONNREFUSED /var/folders/mg/41n1z6t54rz93p2pfmr_ccs00000gn/T/ruby-debug-sock-501/ruby-debug-reiz-63528
Any idea what's the problem?
Same here
Thanks, it worked like a charm!
I'm getting this error message:
XError of failed request: BadName (named color or font does not exist)
Major opcode of failed request: 140 (RANDR)
Minor opcode of failed request: 16 (RRCreateMode)
Serial number of failed request: 36
Current serial number in output stream: 36
Are there someone who faced this problem too?