43

When I write debugger it does not start:

NoMethodError: undefined method `run_init_script' for Debugger:Module
from /usr/local/lib/ruby/gems/1.8/gems/ruby-debug-base-0.10.3/lib/ruby-debug-base.rb:239:in `debugger'
from (irb):4

If I run rake my:task --debugger,it returns me to console immediately. How is it possible to debug rake tasks?

ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
  • Check this if you want to debug from Rubymine. https://stackoverflow.com/questions/4401602/rubymine-debugger-with-rake – Akram Jan 03 '19 at 05:26

6 Answers6

41

I found the solution.

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

or on some systems

$ rdebug rake my:task
marcgg
  • 65,020
  • 52
  • 178
  • 231
Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
27

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.
Liron Yahdav
  • 10,152
  • 8
  • 68
  • 104
  • 2
    Another option instead of putting `require 'debugger'` at the top is to put inline `require 'debugger'; debugger`. That way, when you delete the `debugger` statements, you won't forget to delete the `require`. – wisbucky Apr 02 '15 at 16:37
12

I highly recommend pry for this

bundle install pry
require 'pry'
rake ...

In your rake task file:

binding.pry 
Abram
  • 39,950
  • 26
  • 134
  • 184
7

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

require 'ruby-debug'
# ... code ...
debugger
Sean McCleary
  • 3,690
  • 4
  • 33
  • 43
5

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.

Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76
1

ByeBug is another one for 2.0+

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

Cody Moniz
  • 4,845
  • 3
  • 22
  • 20