0

I am refactoring some shared code in some of my rails apps, and I'm encountering a weird issue with some sym linked directories. Basically, my old structure of my rails app was:

RAILS_ROOT
  -> app
  -> config
     -> initializers
         -> common
  ->lib
     -> common
  ...

Things were working just fine, and rails was recognizing the common folders and loading all of the files in there.

So I decided to move all of the common stuff to its own repo and submodule it into my rails app. So the new folder structure looks like

RAILS_ROOT
  -> app
  -> config
     -> initializers
         -> common -> ../../shared/initializers/
  ->lib
     -> common -> ../shared/lib/
  -> shared
     -> initializers
     -> lib
     ...
  ... 

For some reason, none of the files in the sym linked directories are getting loaded and I'm not sure why. If I create a temp directory in the initializers folder, and copy some of the "common" files over, they load fine, so it seems it has to do with the fact that it is sym linked.

gmoniey
  • 7,975
  • 4
  • 27
  • 30
  • See what happens if you `mount --bind` them over. – Reactormonk Jan 23 '12 at 07:24
  • I'm deving on OS X (although I deploy to cent machines), and there doesn't seem to be a bind option. There is bindfs for OS X, but I read that it has some issues. – gmoniey Jan 23 '12 at 07:51

1 Answers1

1

So I dug into the rails source, and found the following code for loading the initializers:

 def load_application_initializers
  if gems_dependencies_loaded
    Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
      load(initializer)
    end
  end
end

And based on this question: Can I traverse symlinked directories in Ruby with a "**" glob? it looks like that code won't follow sym links.

To get around this, I ended up putting the following monkey patch before the Rails::Initializer.run line

module Rails
  class Initializer
    def load_application_initializers
      if gems_dependencies_loaded
        Dir["#{configuration.root_path}/config/initializers/**{,/*/**}/*.rb"].sort.each do |initializer|
          load(initializer)
        end
      end
    end
  end
end

This is obviously hack, and hopefully I find a cleaner way of doing this, but for now, it solves my problem. Interestingly enough, the code used to load plugins and the lib folder does follow sym links.

Community
  • 1
  • 1
gmoniey
  • 7,975
  • 4
  • 27
  • 30