简体   繁体   English

Ruby on Rails:调试rake任务

[英]Ruby on Rails: debugging rake tasks

When I write debugger it does not start: 当我编写debugger它无法启动:

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. 如果我运行rake my:task --debugger ,它会立即将我返回到控制台。 How is it possible to debug rake tasks? 如何调试rake任务?

I found the solution. 我找到了解决方案。

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

or on some systems 或者在某些系统上

$ rdebug rake my:task

Andrey Kouznetsov's answer didn't work for me using Ruby 1.9.3. Andrey Kouznetsov的回答对我使用Ruby 1.9.3不起作用。 The ruby-debug gem doesn't seem to support Ruby 1.9. ruby-debug gem似乎不支持Ruby 1.9。 I had to use the debugger gem: https://github.com/cldwalker/debugger . 我不得不使用调试器gem: https//github.com/cldwalker/debugger

  1. Add gem 'debugger' to my Gemfile's development group. gem 'debugger'添加到我的Gemfile开发组。
  2. Run bundle . 运行bundle
  3. Add require 'debugger' to the top of my rake task. require 'debugger'添加到我的rake任务的顶部。
  4. Add a call to debugger where I wanted a breakpoint in my rake task. debugger中添加一个调用,在我的rake任务中我想要一个断点。
  5. Run the rake task normally from the command line, eg: rake my:task . 通常从命令行运行rake任务,例如: rake my:task

I highly recommend pry for this 我强烈推荐这个

bundle install pry
require 'pry'
rake ...

In your rake task file: 在你的rake任务文件中:

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+ ByeBug是2.0+的另一个

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

Visual Studio Code has pretty good debugger, built-in. Visual Studio Code具有非常好的内置调试器。 If anybody finds this searching for a way to get it to work with rake, here's a working configuration: 如果有人发现这种搜索方式让它与rake一起工作,这是一个有效的配置:

{
    "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 . 这将运行all rake任务。 You may have to change the path to rake, I didn't find way to run the one in PATH. 您可能必须更改rake的路径,我没有找到在PATH中运行该路径的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM