Skip to content

Instantly share code, notes, and snippets.

@BobbyMcWho
Last active September 27, 2019 21:26
Show Gist options
  • Save BobbyMcWho/6cda0d7742040aa32e526df892e522bf to your computer and use it in GitHub Desktop.
Save BobbyMcWho/6cda0d7742040aa32e526df892e522bf to your computer and use it in GitHub Desktop.
Debugging Ruby locally in VSCode using ruby-debug and native breakpoints

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 rspec 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment