berdario
berdario

Reputation: 1860

Hardcoded "require 'debug'" can't find the sourcefile

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

Upvotes: 3

Views: 691

Answers (2)

berdario
berdario

Reputation: 1860

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)

Upvotes: 2

Fred
Fred

Reputation: 8602

I suspect the Ruby interpreter doesn't have the ability to load the sourcefile without the components in the debug module. So, no -rdebug, no access to the sourcefile. And I agree it is a misleading error. "Debugging features not loaded" might be better.

Upvotes: 1

Related Questions