DEV Community

Dāvis Namsons
Dāvis Namsons

Posted on

Debugging Ruby in VS Code

Although binding.pry is adequate for simple debugging purposes, I find that for cases when I have to quickly see multiple variable and object values, it becomes time consuming and messy. Thus I
use vscode's built-in debugging features with the ruby debug ide interface. Here's how to set it up:

Install the required gems

For JRuby or Ruby v1.8.x

gem install ruby-debug-ide
gem install ruby-debug-base
Enter fullscreen mode Exit fullscreen mode

For Ruby v1.9.x

gem install ruby-debug-ide
gem install ruby-debug-base19x
Enter fullscreen mode Exit fullscreen mode

For Ruby v2.x

gem install ruby-debug-ide
gem install debase
Enter fullscreen mode Exit fullscreen mode

Create a launch.json file

If you don’t have it yet, install the Ruby extension and reload the editor.

In vscode, open the debugging tab and click on the dropdown, then choose the Add configuration... option.

selecting 'Add Configuration in the debug view'

Select Ruby from the new dropdown menu. This will create a .vscode folder with a launch.json file in your current directory.

Selecting Ruby as the configuration

Launch a debugging session

Now you can run the rdebug interface in your terminal:

rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 /path/to/the/file.rb
(In case you need to change the port, remember to specify it in launch.json as well)

If you're trying to debug a Rails app, you should instead specify the path of the rails script in your project with the server command. The script is usually located in the bin directory of your project, in which case you would write it as bin/rails server.

After the interface is running in the terminal, you can start the debugging session. In the debug tab choose Listen for rdebug-ide from the dropdown menu. Click the start button.

selecting rdebug

Your debugging session should be running. You can now hit your set breakpoints and use the debugging console to help analyse your Ruby code.

Here's an example:

debugging preview

Happy debugging!

Discussion (20)

Collapse
karuppasamy profile image
Karuppasamy M

There are more here debugging-Ruby-on-Rails for jRuby, Docker, etc.

Collapse
dnamsons profile image
Dāvis Namsons Author • Edited

I was about to expand this article on launching the debugger directly from VS Code(instead of attaching it) but this is way more detailed than what I would've written, nice work!
You could also contribute this to the vscode-ruby extention's debug guide(launching the debugging session and the example configurations), otherwise this information is quite hard to find.

Collapse
andy profile image
Andy Zhao (he/him)

this information is quite hard to find.

Yeah,no kidding. I spent half a day looking how to set up my VS Code for Ruby and Rails, and it's currently still pretty suboptimal compared to Atom. Looking forward to that language server from the Ruby gem though!

Thread Thread
dnamsons profile image
Dāvis Namsons Author • Edited

There seems to be a lack of people interested in developing extensions for Ruby in the VS Code community. I am currently learning some TypeScript to remedy that a bit as it seems like most Ruby extensions are slowly dying from the lack of contributions. I really like VS Code, it really is a shame that Microsoft isn't maintaining Ruby like they are maintaining Python/Go/etc. I might even go over to being a vim purist if this goes on(let's hope not)
What features are you lacking compared to atom if i may ask?

Thread Thread
andy profile image
Andy Zhao (he/him)

Totally agree with the sentiment that there's a lack of interest in developing Ruby extensions.

I'm not too sure which features I miss from Atom since it's been almost a year since I last used it. I think my major issue with VS Code + Ruby is the slow development of features I expect to be working, like Solargraph or proper syntax highlighting. The latter might be an issue with my theme though.

Since you mentioned vim, I wonder what most Rubyists use for their text editor.

Thread Thread
dnamsons profile image
Dāvis Namsons Author • Edited

It's not just the theme, the problem really lies with the lexer for tokenization of the Ruby syntax, for example:

def method(argument)
  puts argument
end

method argument

This is a valid and widely used way to call a method with just one argument(if I'm not mistaken, RuboCop's default config even throws a warning if parentheses are used), but as you can see, it doesn't get highlighted correctly.

I stumbled across this research by JetBrains a few days ago, it has the statistics of preferred editors for Ruby developers amongst other things.

Collapse
senhalil profile image
senhalil

Hello Davis and Mariappan,
Thanks for both of the articles. I read all the guides I can find on internet and still couldn't figure out how to debug within VSCode when ruby code is run over a server generated with rake.

More specifically, we generate our server like this

bundle exec rake server

COUNT=5 QUEUE=* bundle exec rake resque:workers

and submit the jobs as follows so that the workers perform task posted.

curl -X POST --header "Content-Type:application/json" --data @data.json localhost:port/submit.json?api_key...

Actually I cannot even run the Rake example given in the example configurations page.

I would appreciate any help, pointer, direction!

Best,
halil

Collapse
artoodeeto profile image
aRtoo

@dnamsons hello. I know its late but can you help me? so I just did this.

rdebug-ide --host 0.0.0.0 --port 1234 /path/to/the/file.rb without adding the dispatcher because im getting an error if I try to attached.

2627: connection failed(1)
Exception: Connection refused - connect(2) for "127.0.0.1" port 26162

without dispatcher im running fine but when I added breakpoint it doesn't pause on break points. please. thank you sir!

Collapse
sjdonado profile image
Juan Rodriguez

Add to docker-compose ports:

      - 26162:26162
Enter fullscreen mode Exit fullscreen mode
Collapse
pancutan profile image
Sergio Alonso

Great article.

By the way. Any of you could debug erb templates, as Rubymine, Byebug and Pry does?

I finished searching here github.com/rubyide/vscode-ruby/iss...

Collapse
dnamsons profile image
Dāvis Namsons Author • Edited

I don't really use ERB, as I use haml(way better than ERB), the breakpoints work for me. However, the funcionality is essentially the same, so breakpoints should also work for ERB. If they don't, try following the debugging instructions provided by Karuppasamy M in the comment above.

Collapse
pancutan profile image
Sergio Alonso

Thanks Dāvis
It's working awesome now.

Thread Thread
ramandika profile image
ramandika

Do you manage to debug .html.erb file? How do you solve that?

Thread Thread
pancutan profile image
Sergio Alonso

In the last version it's simply work. Just add a breakpoint and the debugger will stop there.

Collapse
yuritoledo profile image
Yuri Toledo

Nice article. You have a idea to get autocomplete on ctrl + space?

Collapse
dnamsons profile image
Dāvis Namsons Author • Edited

This is currently the best solution - Solargraph.
A Language server is under development for the Ruby extension as well, so eventually it might be better for auto completion. When it comes out, I'll probably write a post about it.

Collapse
yuritoledo profile image
Yuri Toledo

I used the solargraph and it was the only solution I found.

Do you have any problems developing with Ruby (on Rails)? Because of the implicit nature, it is difficult for me (padawan) to know where something happens or where it goes.

Thread Thread
dnamsons profile image
Dāvis Namsons Author • Edited

Well, I wouldn't worry about it. Rails' convention over configuration philosophy is a bit weird when you're starting out, as it does a lot for you and so it can be somewhat difficult in the beginning. However, if you keep at it, you'll get better soon. Most principles and best practices can be learned in the official Ruby on Rails guide(Rails functionality) and the Railscasts(for gems and software development side of things) pages, they are the gold standard as far as free learning resources for Rails go.

Collapse
pke profile image
Philipp Kursawe

How would one debug a jekyll site that is usually started via bundle exec jekyll s?

Collapse
karatedog profile image
Földes László • Edited

Is there any way to start the application from within VSCode instead of changing to terminal to start the app? Debugging stuff in ye' olde' Turbo Pascal IDE was 10 times easier than trying to debug Ruby code 25 years later. I could write an infinite loop that starts the app as soon as it has finished but that's super ugly.