New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
after_initialize hooks sometimes get executed during initialization #1587
Comments
Update: it turns out that Cells does not break the initialization order just by accident but because it explicitly wants to run its initializers after Finisher's :set_routes_reloader. From Cells' railtie: module Cells
class Railtie < Rails::Railtie
options = {}
options[:after] = :set_routes_reloader if Cells.rails3_1?
...
<snip>
...
end
end While Cells might be better served by using :after_initialize hook rather than an initializer, this issue shows that :finisher_hook lacks some constraints. Other initializers, even those that are expected to run before it, may push it down in the execution order. More details here: pull request 1602 |
@cameel Is there still an issue, or can this be closed? |
The issue with Cells is fixed - it has changed its initializers. I think that it's still possible to break initialization order though. There's nothing that forces finisher hook to be executed as the last initializer so some combinations of constraints may force other initializers to appear after it. |
Since this issue was against 3.1 rc versions, and apparently the issue with Cells was fixed, I'm going to close this. Please let us know if you notice any other issue related to this in Rails, and make sure to test latest stable version in this case, and I'll be glad to reopen the discussion. Thanks! |
I am using Cells plugin and I have noticed that in Rails 3.1.0 (checked with both RC1 and RC2) asset generation fails silently when the plugin is declared as dependency in Gemfile. There are no error messages in the log file or on the console (when running with rake assets:precompile). public/assets directory just does not get created.
I've dug into the issue and there doesn't seem to be anything wrong with Cells. The plugin just adds two initializers and this makes a deeper problem surface. Assets are not being compiled because config.assets.paths is still empty when Sprockets' :after_initialize hook is executed and that hook expects them to be there already. The initializer that sets the paths, surprisingly, runs after the hook.
The problem is that initializers from Rails::Application::Finisher (and in particular :finisher_hook that executes :after_initialize hooks) are not guaranteed to run after all the other initializers like they are expected to.
Finisher is properly placed at the end of the list when the list is returned by Rails::Application.initializers()
but the list is later tsorted in Rails::Initializable::Initializer.run_initializers():
Some initializers are constrained with :before/:after parameters that make tsort order them correctly. There are none in Finisher though.
Indeed, in an empty application (generated with rails new) everything is ok on my system: Finisher's initializers are lucky and stay at the end even after the tsort.
But when we add Cells to the Gemfile, its initializers and the ones from from Rails::Engine (including :append_assets_path that sets config.assets.paths) are placed after Finisher's ones.
The text was updated successfully, but these errors were encountered: