# IronRuby internals - How can I get the full path to the current file?

**URL:** <https://www.ruby-forum.com/t/ironruby-internals-how-can-i-get-the-full-path-to-the-current-file/215417>\
**Category:** IronRuby\
**Created:** [January 3, 2012, 11:04pm UTC](https://www.ruby-forum.com/t/ironruby-internals-how-can-i-get-the-full-path-to-the-current-file/215417 "2012-01-03T23:04:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 3, 2012, 11:04pm UTC](https://www.ruby-forum.com/t/ironruby-internals-how-can-i-get-the-full-path-to-the-current-file/215417/1 "2012-01-03T23:04:47Z")

</div>

I thought I’d be a good samaritan and implement require\_relative in  
IronRuby, but I’m having real trouble figuring out what the current  
file’s  
path actually is.

The closest I’ve got is something like this:

- Pull apart the the caller/backtrace to get the file name
- File.expand\_path(file, File.dirname(dir))
- require that file

This is equivalent to the existing ruby idiom of “require  
File.join(File.dirname( **FILE** ), ‘lib/file1’)”, but this doesn’t work  
reliably because **FILE** is already relative, so File.dirname( **FILE** )  
often just returns “.”  
Without the full path to the current file however, this is as good as we  
can get.

I’ve looked through various parts of the IronRuby source code…

- as far as I can tell the Parser/Syntax tree code (which has access to  
the file path) doesn’t keep the information around
- and even if it did, I’m not sure how regular ruby code could access  
the  
parser/AST??

If anyone could help at all, even with a small bit of detail, it’d be  
much  
appreciated.  
Thanks, Orion

PS:

Here’s my test program, which works correctly in MRI 1.92, but I cannot  
get it to work in IronRuby

require\_relative ‘lib/file1’  
Dir.chdir ‘c:/windows/system32’  
require\_relative ‘lib/file2’

Other things I tried

- eval(“ **FILE** ”) - is hard-coded to return “(eval)”

- I thought about poking around in RubyContext.Loader.LoadedFiles, but  
the  
Loader doesn’t know about the entry-point file (is this a bug??), which  
is  
a big stumbling block

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [January 4, 2012, 5:12am UTC](https://www.ruby-forum.com/t/ironruby-internals-how-can-i-get-the-full-path-to-the-current-file/215417/2 "2012-01-04T05:12:51Z")

</div>

I’d recommend to start with some simpler features than this one. This  
seems to be quite non-trivial to implement efficiently and correctly.  
Note that getting the current stack trace is very expensive, so you’d  
probably want to encode the path into Ruby call-sites (RubyCallAction)  
or something like that. But also adding more information to all  
call-sites consumes more memory so maybe we need a special call-site for  
methods called “require\_relative” and fall back to the slow stack trace  
capture only when the method is called via an alias.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Orion E.  
Sent: Tuesday, January 03, 2012 2:02 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] IronRuby internals - How can I get the full  
path to the current file?

I thought I’d be a good samaritan and implement require\_relative in  
IronRuby, but I’m having real trouble figuring out what the current  
file’s path actually is.

The closest I’ve got is something like this:

- Pull apart the the caller/backtrace to get the file name
- File.expand\_path(file, File.dirname(dir))
- require that file

This is equivalent to the existing ruby idiom of “require  
File.join(File.dirname( **FILE** ), ‘lib/file1’)”, but this doesn’t work  
reliably because **FILE** is already relative, so File.dirname( **FILE** )  
often just returns “.”  
Without the full path to the current file however, this is as good as we  
can get.

I’ve looked through various parts of the IronRuby source code…

- as far as I can tell the Parser/Syntax tree code (which has access to  
the file path) doesn’t keep the information around
- and even if it did, I’m not sure how regular ruby code could access  
the parser/AST??

If anyone could help at all, even with a small bit of detail, it’d be  
much appreciated.  
Thanks, Orion

PS:

Here’s my test program, which works correctly in MRI 1.92, but I cannot  
get it to work in IronRuby

require\_relative ‘lib/file1’  
Dir.chdir ‘c:/windows/system32’  
require\_relative ‘lib/file2’

Other things I tried

- eval(“ **FILE** ”) - is hard-coded to return “(eval)”

- I thought about poking around in RubyContext.Loader.LoadedFiles, but  
the Loader doesn’t know about the entry-point file (is this a bug??),  
which is a big stumbling block
