How to Run and Debug Ruby on Rails from Visual Studio Code

How do you run and debug Ruby on Rails from Visual Studio Code?

Setup and Launch

  1. Install the VS Code Ruby plugin (hit ++P on macOS or ctrl++P elsewhere and type ext install in the prompt, then search for ruby)
  2. Install some required Ruby gems
gem install ruby-debug-ide
gem install debase

  1. Add a launch configuration in Visual Studio Code (example configuration shown below)
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/bin/rails",
"env": {
"PATH": "YOUR_PATH_HERE",
"GEM_HOME": "YOUR_GEM_HOME_HERE",
"GEM_PATH": "YOUR_GEM_PATH_HERE",
"RUBY_VERSION": "YOUR_RUBY_VERSION_HERE"
},
"args": [
"server"
]
}

In some cases you might not need to specify the env section.
In other cases you can launch VS Code using the CLI (i.e. from the terminal), which on some systems automatically sets the correct environment variables.


  1. Run!


Troubleshooting

If you get the following error

Debugger terminal error: Process failed: spawn rdebug-ide ENOENT

Your environment variables (env) are most likely not set and the plugin cannot find the necessary binaries.

  1. Make sure all gems are installed and try running bundler install --binstubs if you use bundler.
  2. Make sure the env section is set in your launch configuration. Run the following shell command to generate your env:
printf "\n\"env\": {\n  \"PATH\": \"$PATH\",\n  \"GEM_HOME\": \"$GEM_HOME\",\n  \"GEM_PATH\": \"$GEM_PATH\",\n  \"RUBY_VERSION\": \"$RUBY_VERSION\"\n}\n\n"


Windows

Make sure to use the correct spelling (and capitalization) of the path variable, i.e. Path on Windows



Sources:

  • https://github.com/rubyide/vscode-ruby/issues/214#issuecomment-393111908
  • https://www.reddit.com/r/vscode/comments/5w1acs/getting_error_debugger_terminal_error_process/
  • How to extend $PATH in launch.json in Visual Studio Code?

Configure debug in vscode to Ruby on Rails and Docker

Looks like you have some funny encoding on line 3 of your start.sh file that isn't being interpreted as a comment, which is likely causing the commented line to actually start a server.

When the bundle exec rdebug-ide --host 0.0.0.0 --port 1234... command executes, it throws the 'port in use' error because there is already a server running on port 1234 from the first command.

If you remove the commented-out lines you should be good to go.

How to debug Ruby code on Visual Studio Code?

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.



Related Topics



Leave a reply



Submit