POSTS
使用vscode如何来debug Rails应用
前提条件
需要安装 vscode-ruby插件
ruby v2.x版本,安装 ruby-debug-ide 和 debase
Gemfile中添加:
gem 'debase', '~> 0.2.4'
gem 'ruby-debug-ide', '~> 0.7.0'
- 然后执行
bundle
vscode配置
- 点击导航栏Debug => Open Configurations => launch.json
- 将下面内容粘贴到launch.json文件中
{
"version": "0.2.0",
"configurations": [
{
"name": "Start Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/bin/rails",
"args": [
"server",
"-p",
"3000"
]
},
{
"name": "Debug Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"useBundler": true,
"pathToBundler": "/path/to/rubygem/wrappers/bundle",
"pathToRDebugIDE": "/path/to/rubygem/gems/ruby-debug-ide-x.x.x/bin/rdebug-ide",
"program": "${workspaceRoot}/bin/rails",
"args": [
"server",
"-p",
"3000"
]
},
{
"name": "Run RSpec - all",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "/path/to/rubygem/bin/rspec",
"args": [
"--pattern",
"${workspaceRoot}/spec/**/*_rspec.rb"
]
},
{
"name": "Debug RSpec - open spec file",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"useBundler": true,
"pathToBundler": "/path/to/rubygem/wrappers/bundle",
"pathToRDebugIDE": "/path/to/rubygem/gems/ruby-debug-ide-x.x.x/bin/rdebug-ide",
"debuggerPort": "1235",
"program": "/path/to/rubygem/bin/rspec",
"args": [
"${file}"
]
},
{
"name": "Debug RSpec - open spec file on a certain line",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"useBundler": true,
"pathToBundler": "/path/to/rubygem/wrappers/bundle",
"pathToRDebugIDE": "/path/to/rubygem/gems/ruby-debug-ide-x.x.x/bin/rdebug-ide",
"debuggerPort": "1235",
"program": "/path/to/rubygem/bin/rspec",
"args": ["${file}:${lineNumber}"]
}
]
}
If you are using jRuby, you want to add the below JRUBY environment variable under the above configurations 'Start Rails server' and 'Debug Rails server'. So, it will be like:
{
"name": "Start Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"env": {
"JRUBY_OPTS": "-X-C -J-Xmx4096m -J-XX:+UseConcMarkSweepGC"
},
"program": "${workspaceRoot}/bin/rails",
"args": [
"server",
"-p",
"3000"
]
},
{
"name": "Debug Rails server",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"env": {
"JRUBY_OPTS": "-X-C -J-Xmx4096m -J-XX:+UseConcMarkSweepGC --debug"
},
"useBundler": true,
"pathToBundler": "/path/to/rubygem/wrappers/bundle",
"pathToRDebugIDE": "/path/to/rubygem/gems/ruby-debug-ide-x.x.x/bin/rdebug-ide",
"program": "${workspaceRoot}/bin/rails",
"args": [
"server",
"-p",
"3000"
]
},
- 获取bundle、ruby-debug-ide、rpsec绝对路径,然后替换launch.json里面对应的内容
$: which bundle
/path/to/rubygem/bin/bundle
# 替代 launch.json 文件pathToBundler默认的值,下面一样的操作
$: bundle show ruby-debug-ide
/path/to/rubygem/gems/ruby-debug-ide-x.x.x
# launch.json => pathToRDebugIDE
$: which rspec
/path/to/rubygem/bin/rspec
# launch.json => program
开始debug
- 导航栏点击debug
- 这里可以选择launch.json定义的 name
- 这里选择Debug Rails server,然后打断点,访问url开始调试:
- 左边可以看到VARIABLES存的变量值,WATCH这里相当于console可以输入变量查看值,上面的弹窗表示一些常用的调试操作(单步调试、跳过等)