# Plugin load paths & load order

**URL:** <https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843>\
**Category:** rubyonrails-core\
**Created:** [December 14, 2006, 11:49am UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843 "2006-12-14T11:49:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Adam](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@James\_Adam](https://discuss.rubyonrails.org/u/James_Adam)\
**Post date:** [December 14, 2006, 11:49am UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/1 "2006-12-14T11:49:42Z")

</div>

Messing around with the plugin loading mechanism (as I love to do), I stumbled on a $LOAD\_PATH oddity and I was wondering if I'm missing the reasoning behind it. At the moment, plugin lib directories are added to the $LOAD\_PATH by two independent mechanisms:

&nbsp;&nbsp;&nbsp;&nbsp;\* Rails::Initializer#load\_plugins adds them when the plugin is loaded &nbsp;&nbsp;&nbsp;&nbsp;\* Rails::Initializer::Configuration#default\_load\_paths includes them by default

This behaviour seems broken in the light of the config.plugins; if I have chosen to control the plugins I need to load, other plugin lib directories simply shouldn't be available to Rails.

If there's a reason for this, I'd love to know; alternatively, there's a tiny patch here: [http://dev.rubyonrails.org/ticket/6842](http://dev.rubyonrails.org/ticket/6842)

Also, while I've got your attention, I noticed a while back that Koz suggested that plugin load order could be controlled by config.plugins:

&nbsp;&nbsp;"The recent addition of config.plugins will let you specify which plugins to load, and which order to load them in." ([http://dev.rubyonrails.org/ticket/6418](http://dev.rubyonrails.org/ticket/6418))

This, alas, isn't the case. Is there any support for having Rails respect the order that plugins are specified in on config.plugins? Rather than being anything approaching "plugin dependencies" (spit, gag, splutter), this just adds a bit more meaning to an existing configuration option....

---

<div class="post-metadata">

**Author:** ![Aaron\_Pfeifer](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Aaron\_Pfeifer](https://discuss.rubyonrails.org/u/Aaron_Pfeifer)\
**Post date:** [December 14, 2006, 7:57pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/2 "2006-12-14T19:57:02Z")

</div>

Also reported here: [http://dev.rubyonrails.org/ticket/6571](http://dev.rubyonrails.org/ticket/6571)

James Adam wrote:

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [December 15, 2006, 1:05am UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/3 "2006-12-15T01:05:58Z")

</div>

> &nbsp;&nbsp;"The recent addition of config.plugins will let you specify which plugins to load, and which order to load them in." ([http://dev.rubyonrails.org/ticket/6418](http://dev.rubyonrails.org/ticket/6418))

config.plugins prevents the init.rb from being called, meaning the plugins 'lie dormant'. What's the scenario we're trying to prevent by stopping them from appearing on the load paths?

Having said that, a patch to tidy all that up, and make config.plugins a little more powerful does seem reasonable.

---

<div class="post-metadata">

**Author:** ![James\_Adam](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@James\_Adam](https://discuss.rubyonrails.org/u/James_Adam)\
**Post date:** [December 15, 2006, 10:29am UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/4 "2006-12-15T10:29:22Z")

</div>

> \> "The recent addition of config.plugins will let you specify which \> plugins to load, and which order to load them in." \> ([http://dev.rubyonrails.org/ticket/6418](http://dev.rubyonrails.org/ticket/6418))
> 
> config.plugins prevents the init.rb from being called, meaning the plugins 'lie dormant'. What's the scenario we're trying to prevent by stopping them from appearing on the load paths?

The issue would be where a 'disabled' plugin contains a class with the same name as something lower down in the load path, i.e. a plugin with the file 'struct.rb' will always be loaded rather than one from the Ruby lib paths (which are by default at the end of the load path).

I think it's fairly unlikely that something like this would come up in practice, but it's worth cleaning the load path just for the sake of being a good citizen, and if Rails suggests that you can disable a plugin by not including it in config.plugins, it really should ensure that the plugin has no affect on the running application unless it is enabled.

> Having said that, a patch to tidy all that up, and make config.plugins a little more powerful does seem reasonable.

More than happy to do this. Obrie marked my original patch as a duplicate to his own, although I'm not sure that they have the same effect - it depends on when $LOAD\_PATH.uniq! is called, and the behaviour of Array#uniq!; while at the moment both patches are effective, I think it's just clearer to have a single point where plugin lib directories are dealt with, and that should be in load\_plugin().

I'll repost to this thread with a patch making config.plugins a little more powerful soon.

Cheers!

---

<div class="post-metadata">

**Author:** ![James\_Adam](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@James\_Adam](https://discuss.rubyonrails.org/u/James_Adam)\
**Post date:** [December 15, 2006, 11:26am UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/5 "2006-12-15T11:26:59Z")

</div>

As promised: [http://dev.rubyonrails.org/ticket/6851](http://dev.rubyonrails.org/ticket/6851)

Is there \*any\* chance that this change might be included in 1.2?.... \*grovels\* 😉

I've left out the changes to ensure that plugin lib directories only appear in the $LOAD\_PATH once; I'd suggest you choose from

\* [http://dev.rubyonrails.org/ticket/6842](http://dev.rubyonrails.org/ticket/6842) - remove plugins from the default\_load\_paths \* [http://dev.rubyonrails.org/ticket/6571](http://dev.rubyonrails.org/ticket/6571) - add a trailing slash when loading plugins and then use $LOAD\_PATH.uniq! to clear out duplicates

... they're both tiny & clean patches that should do the job. IMHO #6842 is a cleaner and 'more correct' way to resolve the duplication, but either way the duplicates are cleared.

Cheers guys,

---

<div class="post-metadata">

**Author:** ![Aaron\_Pfeifer](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@Aaron\_Pfeifer](https://discuss.rubyonrails.org/u/Aaron_Pfeifer)\
**Post date:** [December 15, 2006, 6:47pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/6 "2006-12-15T18:47:07Z")

</div>

I agree with James. I'm not really sure why it was added there in the first place (plus it's really not correct either since it doesn't add plugins in subdirectories). So with his solution, we get fix 2 problems with 1 stone.

James Adam wrote:

---

<div class="post-metadata">

**Author:** ![James\_Adam](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@James\_Adam](https://discuss.rubyonrails.org/u/James_Adam)\
**Post date:** [December 15, 2006, 8:32pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/7 "2006-12-15T20:32:01Z")

</div>

It occurred to me on the walk home that our two patches solve slightly different problems; adding the trailing slash to paths added by load\_plugin won't prevent all plugin lib directories being added to the $LOAD\_PATH, it will only prevent duplicates. My original query was with why all the plugin libr directories were being added regardless of the contents of config.plugins...

But anyway, I've rolled in your 'trailing-slash' fix into the patch on Ticket #6842, so that should kill every bird with one tiny pebble 🙂

No brainer patch to apply: [http://dev.rubyonrails.org/ticket/6842](http://dev.rubyonrails.org/ticket/6842)

Cheers

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [December 15, 2006, 11:52pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/8 "2006-12-15T23:52:36Z")

</div>

> No brainer patch to apply: [http://dev.rubyonrails.org/ticket/6842](http://dev.rubyonrails.org/ticket/6842)

Applied this and 6851 to trunk and 1.2. Thanks!

---

<div class="post-metadata">

**Author:** ![Rick\_Olson](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rick\_Olson](https://discuss.rubyonrails.org/u/Rick_Olson)\
**Post date:** [December 27, 2006, 10:30pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/9 "2006-12-27T22:30:38Z")

</div>

Hey guys, I thought we were all ready for a new Rails 1.2 RC, but I just realized I had cache\_classes=true in one of my apps, and it started bombing as soon as I changed it back. So here's the changeset: [http://dev.rubyonrails.org/changeset/5801](http://dev.rubyonrails.org/changeset/5801)

Nicholas' load\_once\_path changes were working, but none of the plugin paths were ever being added to it. #default\_load\_once\_paths in Rails::Initializer wasn't working because it runs before plugins are initialized (and plugin lib paths aren't in default\_load\_paths). So, I just forced an addition to Dependencies.load\_once\_paths in #load\_plugin.

This change seems to work, but it makes it so no plugins reload at all (which has been the current behavior of plugins since the beginning). However, if you do want your plugins to reload, you can just add this to the top of init.rb:

Dependencies.load\_once\_paths.delete(lib\_path)

Any problems with this approach? I just don't think all these plugins have been written to deal with reloading, so I don't want to force folks to restructure them. Maybe this can be changed for Rails 2.0 though...

---

<div class="post-metadata">

**Author:** ![James\_Adam](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@James\_Adam](https://discuss.rubyonrails.org/u/James_Adam)\
**Post date:** [December 28, 2006, 8:24pm UTC](https://discuss.rubyonrails.org/t/plugin-load-paths-load-order/5843/10 "2006-12-28T20:24:03Z")

</div>

Sounds good to me Rick.
