LoginSignup
21
36

More than 5 years have passed since last update.

WindowsでVSCodeを使ってrailsのデバッグを行う

Last updated at Posted at 2018-06-18

ここ最近railsで開発しはじめて約2カ月。Windowsだととても開発しずらい。
MacなんかだとVSCodeでデバッグの方法がいくつも載っているのですが、Windowsだと英語サイトしかないし(しかも質問サイトとか)、よくわからない。
今回はWindowsでrailsのデバッグ方法をまとめました。

環境

Windows10
WSL(Ubuntu上にrailsの環境設定済)
Ruby 2.4.3
VSCode

Railsの前準備

  • デバッグするためdevelopementのgroupに以下の記述を追加
Gemfile
gem 'ruby-debug-ide'
gem 'debase'
  • gemをインストール
bundle install

VSCodeの前準備

WSLをVSCodeのターミナルから実行できるように設定

  • 設定を選択

    image.png

  • 右のタブに以下を追加

"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\bash.exe"

image.png

Rubyの設定

  • 拡張機能にRubyを追加
    image.png

  • F5を押して、Rubyを選択。

  • launch.jsonが新規で作成されるので、そのまま保存する。

  • (2018/07/06追記) デバッグの設定
    remoteWorkspaceRootをローカルパスに変更

launch.json

(省略)
{
  "name": "Listen for rdebug-ide",
  "type": "Ruby",
  "request": "attach",
  "cwd": "${workspaceRoot}",
  "remoteHost": "127.0.0.1",
  "remotePort": "1234",
  "remoteWorkspaceRoot": "/mnt/c/Users/xxxx/Documents/SHIPS" // <-ローカルパス
},
(省略)

デバッグ

  • Ctrl + @で下の方でターミナルを開いて以下のコマンドを実行(Fast Debbuger …が出たらOK)
ターミナル
$ rdebug-ide --host 0.0.0.0 --port 1234 -- ./bin/rails s -b 0.0.0.0 -p 3000
-e development
Fast Debugger (ruby-debug-ide 0.6.1, debase 0.2.2, file filtering is supported) listens on 0.0.0.0:1234
  • デバッグの実行
    デバッグビューに切り替え、「Listen for rdebug-ide」を選択、再生ボタンを押下します。

image.png

ターミナルで以下が表示されたらOKです。

ターミナル
(省略)
=> Booting Puma
=> Rails 5.1.6 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.4 (ruby 2.4.3-p205), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
  • 後は実際の画面で確認するだけ!
    以下はscaffoldで作成したequipment_commentsのindexにブレークポイントを貼った例です。

image.png

参考

Windows10でWSLとVSCodeを使ってプログラミング環境を整える
Visual Studio Codeを使ってRailsをデバッグ実行してみよう
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
https://github.com/rubyide/vscode-ruby/issues/336

21
36
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
21
36