Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Ruby code on Visual Studio Code?

Tags:

I am new to VSCode, however, I did download the required extensions as I read in a website. So far, I can't debug Ruby on VSCode, and I am not sure where the problem lies. It just don't start... I don't think it considers the code as Ruby. Whenever I try to run the code, I see "downloading C# extensions..." in the debugging window. Which is odd of course. Any help?

like image 534
Rou Avatar asked Jul 26 '18 13:07

Rou


People also ask

How do I debug a Ruby program?

To help deal with bugs, the standard distribution of Ruby includes a debugger. In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.


1 Answers

This is actually fairly easy once you follow the right steps. First, you have to download Ruby Extension which is available on vs code marketplace or simply via Extension tab in VS code itself: just search for Ruby, install it, and reload VS Code [update: vscode can perfectly load new extensions without needing a reload]. secondly, you have to follow the debugging guide for this extension which is available on the GitHub link I provided or in vs code marketplace. Here's the section you would be most interested in, but you could also see the original wiki:

Install Ruby Dependencies

In this extension, we implement ruby debug ide protocol to allow VS Code to communicate with ruby debug, it requires ruby-debug-ide to be installed on your machine. This is also how RubyMine/NetBeans does by default.

  • If you are using JRuby or Ruby v1.8.x (jruby, ruby_18, mingw_18), run

    gem install ruby-debug-ide 

The latest version is 0.6.0. Make sure ruby-debug-base is installed together with ruby-debug-ide.

  • If you are using Ruby v1.9.x (ruby_19, mingw_19), run

    gem install ruby-debug-ide 

The latest version is 0.6.0. Make sure ruby-debug-base19x is installed together with ruby-debug-ide.

  • If you are using Ruby v2.x

    gem install ruby-debug-ide -v 0.6.0 (or higher versions) gem install debase -v 0.2.1 (or higher versions) 

Add VS Code config to your project

Go to the debugger view of VS Code and hit the gear icon. Choose Ruby or Ruby Debugger from the prompt window, then you'll get the sample launch config in .vscode/launch.json. The sample launch configurations include debuggers for RSpec (complete, and active spec file) and Cucumber runs. These examples expect that bundle install --binstubs has been called. Detailed instruction for debugging Ruby Scripts/Rails/etc

Read following instructions about how to debug ruby/rails/etc locally or remotely

01 Debugger installation

02 Launching from VS Code

03 Attaching to a debugger

04 Running gem scripts

05 Example configurations

if you follow these steps, you will have every dependency installed in Step 1. Step 2 helps you config your project workspace to start debuging codes written in ruby. by finishing step 2, you should be able to start debugging. here is a simple config that I use in my recent ruby project to simply debug the current open file. this is fully explained in the second step I linked

{    "name": "Debug Local File",    "type": "Ruby",    "request": "launch",    "cwd": "${workspaceRoot}",    "program": "${file}" } 

"program": "${file}" is the line that enables debugging the current open file.

like image 102
Mahdad Baghani Avatar answered Sep 30 '22 20:09

Mahdad Baghani