rails/rails — Issue #4262: app/models, app/controllers, ... not always in load_paths
Author: lfarcy
Created: 2012-01-02 17:17:56+00:00
State: open

While transitioning a Rails app from 2.3.5 to 3.0.11, I stumbled upon an unexpected situation where Rails.root/app/models, Rails.root/app/controllers, ... were no more in $LOAD_PATHS.

After searching a bit and comparing Rails 2.3.5 and 3.0.11 codebase, I realized this was due to the way we use config.eager_load_paths.

* With Rails 2.3.5, eager_load_paths can be emptied but paths under RAILS_ROOT/app stay on the load paths.
* With Rails 3.0.11, eager_load_paths can still be emptied (for startup optimization) but it has a side effect: paths under Rails.root/app are no more in the load paths.

This is due to how those paths are now declared in Rails::Engine::Configuration

```ruby
paths.app "app", :eager_load => true, :glob => "*"
paths.app.controllers "app/controllers", :eager_load => true
paths.app.helpers "app/helpers", :eager_load => true
paths.app.models "app/models", :eager_load => true
paths.app.mailers "app/mailers", :eager_load => true

paths.lib "lib", :load_path => true
```
which means only Rails.root/lib is sure to be in the load paths. Other paths get on the load paths because they are eager loaded as "specified" by Rails::Engine.

```ruby
def _all_autoload_paths
@_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end

def _all_load_paths
@_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
end
```

A possible fix is

```ruby
paths.app "app", :load_path => true, :eager_load => true, :glob => "*"
paths.app.controllers "app/controllers", :load_path => true, :eager_load => true
paths.app.helpers "app/helpers", :load_path => true, :eager_load => true
paths.app.models "app/models", :load_path => true, :eager_load => true
paths.app.mailers "app/mailers", :load_path => true, :eager_load => true

paths.lib "lib", :load_path => true
```
which prevents app/ paths from being accidentally removed from load_paths.

Not a major annoyance but surely an unattended behaviour.