硬编码“需要”调试“找不到源文件 [英] Hardcoded "require 'debug'" can't find the sourcefile
问题描述
使用这个最小的红宝石码:
require'debug'
puts
在一个文件中,例如script.rb
如果我像这样启动它: ruby -rdebug script.rb
,然后在调试提示符下按 l
,按预期方式获取列表
如果我正常按 ruby script.rb
按 l
我得到:
(rdb:1)l
[-3,6 ]在script.rb
没有源文件可用于script.rb
错误消息似乎误导最好:工作目录没有改变,文件肯定还在那里!
我无法找到有关此行为的文档(我在两者都尝试过jruby和mri,行为是一样的)
我知道调试器和撬,但是它们提供了一个不同的用例: em>
我习惯了使用内置调试模块的其他脚本语言,可以让我在代码中的任何地方放一个语句,把我放在一个调试shell,i nspect代码,变量等等...拥有它的优点是它可以随处可见,而不必为它设置环境,甚至当我在不是我自己的机器上
我可以明白地解决这个问题,总是用 -rdebug
来调用解释器,并手动设置断点,但是我发现这更多的工作比替代
在调查调试源代码后,我找到了一个解决方法一个修复:
解决方法可以是:
trace on
next
trace off
list
这将让你得到列出而不用 -rdebug
重新启动解释器,缺点是您将从跟踪中获得一些其他不需要的输出,您只能在移动后才能看到它通过一个语句
进行修复:问题是 SCRIPT_LINES __
哈希缺少当前文件的值...显然它只设置在 tracer.rb
我已经更改了第161行,并使用跟踪其中[] =已经被调用的子类来更改哈希,并且我无法挖掘在进入不同文件的函数时执行该工作的实际代码另外:我还没有找到一个积极使用这个模块的人(我在#ruby上#puby,#jruby和#pry在freenode上),并且一起因为它使用现在已经过时的功能这让我对这个模块的维护状态有点悲观。然而,我提交了一个提取修复的请求(它实际上是相当愚蠢和简单,但否则我需要更深入地了解这个模块,并可能重构其中的一些部分...但如果这个模块没有积极的维护,我不知道这是一件好事情,努力)
With this minimal ruby code:
require 'debug'
puts
in a file called, e.g. script.rb
if I launch it like so: ruby -rdebug script.rb
and then press l
on the debug prompt, I get the listing, as expected
if I instead run it normally as ruby script.rb
when pressing l
I get:
(rdb:1) l
[-3, 6] in script.rb
No sourcefile available for script.rb
The error message seems misleading at best: the working directory hasn't changed, and the file is definitely still there!
I'm unable to find documentation on this behavior (I tried it on both jruby and mri, and the behavior is the same)
I know about 'debugger' and 'pry', but they serve a different use case:
I'm used to other scripting languages with a builtin debug module, that can let me put a statement anywhere in the code to drop me in a debug shell, inspect code, variables and such... the advantage of having it builtin it's that it is available everywhere, without having to set up an environment for it, or even when I'm on a machine that's not my own
I could obviously workaround this by always calling the interpreter with -rdebug
and manually setting the breakpoint, but I find this more work than the alternative
After looking into the debug source code, I found a workaround and a fix:
the workaround can be:
trace on
next
trace off
list
this will let you get the listing without restarting the interpreter with -rdebug
, with the disadvantage that you'll get some otherwise unwanted output from the tracing, and you'll be able to see it only after moving by one statement
for the fix: the problem is that the SCRIPT_LINES__
Hash lacks a value for the current file... apparently it's only set inside tracer.rb
I've changed line 161, and changed the Hash with a subclass that tracks where []= has been called from, and I wasn't able to dig up the actual code that does the work when stepping into a function that comes from a different file
Also: I haven't found a single person yet who actively uses this module (I asked both on #ruby, #jruby and #pry on freenode), and together with the fact that it uses a function that is now obsolete it leads me to be a bit pessimistic about the maintenance state of this module
nonetheless, I submitted a pull request for the fix (it's actually quite dumb and simple, but to do otherwise I'd need a deeper understanding of this module, and possibly to refactor some parts of it... but if this module isn't actively maintaned, I'm not sure that's a good thing to put effort on)
这篇关于硬编码“需要”调试“找不到源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!