【问题标题】:How to get RSpec working with debugger IDE in VSCode如何让 RSpec 在 VSCode 中使用调试器 IDE
【发布时间】:2020-05-22 18:08:26
【问题描述】:

尽管我花了大约三个小时试图让这个工作,但我无法在我的 RSpec 的生命周期中使用 VSCode 上的调试器。我可以让 Rspec 在 VSCode 上的 terminal 中运行,但这并没有为我提供 IDE 的任何用于检查和单步执行的调试功能。

这是我的 launch.json 文件中的内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run RSpec - all",
            "type": "Ruby",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "/Users/my-home-dir/.rvm/gems/ruby-2.6.5@project-gemset-name/wrappers/rspec",
            "pathToRDebugIDE": "/Users/my-home-dir/.rvm/gems/ruby-2.6.5@project-gemset-name/gems/ruby-debug-ide-0.7.2",
            "args": [
                "--pattern",
                "${workspaceRoot}/spec/**/*_rspec.rb"
            ]
        }
    ]
}

我的 gemfile 包含:

  gem 'ruby-debug-ide', '~>0.7.2'
  gem 'debase', '~>0.2.4.1'

我感觉这些错误可能是由于 RVM 和 VSCode 之间的不兼容而导致的,但我不知道如何解决这个问题。

这里的所有设置都是按照 Microsoft 配方进行的:https://github.com/Microsoft/vscode-recipes/tree/master/debugging-Ruby-on-Rails

每次运行此设置时,我都会在调试控制台中收到以下错误:

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

有没有办法让它运行?还有什么方法可以让它使用保护让它自动运行?

【问题讨论】:

标签: ruby-on-rails visual-studio-code rspec vscode-debugger


【解决方案1】:

我让它工作了。不幸的是,我不使用 RVM。所以,我的解决方案涉及 rbenv。无论如何我都会在这里分享它,以防它对你或其他人有帮助。

which rspec 向我指出了 rbenv 用来执行安装在当前 Ruby 版本下的 rspec 版本的 shim(shell 脚本)。当我使用该路径配置launch.json 时,rdebug-ide 不喜欢 shim。我认为它期待可执行文件。

所以,我运行rbenv which rspec 并获得了可执行文件的实际路径。一旦我将其插入launch.json,它就可以正常工作了。当然,如果我更改正在运行的 Ruby 版本,我必须更新文件以指向安装在新版本 Ruby 下的 RSpec 版本。

鉴于 Ruby 版本管理器在社区中的流行,我认为 ruby​​-debug-ide 会考虑这一点。也许值得在他们的 GitHub 上发布一个问题:https://github.com/ruby-debug/ruby-debug-ide

【讨论】:

  • 这让我想起了早期的网络,当时所有的曲目都做得很差。可悲的是,这对我没有用,虽然我很高兴它对你有用。
  • 你能像下面我的回答那样使用 GEM_HOME 吗?
【解决方案2】:

我添加了一个 preLaunchTask 来启动 rdebug-ide 服务器,然后在附加调试器之前寻找一个模式。

我使用焦点标志来控制运行或不运行哪些测试。能够在主脚本文件和 *_spec.rb 文件中设置断点。

宝石文件:

group :test, :development do
  gem "ruby-debug-ide", "~> 0.7.2"
  gem "debase", "~> 0.2.4"
end

launch.json:

{     
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for rdebug-ide rspec",
            "type": "Ruby",
            "request": "attach",
            "remoteHost": "127.0.0.1",
            "remotePort": "1234",
            "preLaunchTask": "run-rdebug-for-rspec",
            "remoteWorkspaceRoot": "${workspaceRoot}",
            "cwd": "${workspaceFolder}"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [{
        "label": "run-rdebug-for-rspec",
        "command": "bundle",
        // could update these are you see fit 
        // my tests were located in /spec folder
        "args": [
          "exec","rdebug-ide",
          "--host","0.0.0.0",
          "--port","1234",
          "--dispatcher-port","26162",
          // needed to specify the full path of bundle
          // can be found by `which bundle`
          "--", "/usr/local/bin/bundle", "exec", "rspec", "spec/"
        ],
        "type": "shell",
        "isBackground": true,
        // this will look for a pattern to attach to rdebug server
        // attaching to the server will start running the tests
        "problemMatcher": [
            {
              "pattern": [
                {
                  "regexp": ".",
                  "file": 1,
                  "location": 2,
                  "message": 3
                }
              ],
              "background": {
                "activeOnStart": true,
                // once server is up and running it'll display something like:
                // Fast Debugger (ruby-debug-ide 0.7.2, debase 0.2.4.1, file filtering is supported) listens on 0.0.0.0:1234
                "beginsPattern": "^Fast Debugger.*1234$",
                "endsPattern": ".",
              }
            }
          ],
        // open up a new task window everytime
        // if set to default `shared` then previous task window needs to be closed 
        // otherwise, was having issues getting patternMatcher to work
        // and for rdebug to attach within a shared terminal on successive runs, or restarts. 
        "presentation": {
          "panel": "new"
        }
    }]
}

【讨论】:

    【解决方案3】:

    虽然上面的答案可能有效,但一旦升级 ruby​​ 版本就会出现问题,因为路径是硬编码的。

    这是一种不那么脆弱的方法,应该能够在版本升级中毫无问题地存活。

    我使用的是 ruby​​ 3.0.3,由 mac osx 上的 rvm 管理。

    Gemfile 包括:

    group :development do 
      gem 'ruby-debug-ide'
      gem 'debase', "~> 0.2.5.beta2"
    end
    

    (debase 的稳定版本不支持 ruby​​ v3,因此是 beta)

    启动配置使用 GEM_HOME 环境变量来指定 rspec 二进制文件(vscode 默认配置出于某种原因将其放在工作区根目录中):

       {
            "name": "RSpec - all",
            "type": "Ruby",
            "request": "launch",
            "program": "${env:GEM_HOME}/bin/rspec",
            "args": [
                "-I",
                "${workspaceRoot}"
            ]
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-07
      • 2020-08-18
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多