Home > OS >  How to Debug a Rackup App With Visual Studio Code
How to Debug a Rackup App With Visual Studio Code

Time:02-02

This question has been asked before; however, I have not found the solution that I came up with anywhere else after posting the question.

Debugging a Rack app (which is, of course, written in Ruby) from Visual Studio Code is problematic because Rack is actually a bash script, and the rebornix.Ruby debugger does not know how to handle it.

I have found many examples of how to do this (here is one), but I experienced errors like:

Fast Debugger (ruby-debug-ide 0.7.3, debase 0.2.5.beta2, file filtering is supported) listens on 127.0.0.1:1234

Uncaught exception: /home/mslinn/.rbenv/shims/rackup:3: syntax error, unexpected string literal, expecting `do' or '{' or '('
[ -n "$RBENV_DEBUG" ] && set -x
     ^
/home/mslinn/.rbenv/shims/rackup:3: syntax error, unexpected ']', expecting end-of-input
[ -n "$RBENV_DEBUG" ] && set -x

                    ^

When not debugging, I launch my Rack app with bundler:

$ bundle exec rackup

This was my vscode launch configuration:

    {
      "args": [
          "-p", "9292",
          "-E", "development"
      ],
      "cwd": "${workspaceRoot}",
      "name": "Debug rackup application",
      "pathToBundler": "${userHome}/.rbenv/shims/bundle",
      "pathToRDebugIDE": "${userHome}/.rbenv/shims/rdebug-ide",
      "program": "${userHome}/.rbenv/shims/rackup",
      "request": "launch",
      "showDebuggerOutput": true,
      "type": "Ruby",
      "useBundler": true,
    },

The problem with the above launch configuration is that the rbenv shims are bash scripts. They invoke gems; however, I want to avoid hard-coding the path to the gems in the launch configuration because that will break the launch configuration when the gems used in the project are updated.

Gemfile is:

source 'https://rubygems.org'

gem 'data_mapper'
gem 'dm-sqlite-adapter'
gem 'sinatra', '>=3.0.5'
gem 'sinatra-flash'
gem 'warden'

gem 'debase', require: false
gem 'rackup', require: false
gem 'ruby-debug-ide', require: false
gem 'rubocop', require: false
gem 'rubocop-rake', require: false
gem 'rubocop-rspec', require: false
gem 'shotgun', require: false
gem 'thin', require: false

CodePudding user response:

A manually created binstub for rackup turned out to be the way forward. Here is mine, saved in ${workspaceRoot}/bin/rackup:

#!/usr/bin/env ruby

# Prepares the $LOAD_PATH by adding to it lib directories of all gems in the
# project's bundle:
require 'bundler/setup'

load Gem.bin_path('rack', 'rackup')

The above binstub worked once I modified the launch configuration to the following:

    {
      "args": [
          "-p", "9292",
          "-E", "development"
      ],
      "cwd": "${workspaceRoot}",
      "name": "Debug rackup application",
      "pathToBundler": "${userHome}/.rbenv/shims/bundle",
      "pathToRDebugIDE": "${userHome}/.rbenv/shims/rdebug-ide",
      "program": "${workspaceRoot}/bin/rackup",
      "request": "launch",
      "showDebuggerOutput": true,
      "type": "Ruby",
      "useBundler": true,
    },

Problem solved! The manually created binstub should survive updates to rackup, without requiring any modifications.

  • Related