Alex Kovar

Alex Kovar

Software Engineer

© 2020

A Quick Fix For An Issue With Symlinking Ruby

Ran into this problem the other day. Thought I’d blog about it before it got too stale in my mind. In most typical entry points to an application you’ll see something like this:

$LOAD_PATH << File.join( File.dirname(__FILE__), '..', 'lib' )
require 'your-lib-name'

p. Here it is adding “../lib” to the load path and then loading your-lib-name.rb from that directory. However, when this script is being run via a symlink, FILE will be interpretted as the location of the symlink. Obviously, because “../lib” is a relative location this can cause problems. This will give an error that I am sure most developers will be familiar with:

:in `require': no such file to load -- yourlib (LoadError)

And absolute paths really aren’t a very good option either. So what to do? Well, luckily, you can do this:

require 'pathname'
$LOAD_PATH << File.join( File.dirname(Pathname.new(__FILE__).realpath), '..', 'lib' )
require 'your-lib-name'

As you can see, Pathname.realpath resolves the symlink to the real location. I thought it was an interesting problem and am surprised I haven’t seen this sooner.