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
For Ruby v1.9.x
gem install ruby-debug-ide
gem install ruby-debug-base19x
For Ruby v2.x
gem install ruby-debug-ide
gem install debase
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.
Select Ruby
from the new dropdown menu. This will create a .vscode
folder with a launch.json
file in your current directory.
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.
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:
Happy debugging!
Top comments (21)
There are more here debugging-Ruby-on-Rails for jRuby, Docker, etc.
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.
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!
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?
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.
It's not just the theme, the problem really lies with the lexer for tokenization of the Ruby syntax, for example:
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.
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
and submit the jobs as follows so that the workers perform task posted.
Actually I cannot even run the Rake example given in the example configurations page.
I would appreciate any help, pointer, direction!
Best,
halil
@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!
Add to docker-compose ports:
This is my config to directly debug without run command
rdebug-ide file.rb
everytime: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...
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.
Thanks Dāvis
It's working awesome now.
Do you manage to debug .html.erb file? How do you solve that?
In the last version it's simply work. Just add a breakpoint and the debugger will stop there.
How would one debug a jekyll site that is usually started via
bundle exec jekyll s
?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.
Nice article. You have a idea to get autocomplete on ctrl + space?
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.
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.
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.