0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】VSCode+Ruby LSPでDockerコンテナのRailsアプリをデバッグ

Last updated at Posted at 2024-12-14

はじめに

DockerコンテナでRailsアプリを開発している環境で、VSCodeのRuby LSPとデバッグ接続するのにえらい苦労したのでメモ。

基本的なことまで書くとごちゃごちゃするのでポイントだけ記載。ある程度VSCodeとRailsを理解している人向け。

環境

  • Windows 11 Home 23H2
    • ruby 3.3.6 (コンテナ側のバージョンと合わせる)
    • VSCode 1.96.0
      • Ruby LSP 0.8.16
    • Docker Desktop 4.35.1
      • ruby 3.3.6
        • gem rails 8.0.0
        • gem debug 1.9.2

設定

コンテナ側

  • 環境変数に以下を追加。
RUBY_DEBUG_LOCAL_FS_MAP=リモートパス:ローカルパス

パスにはRailsのルートディレクトリ(Gemfileが置いてあるディレクトリ)の絶対パスを指定。
リモートパスにはコンテナのパス、ローカスパスにはWindowsのパスを指定。先頭のC:は付けない。
私の環境だと以下のような指定。

RUBY_DEBUG_LOCAL_FS_MAP=/web:/Users/tamanegisoul/workspace/docker/web
  • コンテナのrailsサーバはデバッグモード(rdbg)で起動。
rdbg -n --open --host 0.0.0.0 --port 12345 -c -- bundle exec rails s -p 3000 -b '0.0.0.0'

VSCode側

  • launch.jsonに以下を追記
{
    "configurations": [
        {
            "type": "ruby_lsp",
            "request": "attach",
            "name": "Attach to a debuggee",
            "debugPort": 12345,
        },
    ]
}

デバッグ方法

VSCodeでAttach to a debuggeeでデバッガを起動。
VSCodeでブレークポイントを設定しても効かない(泣)ので、ソースコードの停止させたい位置に以下を追記。

binding.break

この状態でWebブラウザからアプリを実行すればVSCodeでデバッグできる。やほー。

課題

停止位置のファイルが絶対パス参照の別ウィンドウで開いてしまう(画像赤枠)のが少し残念。
aaa.png
RUBY_DEBUG_LOCAL_FS_MAP${workspaceFolder}を指定してみたが以下のエラーになるので、これはどうしようもない気がする。いつか解決したい。

Could not load source '${workspaceFolder}/app/controllers/users_controller.rb': not found....

参考にしたページ

スペシャルサンクス!
https://github.com/Shopify/ruby-lsp/issues/1765#issuecomment-2348162459
https://zenn.dev/igaiga/books/rails-practice-note/viewer/ruby_rails_debug_gem

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?