Initializer should load files in parent directories before subdirectories to minimize dependency pain
Reported by Tom Ward | August 19th, 2008 @ 12:12 PM | in 2.x
The problem
I have the following files:
app/models/zoo.rb
class Zoo < ActiveRecord::Base
include HippoManangement
end
app/models/zoo/hippo_management.rb
module Zoo::HippoManagement
end
What happens
On starting the application up in some modes (but not all), I get a dependencies error, similar to 'Expected app/models/zoo/hippo_management.rb to define Zoo::HippoManagement'. This is cause by require_dependency being called on app/models/zoo/hippo_management.rb before app/models/zoo.rb.
The code calling require_dependency is in Rails::Initializer#load_application_classes:
def load_application_classes
if configuration.cache_classes
configuration.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").each do |file|
require_dependency file.sub(matcher, '\1')
end
end
end
end
The problem is that the Dir.glob enumerates app/models/zoo/hippo_mangement.rb before app/models/zoo.rb.
The solution?
One way to get around this would be to sort the globbed files before requiring them. Another (far more involved) solution would be to fix the dependencies code so it doesn't fall over in this situation.
Comments and changes to this ticket
-
Tom Ward August 19th, 2008 @ 01:29 PM
- → Tag changed from dependencies eager_loading to dependencies eager_loading patch
I've written a patch (with tests) which sorts the globbed files. Having thought about this in some depth, I'm convinced that eager loading should load parents before children.
In my particular situation (using modules to partition behaviour) I'm aware there are alternative solutions (such as Rick's concerned_with) but I believe using modules is not only simpler but more flexible. In any case, eager loading breaks the concerned_with pattern as it forces all concerns to be loaded, whether declared or not.
-
-

Repository August 22nd, 2008 @ 01:09 PM
- → State changed from new to resolved
(from [89d1c77dd012f087c091e0f23874c582ea4e3703]) Initializer to sort files before eager loading. [#859 state:resolved]
Changed Rails::Initializer to sort files before eager loading them. This ensures that any files in a parent directory will be loaded before files in a subdirectory of the 'same' name. i.e. zoo.rb will be loaded before zoo/reptile_house.rb
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..
