config.assets.precompile ignored with recommended settings #10428

iamveen opened this Issue May 2, 2013 · 15 comments


None yet
8 participants

iamveen commented May 2, 2013

config.assets.precompile is ignored if using the suggested format from rails guides.

config.assets.precompile += ['test.js']   # ignored
config.assets.precompile << 'test.js'  # works

Both pieces of code produce the same array, but in the former, additions are not compiled.

I made a new app to verify something wasn't amiss in my larger app, and the same problem occurs.

Rails 4.0.0.beta1
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]

# default production.rb

pipe-test ❯ RAILS_ENV=production rake assets:precompile
[#<Proc:0x007ff48c920350@/Users/gdunne/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/sprockets-rails-2.0.0.rc4/lib/sprockets/railtie.rb:39 (lambda)>, /(?:\/|\\|\A)application\.(css|js)$/]
INFO -- : Writing .../public/assets/rails-d8fb6a2617998f32edf5edb354fc5326.png
INFO -- : Writing .../public/assets/application-d4a508061cd56ce9e544945495285e0c.js
INFO -- : Writing .../public/assets/application-fa5fb97555c828140aa155ca341aaff3.css

# production.rb as per:
# config.assets.precompile += ['test.css', 'test.js']

pipe-test ❯ RAILS_ENV=production rake assets:precompile                                                                             ⏎
[#<Proc:0x007f884a8a4358@/Users/gdunne/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/sprockets-rails-2.0.0.rc4/lib/sprockets/railtie.rb:39 (lambda)>, /(?:\/|\\|\A)application\.(css|js)$/, "test.css", "test.js"]
INFO -- : Writing .../public/assets/rails-d8fb6a2617998f32edf5edb354fc5326.png
INFO -- : Writing .../public/assets/application-d4a508061cd56ce9e544945495285e0c.js
INFO -- : Writing .../public/assets/application-fa5fb97555c828140aa155ca341aaff3.css

# production.rb:
# ['test.css', 'test.js'].each do |asset|
#  config.assets.precompile << asset
# end

pipe-test ❯ RAILS_ENV=production rake assets:precompile
[#<Proc:0x007fd7c4338328@/Users/gdunne/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/sprockets-rails-2.0.0.rc4/lib/sprockets/railtie.rb:39 (lambda)>, /(?:\/|\\|\A)application\.(css|js)$/, "test.css", "test.js"]
INFO -- : Writing .../public/assets/rails-d8fb6a2617998f32edf5edb354fc5326.png
INFO -- : Writing .../public/assets/application-d4a508061cd56ce9e544945495285e0c.js
INFO -- : Writing .../public/assets/test-a3b89b66a8471f3e75abb239e3b35c7d.js
INFO -- : Writing .../public/assets/application-fa5fb97555c828140aa155ca341aaff3.css
INFO -- : Writing .../public/assets/test-a3b89b66a8471f3e75abb239e3b35c7d.css

pbrisbin commented May 5, 2013

Indeed, the following test case fails:

    test "precompile allows for += syntax" do
      add_to_config "config.assets.precompile += [ 'something.js' ]"



pftg commented May 8, 2013

@pbrisbin you forget to add: app_file "app/assets/javascripts/something.js.erb", "alert();"

Have not reproduce it for master and for Ruby 1.9.3 and 2.0.0 by adding tests. Will investigate more.


pftg commented May 8, 2013

Ok, found the problem: this happen only when config.assets.precompile += ['test.css', 'test.js'] added in environments file like production.rb. I think, Sprockets already use reference for the array before environment invoked.

iamveen commented May 8, 2013

That makes sense. It never occurred to me that += returns a new array, while << appends to the existing one.

pftg referenced this issue in rails/sprockets-rails May 9, 2013


Read assets options changed in environment #50

2 wasted days, and an << fixes my issue. Dumb bug.

guilleiguaran was assigned Jun 3, 2013


dhh commented Jun 3, 2013

If you had this problem, please help test the fix: rails/sprockets-rails#63


pftg commented Jun 3, 2013

@dhh I added tests for this bug in #10537 and rails/sprockets-rails#50 but they have not been reviewed :(


guilleiguaran commented Jun 3, 2013

@pftg can you check if rails/sprockets-rails#63 is working fine with your apps?

@pftg pftg added a commit to jetthoughts/rails that referenced this issue Jun 20, 2013

@pftg pftg #10428: Added tests for config.assets.precompile
Tests for bug in sprocket-rails:
  do not use value of configuration options
  which changed after environment loaded

@guilleiguaran guilleiguaran added a commit that referenced this issue Jun 21, 2013

@guilleiguaran guilleiguaran Merge pull request #10537 from jetthoughts/10428_bug_with_config_asse…

 #10428: Added tests for config.assets.precompile

enwood commented Sep 10, 2013

I just generated a new, blank app with Rails 4.0.0 and see that production.rb is still generated with the following comment:

 # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( search.js )

Not being intimately acquainted with Sprockets and precompilation, and having tried to chase this thread of why specified assets in environment config files aren't being precompiled, is the "+=" syntax approach still/now acceptable? What about @iamveen's comment above that "that += returns a new array, while << appends to the existing one".

Is white-listing assets in the environment configuration files now working correctly for Rails 4.0.0? And, by what means should it be made to work correctly for Rails 3.2.x?


pftg commented Sep 10, 2013

May you share your application on github with this problem?

enwood commented Sep 10, 2013

Hi Paul;

I'm afraid I can't. It's not that I have an immediate problem; I've just been trying to sort out what the correct approach is now and what has been fixed. In my Rails 3.2.12 app, this approach:

config.assets.precompile += w%[  application-print.css ]

didn't work (application-print.css was not precompiled, nor copied to public/assets), but this approach:

config.assets.precompile << 'application-print.css' 

does work.

So, I am surprised to see a new Rails 4.0.0 app still generating the "+=" syntax. Has Rails 4.0.0 (or Sprockets) been fixed to correctly handle this now? I'm just trying to unravel this very complex thread and related issues.



rafaelfranca commented Sep 12, 2013

Have you tried using 4-0-stable?

enwood commented Sep 12, 2013

Hi Rafael;

No, it's not that I have a problem with this. I'm just trying to ascertain what the CORRECT approach is now with both Rails 3.2.x and Rails 4.0. Please see the questions in my previous posting.



rafaelfranca commented Sep 12, 2013

The correct approach is what it always was, using +=.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment