Require when Executed file is required by another file.

Z

Zev Blut

Hello,

My colleagues and I just encountered an interesting gotcha with
"require".

Put the following in a file called "req1.rb"

-------------- [req1.rb]
#!/usr/local/bin/ruby1.8 -w

puts "$0 #{$0}"
base = $0.split("/")
PATH = base[0...(base.size - 1)].join("/")

def jump
require "req2"
end

puts "Fun"

if __FILE__ == $0
puts "Require 1"
jump
end
---------- [req1.rb]

And then put the following in another file called "req2.rb"

---------- [req2.rb]
require "#{PATH}/req1"

puts "Require 2"
---------- [req2.rb]

Now run req1.rb from the full path.

Here is an example of my output:
========
$ /home/zev/projects/rubyplay/recurse_require/req1.rb
$0 /home/zev/projects/rubyplay/recurse_require/req1.rb
Fun
Require 1
$0 /home/zev/projects/rubyplay/recurse_require/req1.rb
/home/zev/projects/rubyplay/recurse_require/req1.rb:5: warning:
already initialized constant PATH

/home/zev/projects/rubyplay/recurse_require/req1.rb:7: warning: method
redefined; discarding old jump

Fun
Require 1
Require 2
=========

Notice that the __FILE__ == $0 gets called twice!
Also the fact that PATH and jump get redefined too...

Is there a way to easily stop this?

Thanks,
Zev
 
G

Guest

It seems that although require normally makes sure a certain files only
gets included once require somehow doesn't take into account the file
the interpreter first included.

Regards,

Peter
 
Z

Zev Blut

It seems that although require normally makes sure a certain files only
gets included once require somehow doesn't take into account the file
the interpreter first included.

Exactly, but should this be the case?

Cheers,
Zev
 
G

Gennady Bystritsky

Exactly, but should this be the case?

require provides a simple mechanism trying to prevent multiple file
loads -- it keeps loaded file paths in array $" just as given
(appending a proper extension - .rb, .so, etc. - if it is missing) and
does not load a file if the same path is already present in the array.
It is very easy to trick it by requiring the same file using different
paths, like in "test.rb", "./test.rb", "././test.rb".

In general, it is very hard and time consuming to determine that
different paths refer to the same file, so require does not even try to
pretend that it is robust.

Knowing how require works, you can easily implement some work around
for your particular case.
Cheers,
Zev

Sincerely,
Gennady Bystritsky
 
Z

Zev Blut

Hello,

In general, it is very hard and time consuming to determine that
different paths refer to the same file, so require does not even try to
pretend that it is robust.

It certainly appears to be a bit tricky. Although, it would be nice
if the executed program was added to $" so that it could prevent being
required again.

Knowing how require works, you can easily implement some work around for
your particular case.

The solution for the day is probably that my colleagues and I need to
make all executable programs thin wrappers that only require the main
code and implement the __FILE__ == $0 logic.

Best,
Zev
 
M

Martin Ankerl

In general, it is very hard and time consuming to determine that
different paths refer to the same file, so require does not even try to
pretend that it is robust.

Why not calculate a hash of the file and use this in the check. This
would make the whole thing independent from the actual path, and allows
to re-require a file if its content changes.

martinus
 
Z

Zev Blut

Why not calculate a hash of the file and use this in the check. This
would make the whole thing independent from the actual path, and allows
to re-require a file if its content changes.

Interesting solution! Although, I am sure some people may not like the
potential performance hit. Also the re-require could be good and it
could be bad, depending upon various situations.

A coworker of mine just commented why not just use File.expand_path for
the required file? Is there any reason not to do this?

Cheers,
Zev
 
J

Joel VanderWerf

Zev said:
A coworker of mine just commented why not just use File.expand_path for
the required file? Is there any reason not to do this?

Symlinks...

$ cd /tmp
$ touch foo
$ ln -s foo bar
$ ruby -e 'p File.expand_path("foo")'
"/tmp/foo"
$ ruby -e 'p File.expand_path("bar")'
"/tmp/bar"

It might not be very portable, but there is File::Stat#ino:

$ ruby -e 'p File.stat("foo").ino'
1239183
$ ruby -e 'p File.stat("foo").dev'
776
$ ruby -e 'p File.stat("bar").ino'
1239183
$ ruby -e 'p File.stat("bar").dev'
776
 
Z

Zev Blut

Symlinks...

$ cd /tmp
$ touch foo
$ ln -s foo bar
$ ruby -e 'p File.expand_path("foo")'
"/tmp/foo"
$ ruby -e 'p File.expand_path("bar")'
"/tmp/bar"

Yes that is true, but I noticed if you actually go to directory with
a symlink, it will expand to the true path. Like so:

$ cd /tmp/
$ mkdir foo
$ ln -s foo bar
$ touch foo/baz.rb
$ ruby -e 'Dir.chdir("foo") ;p File.expand_path("baz.rb")'
"/tmp/foo/baz.rb"
$ ruby -e 'Dir.chdir("bar") ;p File.expand_path("baz.rb")'
"/tmp/foo/baz.rb"

Is this portable or are there other problems with doing it this way?

Best,
Zev
 
J

Joel VanderWerf

Zev said:
Yes that is true, but I noticed if you actually go to directory with
a symlink, it will expand to the true path. Like so:

$ cd /tmp/
$ mkdir foo
$ ln -s foo bar
$ touch foo/baz.rb
$ ruby -e 'Dir.chdir("foo") ;p File.expand_path("baz.rb")'
"/tmp/foo/baz.rb"
$ ruby -e 'Dir.chdir("bar") ;p File.expand_path("baz.rb")'
"/tmp/foo/baz.rb"

Is this portable or are there other problems with doing it this way?

Hm, I didn't expect that. And also:

$ ruby -e 'Dir.chdir("bar") ;p Dir.pwd'
"/tmp/foo"

In the shell (using linux, zsh) it works differently:

$ cd bar
$ pwd
/tmp/bar

Another data point:

$ ruby -e 'Dir.chdir("bar"); puts `pwd`'
/tmp/foo

I guess ruby follows symlinks when doing Dir.chdir?

Anyway, it doesn't solve the problem of a symlinked .rb file, if that's
a problem.
 
Z

Zev Blut

Hello,

Zev Blut wrote:
Yes that is true, but I noticed if you actually go to directory with
a symlink, it will expand to the true path. Like so:
[Snip examples]
Hm, I didn't expect that. And also:

[Snip more examples]

I guess ruby follows symlinks when doing Dir.chdir?

Anyway, it doesn't solve the problem of a symlinked .rb file, if that's
a problem.

Yes unfortunately it does not...
Personally I use require with symlinks to directories, but not files.
Although, I am sure someone does require symlinked files.

Your File.stat suggestion looks good, but I just confirmed, as you
suggested,
that it is not portable to a Windows environment.

I suppose Martin Ankerl's suggestion of computing a hash of each file is
the
most robust.

Looking at the ToDo in Ruby I noticed this:

* save both "feature names" and "normalized path" in $"

Maybe, there is already a better solution and it just waits for
implementation :)

Zev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top