Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

  • How can you launch Ruby on Rails using the built-in Visual Studio Code Launch/Debug features?

  • How do you fix the Debugger terminal error: Process failed: spawn rdebug-ide ENOENT error?

like image 508
janniks Avatar asked Aug 07 '18 08:08

janniks


1 Answers

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?
like image 159
janniks Avatar answered Sep 19 '22 07:09

janniks