Ruby on Rails:调试 rake 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2663912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:33:06  来源:igfitidea点击:

Ruby on Rails: debugging rake tasks

ruby-on-railsrubydebuggingrakeruby-debug

提问by Andrey Kuznetsov

When I write debuggerit 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. How is it possible to debug rake tasks?

如果我运行rake my:task --debugger,它会立即将我返回到控制台。如何调试 rake 任务?

采纳答案by Liron Yahdav

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.

Andrey Kouznetsov 的回答对我使用 Ruby 1.9.3 不起作用。ruby-debug gem 似乎不支持 Ruby 1.9。我不得不使用调试器 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 debuggerwhere I wanted a breakpoint in my rake task.
  5. Run the rake task normally from the command line, e.g.: rake my:task.
  1. 添加gem 'debugger'到我的 Gemfile 的开发组。
  2. 运行bundle
  3. 添加require 'debugger'到我的佣金任务的顶部。
  4. debugger在我的 rake 任务中添加对我想要断点的位置的调用。
  5. 在命令行中,如正常运行rake任务:rake my:task

回答by Andrey Kuznetsov

I found the solution.

我找到了解决方案。

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

or on some systems

或在某些系统上

$ rdebug rake my:task

回答by Abram

I highly recommend pryfor this

我强烈建议这个

bundle install pry
require 'pry'
rake ...

In your rake taskfile:

在您的rake 任务文件中:

binding.pry 

回答by Sean McCleary

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

这种方法对我不起作用。我刚刚在我的代码中添加了这个:

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

回答by Matthias Winkelmann

Visual Studio Codehas 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:

Visual Studio Code有非常好的内置调试器。如果有人发现这正在寻找一种让它与 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. You may have to change the path to rake, I didn't find way to run the one in PATH.

这将运行 rake 任务all。您可能需要更改 rake 的路径,我没有找到在 PATH 中运行该路径的方法。

回答by Cody Moniz