Debugging Ruby locally in VSCode using ruby-debug and native breakpoints
27 September, 2019 - 2 min read
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!