Different order of plugin load_paths in $LOAD_PATHS and Dependencies

I was just about to submit the following ticket including a patch for Rails 2.0. But then I noticed that this behaviour hasn't been invented with the recent refactorings of the plugin loading mechanism but was already there in Rails 1.2.x: http://dev.rubyonrails.org/browser/tags/rel_1-2-6/railties/lib/initializer.rb#L393

Therefor I thought it might make more sense to submit this to the mailinglist first for discussion.

So, is there a particular reason for this behaviour?

# Different order of plugin load_paths in $LOAD_PATHS and Dependencies

In Rails::Plugin::Loader#add_plugin_load_paths paths are inserted into $LOAD_PATHS in reverse alphabetical order so that plugins loaded later will take precedence over plugins loaded earlier. (This has been described as the intended behaviour here: http://www.ruby-forum.com/topic/133009)

However these paths are appended to Dependencies.load_paths and Dependencies.load_once_paths so they will appear in alphabetical order in these array.

http://dev.rubyonrails.org/browser/tags/rel_2-0-1/railties/lib/rails/plugin/loader.rb#L44

So in effect if there are two files with the same name in two different plugins like these ...

/plugins/plugin_a/lib/something.rb
/plugins/plugin_b/lib/something.rb

... require 'something' will load the one with latter file while using Dependencies constant autoloading will load the former file:

require 'something' # loads /plugins/plugin_b/lib/something.rb (correct)
Something # loads /plugins/plugin_a/lib/something.rb (wrong)

plugin_loader_load_paths_order.diff (1.12 KB)

plugin_loader_test_load_paths_order.diff (2.94 KB)

In days of old, this meant that 'your' stuff would be top of the list when your init.rb ran, but that's no longer the case.
Just my opinion, but I wouldn't think you'd ever want to be relying on a specific load path ordering

Fred

Check out the thread recently called 'Are plugins supposed to be
loaded alphabetically?' We explored this in depth, and I think the
current behavior is correct. Bottom line, you SHOULDN'T have
identically named files on your load path and depend on load path
order. If you don't agree with that, it's a separate issue. I think
I agree with it :slight_smile:

-- Chad

Hi Frederick, hi Chad,

thanks for your responses.

No offense, but I think both of you missed the point here.

Check out the thread recently called 'Are plugins supposed to be
loaded alphabetically?' We explored this in depth, and I think the
current behavior is correct.

I even linked to that thread. And no, it doesn't explore this issue.

Bottom line, you SHOULDN'T have
identically named files on your load path and depend on load path
order.

I'm not talking about load path order.

In fact, the current implementation in Plugin::Loader makes sure that the issue is present no matter in which order plugins are loaded: plugin load_paths are appended to the end of Dependencies.load_paths, but unshifted to the front (sort of, you get the point) of $LOAD_PATHS. (see lines 47-49 in rails/plugin/loader.rb)

Thus, no matter how the load order of two plugins is specified, if you have two files with the same filenames in your plugin load_paths, the following statements will load different files:

require "something" # uses $LOAD_PATH
Something.called # uses Dependencies.load_paths

I even linked to that thread. And no, it doesn't explore this issue.

Sorry, should have read closer :confused:

I'm not talking about load path order.

In fact, the current implementation in Plugin::Loader makes sure that
the issue is present no matter in which order plugins are loaded:
plugin load_paths are appended to the end of Dependencies.load_paths,
but unshifted to the front (sort of, you get the point) of
$LOAD_PATHS. (see lines 47-49 in rails/plugin/loader.rb)

Thus, no matter how the load order of two plugins is specified, if you
have two files with the same filenames in your plugin load_paths, the
following statements will load different files:

require "something" # uses $LOAD_PATH
Something.called # uses Dependencies.load_paths

I get it. If this is the case, it does seem like a bug to me. As
mentioned in the other thread, I think the official position still
seems to be that you shouldn't have identically-named files, which
would make this a moot point. However, given that 1) this bug didn't
exist before, 2) there's probably plenty of plugins out there that do
rely on the identically-named-file approach, 3) some people won't want
to buy into the idea that identically named files are bad, then it
seems like this should be fixed.

-- Chad

Ok, I now created a ticket for this and added a patch accordingly.

See http://dev.rubyonrails.org/ticket/10737

Anybody mind to check this out?

Thanks!