1

I have a bizarre situation of suddenly being faced with a pre-compile failure and I honestly cannot see the issue for some reason.

I have added a gist with the log and reference files here https://gist.github.com/malagodia/8506722

What confuses me is that I read Heroku recommendations around database connections and have explicitly made sure that application.rb contains

config.assets.initialize_on_precompile = false

Here is the log output error, full log on the gist

Running: rake assets:precompile
       rake aborted!
       undefined method `[]=' for nil:NilClass
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/initializers/database_connection.rb:7:in `block (2 levels) in <top (required)>'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:27:in `each'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/initializers/database_connection.rb:5:in `block in <top (required)>'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `call'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `each'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application/finisher.rb:62:in `block in <module:Finisher>'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/config/environment.rb:5:in `<top (required)>'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:189:in `require_environment!'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/application.rb:250:in `block in run_tasks_blocks'
       /tmp/build_2826537d-5a5c-43f7-b792-45112618d3df/vendor/bundle/ruby/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:54:in `block (2 levels) in define'
       Tasks: TOP => environment
       (See full trace by running task with --trace)  !  !     Precompiling assets failed.  !

 !     Push rejected, failed to compile Ruby app

Additional trace heroku pointed out ~ $ rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile

1
  • Same issue ... ``` NoMethodError: undefined method []=' for nil:NilClass ... /tmp/.../config/initializers/database_connection.rb:7:in block (2 levels) in <top (required)>'j``` ... never found out what it was? Commented May 1, 2014 at 17:55

3 Answers 3

1

I recently had this problem while upgrading from Rails 4 to 4.1.

To solve it I just removed the file database_connection.rb. I've had this file because of a Heroku's article on Concurrency and Database Connections in Ruby with ActiveRecord.

I hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

0

I believe your config value is nil because the database (or anything else in the environment) isn't available at the time you precompile your assets, so you should do your best to avoid anything related to a database at that stage. The situation is described here.

And the solution they describe is simply "Don't do that":

"Once you’ve isolated the error, you must fix it by not relying on environment variables and the database to be present at precompile time."

5 Comments

Thanks for the suggestion. I have attempted to replicate this locally by simply shutting down the database, but simply can't get the same issue.
I don't think that replicates the situation exactly, but regardless, the link provided describes how you can confirm if this is the issue or not. And either way, Heroku guidance is pretty clear that relying on the environment during precompilation is a bad idea.
Thanks again, I did run the proposed method to replicate and unfortunately it does not result in a fatal error. Strangely the advice from Heroku on managing concurrency link introduced this issue, obviously I am missing something in the interaction between unicorn and database_connection
Giving you credit for the answer, it is correct, despite I am confused about why Heroku's own recommendation is not working - when I figure it out (there is always a reason) I will share back
As a follow up, heroku support showed me that the environment is being loaded too early and this causes the issue. I am unfortunately unable to even find an earlier branch, which gives me a different trace, so hard to debug. Pasted trace above
0

Im my case, I forgot to update the extension of my application.css to include .scss. So, I'm guessing, sprockets was trying to compile a .scss file that did not exist; hence the nil error.

I identified the error by running "RAILS_ENV=production bundle exec rake assets:precompile" to compile my assets locally but in the production environment (as per Heroku documentation: https://devcenter.heroku.com/articles/rails-asset-pipeline) and by looking at the last couple lines of output (the output is a little cleaner and quicker than pushing the full app to Heroku):

NoMethodError: undefined method `[]' for nil:NilClass (in /other_stuff/my_app/app/assets/stylesheets/application.css)

I noticed the .css extension and added .scss to fix the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.