Ruby on Rails: debugging rake tasks

有些话、适合烂在心里 提交于 2019-11-28 19:07:08

Andrey Kouznetsov's answer didn't work for me using Ruby 1.9.3. The ruby-debug gem doesn't seem to support Ruby 1.9. I had to use the debugger gem: https://github.com/cldwalker/debugger.

  1. Add gem 'debugger' to my Gemfile's development group.
  2. Run bundle.
  3. Add require 'debugger' to the top of my rake task.
  4. Add a call to debugger where I wanted a breakpoint in my rake task.
  5. Run the rake task normally from the command line, e.g.: rake my:task.
Andrey Kuznetsov

I found the solution.

$ gem install ruby-debug
$ ruby-debug rake my:task

or on some systems

$ rdebug rake my:task

I highly recommend pry for this

bundle install pry
require 'pry'
rake ...

In your rake task file:

binding.pry 

This approach did not work for me. I just added this in my code:

require 'ruby-debug'
# ... code ...
debugger

ByeBug is another one for 2.0+

https://github.com/deivid-rodriguez/byebug

Visual Studio Code has pretty good debugger, built-in. If anybody finds this searching for a way to get it to work with rake, here's a working configuration:

{
    "name": "Debug a rake task",
    "type": "Ruby",
    "request": "launch",
    "useBundler": true,
    "cwd": "${workspaceRoot}",
    "program": "/usr/local/bin/rake",
    "args": ["all"]
}

This would run the rake task all. You may have to change the path to rake, I didn't find way to run the one in PATH.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!