DEV Community

Natalia Sergeeva
Natalia Sergeeva

Posted on

Rbenv: Love at first sight while setting up VSCode debugger for RSpec

I had been using RVM to manage Ruby versions since I started my Ruby (and Rails) journey far in 2011. I was pretty happy with it. It did the job well and I didn't even think of trying something else. Even in complex environments with many different MRI and JRuby versions. Until now...

I use VSCode for coding. It does what I need and it's pretty comfortable to work with (don't blame me, vim Adventists, I like it too, but...). Previously I worked with C# and Microsoft Visual Studio, and I loved its built-in debugger. I use pry and console to debug Ruby code mostly but VSCode has a debugger too. So a couple of days ago, I decided to give it a try. It worked fine on my machine with a couple of MRI Ruby versions installed via RVM but I failed to run it on my office machine with MRI mixed with jRuby of all sorts and colours. I just wanted to run a debugger on a single spec. It sounds to be easy but it wasn't. Every time I fix one issue with paths, gems, whatever, another pops up. I was about to give up.

Rbenv came for the rescue. I've heard about it before but I didn't realise how cool this tool actually is. It so simple and easy to install and use, and it offers shims which make setting up environments work like a charm. Add configuration for a debugger once, and then just pick any ruby version and everything works with no configuration changes needed. It turned out to be that simple!

Add to Gemfile:

group :development, :test do 
  gem "ruby-debug-ide"

  platform :jruby do
    gem "ruby-debug-base"
  end

  platform :ruby do
    gem "debase"
  end
end

Install Rbenv, Ruby and gems:

brew install rbenv
rbenv init
rbenv install 2.6.3
rbenv local 2.6.3
rbenv rehash
gem install bundler 
bundle install

And then add configuration in VSCode:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug RSpec - open spec file on a certain line",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "useBundler": true,
      "pathToBundler": "<home dir>/.rbenv/shims/bundler",
      "showDebuggerOutput": false,
      "pathToRDebugIDE": "<home dir>/.rbenv/shims/rdebug-ide",
      "program": "<home dir>/.rbenv/shims/spec",
      "args": [
        "${file}:${lineNumber}"
      ]
    }
  ]
}
// You can find paths using `which` command in terminal: `which bundler`, etc

Set breakpoints, open spec file, select spec, and press F5. It works like a charm for me. No dances with a tambourine. Am I happy? Definitely, I am! I'm in love with Rbenv now and will I suggest everybody who is not using it yet to give it a try. You'll like it!

Top comments (0)