ruby 如何使用Visual Studio代码调试Rackup应用程序

dauxcl2d  于 2023-02-03  发布在  Ruby
关注(0)|答案(1)|浏览(164)

这个问题以前有人问过然而,我还没有找到解决方案,我想出了其他任何地方后,张贴的问题。
从Visual Studio Code调试Rack应用程序(当然,它是用Ruby编写的)是有问题的,因为Rack实际上是一个bash脚本,rebornix.Ruby调试器不知道如何处理它。
我已经找到了许多如何执行此操作的示例(here is one),但我遇到了如下错误:

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

                    ^

不调试时,我使用bundler启动Rack应用:

$ bundle exec rackup

这是我的vscode启动配置:

{
      "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,
    },

上述启动配置的问题在于rbenv shim是bash脚本。但是,我希望避免在启动配置中硬编码gem的路径,因为当项目中使用的gem更新时,这将破坏启动配置。
Gemfile为:

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
fae0ux8s

fae0ux8s1#

rackup手动创建的binstub被证明是前进的方向。下面是我的,保存在${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')

一旦我将启动配置修改为以下内容,上面的binstub就可以正常工作:

{
      "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,
    },

问题解决了!手动创建的binstub应该在更新到rackup后仍然有效,不需要任何修改。

相关问题