DEV Community

Bobby McDonald
Bobby McDonald

Posted on • Updated on • Originally published at blog.rmcd.io

Debugging Ruby locally in VSCode using ruby-debug and native breakpoints

This is one of the biggest pain points I've had developing with ruby on rails and rspec, and I could never get the ruby extension by rebornix to debug properly with breakpoints. Using the ruby-debug extension by the developer of the Solargraph extension, you are now able to debug rspec and ruby scripts locally!

In the extensions marketplace, search "ruby-debug" and install globally. You may need to reload your workspace afterwards.

You'll need to add readapt to your project's gemfile and bundle install.

group :development do 
  # gems...
  gem 'readapt' # Debugging with vscode ruby-debug extension
  # gems...
end

Then add the appropriate configuration either to your global settings.json:

{
"launch": {
  "version": "0.2.0",
  "configurations": [
      {
      "type": "ruby-debug",
      "request": "launch",
      "name": "Ruby",
      "program": "${file}",
      "programArgs": [],
      "useBundler": false
    },
    {
      "type": "ruby-debug",
      "request": "launch",
      "name": "RSpec LocalFile",
      "program": "rspec",
      "programArgs": ["${relativeFile}"],
      "useBundler": true
    },
    {
      "type": "ruby-debug",
      "request": "launch",
      "name": "RSpec Selected Line",
      "program": "rspec",
      "programArgs": ["${relativeFile}:${lineNumber}"],
      "useBundler": true
    }
  ]
}

or your workspace/.vscode/launch.json file

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "ruby-debug",
      "request": "launch",
      "name": "Ruby",
      "program": "${file}",
      "programArgs": [],
      "useBundler": false
    },
    {
      "type": "ruby-debug",
      "request": "launch",
      "name": "RSpec LocalFile",
      "program": "rspec",
      "programArgs": ["${relativeFile}"],
      "useBundler": true
    },
    {
      "type": "ruby-debug",
      "request": "launch",
      "name": "RSpec Selected Line",
      "program": "rspec",
      "programArgs": ["${relativeFile}:${lineNumber}"],
      "useBundler": true
    }
  ]
}

Open an rspec test or plain ol' ruby file, and go to the debug tab. Select the option you want to run when you press F5.

Put a breakpoint in your file and press F5 or click the green arrow shown above to run the test.

Use the debug window at the bottom of your window to debug!

Top comments (3)

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

Do you have any idea if this works with the VS Code remote workspaces feature? My Ruby projects are often inside containers to prevent needing things like rvm to manage gemsets, but that means gems and OS dependencies aren't available locally for tools like this, and the server isn't even running locally.

Collapse
 
bobbymcwho profile image
Bobby McDonald

Hmm, I haven't tried that yet, but vscode does have built in support for devcontainers that might help with what you're looking for. Would you be interested in me exploring that and making a post with some quick finds?

Collapse
 
ryanwjackson profile image
Ryan

Know how this works with gem development (e.g. *.gemspec instead of Gemfile)?