[Rubygems-developers] [ rubygems-Bugs-29077 ] [PATCH] executable wrapper should set $0

noreply at rubyforge.org noreply at rubyforge.org
Tue Mar 15 18:30:05 EDT 2011


Bugs item #29077, was opened at 2011-03-11 12:07
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=575&aid=29077&group_id=126

Category: `gem install` command
Group: v1.6.x
>Status: Closed
>Resolution: Rejected
Priority: 3
Submitted By: Suraj Kurapati (snk)
Assigned to: Eric Hodel (drbrain)
Summary: [PATCH] executable wrapper should set $0

Initial Comment:
Hello,

The wrapper executable that RubyGems generates should set $0 to the actual executable file before loading it.  This allows us to use the "if __FILE__ == $0" idiom (which is an indispensable feature for essentially single-file executables that we just want to release as ruby gems) in our executables.

Thanks for your consideration.

----------------------------------------------------------------------

>Comment By: Eric Hodel (drbrain)
Date: 2011-03-15 15:30

Message:
In the past 'bin' was on the load path so require may have loaded executables unintentionally.

Since this functionality is only desired to make your testing easier you should move testable code out of bin/ and place it in lib/.  Your executable in bin should be something simple like:

#!/usr/local/bin/ruby19

require 'meme'

Meme.run ARGV

Furthermore, setting $0 accurately may not be possible due to platform issues.  On some operating systems $0 will be truncated when set.

----------------------------------------------------------------------

Comment By: Suraj Kurapati (snk)
Date: 2011-03-14 14:30

Message:
Although it is a single-file executable, it still uses data 
structures / logic factored into separate classes & modules.  
The "if __FILE__ == $0" idiom allows me to try out these 
data structures in IRB without "running" the executable.  
That idiom also allows me to embed a unit test inside the 
executable (via an additional "if $DEBUG") if I want.

I don't understand your comment about RubyGems not putting 
'bin' on the $LOAD_PATH.  Why does that matter?  Doesn't 
RubyGems install wrapper executables that rely on 
Gem.bin_path() instead of $LOAD_PATH?

IMHO, the wrapper executables RubyGems installs should be 
completely transparent to the underlying "real" executable 
being invoked.

Thanks for your consideration.

----------------------------------------------------------------------

Comment By: Eric Hodel (drbrain)
Date: 2011-03-14 13:45

Message:
If this is only a single-file executable why would you need the `if __FILE__ == $0` check?  RubyGems no longer places 'bin' on the load path.

----------------------------------------------------------------------

Comment By: Suraj Kurapati (snk)
Date: 2011-03-11 12:27

Message:
Here is a handy little command to fix existing wrappers:

sed -i '$s/^load G/load $0 = G/' \
$(gem env | sed -n 's/^.*EXECUTABLE DIRECTORY: *//p')/*

Cheers.

----------------------------------------------------------------------

Comment By: Suraj Kurapati (snk)
Date: 2011-03-11 12:15

Message:
Whoops, sorry for filing this patch request under the bug 
tracker (I started out writing a bug report but decided to 
submit a patch midway).  Please move it under the patch 
tracker if possible.  Thanks!

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=575&aid=29077&group_id=126


More information about the Rubygems-developers mailing list