Ruby on Rails: debugging rake tasks
Asked Answered
B

6

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?

Balmung answered 18/4, 2010 at 20:53 Comment(1)
Check this if you want to debug from Rubymine. https://mcmap.net/q/390518/-rubymine-debugger-with-rakeOscar
B
41

I found the solution.

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

or on some systems

$ rdebug rake my:task
Balmung answered 18/4, 2010 at 21:7 Comment(6)
Really though you shouldn't be doing large amounts of logic in a rake task. Most of my rake tasks are one-liners that call a method on a model which is then fully covered with tests.Torrie
I am doing spider based on mechanize and I need debug some lines. All them in methods and not covered by tests yet. Thank you for the reply.Balmung
@rspeicher, it's useful if you have a seed taskEfficacy
on my system is rdebug instead of ruby-debugEfficacy
TIP: Add in the beginning Debugger.settings[:autoeval] = true that will cause the typical rails behavior where everything you type runs scoped to the self where you put the debugger call.Lovelady
This didn't work for me in Ruby 1.9. See my answer for what did work for me in Ruby 1.9.Rick
R
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.
Rick answered 8/3, 2013 at 20:46 Comment(1)
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.Ekg
I
12

I highly recommend pry for this

bundle install pry
require 'pry'
rake ...

In your rake task file:

binding.pry 
Intrigante answered 23/6, 2014 at 21:40 Comment(0)
W
7

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

require 'ruby-debug'
# ... code ...
debugger
Windsor answered 29/6, 2011 at 20:58 Comment(0)
D
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.

Demilune answered 22/10, 2016 at 7:31 Comment(3)
all didn't work for me. I used rvms rake and I set args to the name of my task with a breakpoint on the first line of it and it worked for me.Conditioned
@SomeGuyOnAComputer: Hence the sentence "This would run the rake task all". It's just the name of a task I have (/had two years ago).Demilune
my bad I missed thatConditioned
G
1

ByeBug is another one for 2.0+

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

Gujarati answered 10/4, 2015 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.