LoginSignup
0

VS Code RSpec デバッグ環境

Last updated at Posted at 2023-01-24

VS Code のデバッグ機能で RSpec を実行したい。

  • F5 押下で Spec を実行できる
  • ブレイクポイントを指定しステップインしたり
  • 変数の中身を確認したりできる

下記を参考にほしい環境を構築できました。

手順

環境

  • Ruby 2.7.2
  • Rails 6.0.6
  • Bundler version 2.1.4

つまずいた箇所

つまずいた箇所もあったので残しておきます。

RSpec のパスを手順通りに書き換えてしまっている

手順では which rspec で実行ファイルのパスを取得して、launch.json 内のパスを書き換えるように指示しています。

❯ which rspec
/Users/user_name/.rbenv/shims/rspec

これをそのままやってデバッグを実行した結果、次のエラーが発生しました1

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

どうやら which rspec で参照されるファイルは rbenv が用意したラッパースクリプトで、Ruby で実行可能なファイルにはなっていないのが原因のようでした2

エラー解消のために RSpec の binstub を作成します。

bundle binstubs rspec-core

これによりプロジェクト配下の /bin 配下にスクリプトが生成されます。
/bin/rspec とすれば Gemfile.lock で指定されたバージョンの RSpec が実行されます。

環境変数を設定できていない

デバッグ実行環境に環境変数を渡すには launch.json に記載しておかねばなりません。
VS Code のアクティブなターミナルで環境変数を設定していてもデバック環境には渡せないです。

launch.jsonenv プロパティを用意すると設定できます。

(いずれは envrc を使っていい感じに渡せるようにしたいです)

最終的な設定内容

launch.json は最終的には以下のようになりました。

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Start Rails server",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "program": "${workspaceRoot}/bin/rails",
      "env": {
        "TEST_ENV": "hoge",
      },
      "args": [
        "server",
        "-p",
        "3000"
      ]
    },
    {
      "name": "Debug Rails server",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "useBundler": true,
      "pathToBundler": "/Users/user_name/.rbenv/shims/bundle",
      "pathToRDebugIDE": "/Users/user_name/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ruby-debug-ide-0.7.3",
      "program": "${workspaceRoot}/bin/rails",
      "env": {
        "TEST_ENV": "hoge",
      },
      "args": [
        "server",
        "-p",
        "3000"
      ]
    },
    {
      "name": "Run RSpec - all",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "program": "${workspaceRoot}/bin/rspec",
      "env": {
        "TEST_ENV": "hoge",
      },
      "args": [
        "--pattern",
        "${workspaceRoot}/spec/**/*_spec.rb"
      ]
    },
    {
      "name": "Debug RSpec - open spec file",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "useBundler": true,
      "pathToBundler": "/Users/user_name/.rbenv/shims/bundle",
      "pathToRDebugIDE": "/Users/user_name/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ruby-debug-ide-0.7.3",
      "debuggerPort": "1235",
      "program": "${workspaceRoot}/bin/rspec",
      "env": {
        "TEST_ENV": "hoge",
      },
      "args": [
        "${file}"
      ]
    },
    {
      "name": "Debug RSpec - open spec file on a certain line",
      "type": "Ruby",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "useBundler": true,
      "pathToBundler": "/Users/user_name/.rbenv/shims/bundle",
      "pathToRDebugIDE": "/Users/user_name/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/ruby-debug-ide-0.7.3",
      "debuggerPort": "1235",
      "program": "${workspaceRoot}/bin/rspec",
      "env": {
        "TEST_ENV": "hoge",
      },
      "args": [
        "${file}:${lineNumber}"
      ]
    }
  ]
}

ちなみに

Ruby 3.1 以降は新しく導入された debug.gem を使う方がよさそうです。

VS Code の拡張機能も用意されています。

  1. https://qiita.com/pldb/items/6ee58582c47b60cc499f と全く同じ事象です

  2. https://qiita.com/NasuPanda/items/975328eddf4ea22465f8 が参考になりました

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0