Projects

Ticket #721 (new defect)

Opened 21 months ago

Last modified 6 months ago

Compiled ruby files return incorrect values for __FILE__

Reported by: mred@… Owned by: lsansonetti@…
Priority: major Milestone: MacRuby Later
Component: MacRuby Keywords:
Cc: isaackearse@…

Description

Beginning with files "test_file.rb"

puts "__FILE__ = #{__FILE__}"

and "main.rb"

require 'test_file.rb'
require 'test file.rbo'

arranged like this

/
  tmp
    ruby
      test_file.rb
    ruby2
      main.rb  

I then issued the following commands starting in the /tmp directory

macruby -C ruby/test_file.rb -o ruby2/test_file.rbo
mv ruby/test_file.rb ruby2
cd ruby2
macruby main.rb

and got the following results

__FILE__ = /tmp/ruby2/test_file.rb
__FILE__ = ruby/test_file.rb

The incorrect FILE values make it very difficult design a hierarchy of files and then load them using 'require' and relative paths for example.

Change History

Changed 14 months ago by isaackearse@…

  • cc isaackearse@… added

Cc Me!

Changed 14 months ago by isaackearse@…

The description here is slightly wrong: the first command issued should be a call to "macrubyc" - no "macruby"

Changed 14 months ago by isaackearse@…

The output I get when running the last command "macruby main.rb" on the latest nightly is:

__FILE__ = /Users/isaac/src/tmp/ruby2/test_file.rb
/Users/isaac/src/tmp/ruby2/main.rb:2:in `<main>': no such file to load -- test file.rbo (LoadError)

Which I presume is the correct result, because I don't think .rbo files should not be loaded directly like this. From the macrubyc man page: "The MacRuby runtime will always pick .rbo files over .rb files upon calls to the require method".

Changed 14 months ago by isaackearse@…

I can reproduce the original results by changing main.rb to this:

require 'test_file.rb'
$: << File.dirname(__FILE__)
require 'test_file'

So it looks like FILE evaluates to an absolute path in an .rb file and to a relative path in an .rbo file.

Changed 14 months ago by lsansonetti@…

We could compile FILE as the absolute path during compilation, but it means that if the .rbo file is moved later to a different directory in the filesystem, the FILE variable won't be updated.

Changed 14 months ago by lsansonetti@…

  • milestone set to MacRuby Later

Moving to Later for now. We can move it to 1.0 if needed.

Changed 6 months ago by mrada@…

I duplicated this issue with ticket #1370. I was testing out a compiled Hot Cocoa. I hacked up a work around for that particular case that should work for most other cases:

 https://github.com/ferrous26/hotcocoa/commit/8e38eb2546e3385e2f934577bd852154d6980f6c

It is based on the fact that $LOADED_FEATURES keeps the absolute path for loaded files. I think a similar bit of code could fix this issue if it is possible to set __FILE__ at load time using whatever code is already being used to add the file name to $LOADED_FEATURES. I haven't looked into it though...

Note: See TracTickets for help on using tickets.