Why are my Rails assets getting precompiled twice? - ruby-on-rails

I've noticed my assets seem to get compiled twice, which considerably slows down my deployment, as this step is the most time consuming part:
~/projects/rewportal(mapwidget ✔) rake assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194#rewportal/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/application-5170f52c1dd49cb382d5135bee01d75e.js
[...]
Fetching files to flag for delete
Flagging 8 file(s) for deletion
Deleting: assets/active_admin-4ce46d089d4b0080e87c9abcb6fa6c97.css
[...]
AssetSync: Done.
It this normal?
When I precompile to other environments (non-production), I can see the detailed compilation of each asset twice:
~/projects/rewportal(mapwidget ✔) rake RAILS_ENV=qa assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/home/ruy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /home/ruy/.rvm/gems/ruby-1.9.3-p194#rewportal/bin/rake assets:precompile:all RAILS_ENV=qa RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
Compiled gmaps4rails/gmaps4rails.base.js (141ms) (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js (148ms) (pid 8480)
[...]
Compiled active_admin.css (1299ms) (pid 8480)
Compiled active_admin/print.css (113ms) (pid 8480)
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
AssetSync: using /home/ruy/projects/rewportal/config/initializers/asset_sync.rb
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
Compiled gmaps4rails/gmaps4rails.base.js (133ms) (pid 8480)
Compiled gmaps4rails/gmaps4rails.googlemaps.js (133ms) (pid 8480)
[...]
Compiled active_admin.css (1290ms) (pid 8480)
Compiled active_admin/print.css (116ms) (pid 8480)
AssetSync: Syncing.
Using: Directory Search of /home/ruy/projects/rewportal/public/assets
Uploading: assets/active_admin-d05b61ab8366b74eabc9074d3e60fe82.css.gz
[...]
Fetching files to flag for delete
Flagging 6 file(s) for deletion
Deleting: assets/active_admin-ec90e7d9a9f45f14d1387f58fa1452b4.css
[...]
AssetSync: Done.
My application.rb has the following:
config.assets.precompile += %w( active_admin/print.css active_admin.css active_admin.js admin.js admin.css html5shiv.js )
Ideas?

Rails 3 by default compiles once to generate fingerprinted assets, and once to generate non-fingerprinted assets (the fingerprinted ones have the MD5 hash in the filename).
You can use the turbo-sprockets-rails3 gem to create both from one compilation.
In Rails 4, this functionality was extracted into the sprockets-rails gem and the behavior was changed, so the double compilation does not happen in Rails 4.

Are you using capistrano? If so, you can try to compile your assets locally, and then upload to server, with a task like this
namespace :deploy do
namespace :assets do
desc "Precompile assets on local machine and upload them to the server."
task :precompile, roles: :web, except: {no_release: true} do
run_locally "bundle exec rake assets:precompile"
find_servers_for_task(current_task).each do |server|
run_locally "rsync -vr --exclude='.DS_Store' public/assets #{user}##{server.host}:#{shared_path}/"
end
end
end
end

I overwrite the deploy_all task inside deploy.rb to compile before uploading:
task :remake_all do
puts "precompiling assets"
res = `env rake assets:precompile:all RAILS_ENV=precompile RAILS_GROUPS=assets 2>&1`
$stderr.puts "assets res is: #{res}"
if res =~ /aborted|don't|invalid|segmentation|bug/i
puts "############ Unable to compile assets #########"
exit
end
end
I had issues with the development environment not compiling properly and the production environment needing access to mysql servers behind a firewall, so made a new environment called :precompile

The solution for me (Rails 3) was to run rake assets:precompile:primary instead of rake assets:precompile:all.

Related

Rails: "rake aborted! Sass::SyntaxError: File to import not found or unreadable"

I'm using Rails 4.2.
In my rails project directory, I have a frontend directory:
/railsproject/frontend
/railsproject/frontend/styles
/railsproject/frontend/styles/main.scss
I have added config.assets.paths << Rails.root.join("frontend","styles") to application.rb.
In /railsproject/app/assets/stylesheets/application.css.scss:
#import "main.scss";
Everything works fine in Development mode.
But when I try to precompile for production, it fails:
$ RAILS_ENV=production bundle exec 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
rake aborted!
Sass::SyntaxError: File to import not found or unreadable: main.scss.
(sass):17
/Users/max/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/sass-3.4.15/lib/sass/tree/import_node.rb:67:in `rescue in import'
/Users/max/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/sass-3.4.15/lib/sass/tree/import_node.rb:45:in `import'
/Users/max/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/sass-3.4.15/lib/sass/tree/import_node.rb:28:in `imported_file'
/Users/max/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/sass-3.4.15/lib/sass/tree/import_node.rb:37:in `css_import?'
etc...

Ruby on Rails in production error

I've just put my Rails app online in production. And I kind of have two problems:
I get a 500 on my website for which the production.log says:
Completed 500 Internal Server Error in 10.0ms
ActionView::Template::Error (./icons/icon_nameplate.png isn't precompiled):
For this what I tried is running the following command:
bundle exec rake assets:precompile
This didn't work. I want to say that when I run the server locally on my computer with rails server everything works just fine.
Output of bundle exec rake assets:clean assets:precompile --trace:
** Invoke assets:clean (first_time)
** Execute assets:clean
/usr/bin/ruby1.9.1 /usr/local/bin/rake assets:clean:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:clean:all (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:clean:all
rm -rf /home/celliptic/public/assets
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/bin/ruby1.9.1 /usr/local/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
The second problem may be why the first one didn't get corrected after precompilation. It's just that any modification on the server isn't directly reflected on the website. Is it normal? Is it because of some kind of proxy caching my website?
Thank you for your answers!
Try running:
bundle exec rake assets:clean assets:precompile --trace
then spawn it if you are using passenger:
touch tmp/restart.txt
then check again.
The error you have: ActionView::Template::Error (./icons/icon_nameplate.png isn't precompiled), to me, seems like you're trying to load a static asset when it's not there.
I would look at your views or CSS to where you referenced icon_nameplate.png - I believe the problem will likely be that you're trying to reference the file directly, and as it has not been compiled, it's unavailable. The proof of this would be that it's referencing a static (non fingerprinted) link
After you've followed tungsten_carbide's directions, please let us know what happens
Hi what webserver you are using? and under which Linux?
try those steps:
see if you got the permissions to write the compiled assets.
you sure you include it under application.css?
paste your complete css file maybe you use methods like asset-url which can cause the error you talk about.
Thanks everyone for your help. It works well now, but I don't really know which command made the deal. I've made:
bundle exec rake assets:clean assets:precompile --trace
touch tmp/restart.txt
Changed config.assets.compile = true in production.rb
At this moment it didn't work, so I went to sleep. Woke up, load my website and it worked
So maybe it needed time to activate the changes or I don't know.
Still thank you for your help.

spree 2.0.3 heroku error precompiling assets "Error: Out of stack space"

I'm trying to get a basic spree application working on Heroku. I'm following http://railsapps.github.io/rails-heroku-tutorial.html and the spree heroku guide.
I have edited config/application.rb to include config.assets.initialize_on_precompile = false
Then, because I need to precompile before I push to Heroku, I get stuck here:
D:\code\foo>rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
d:/RailsInstaller/Ruby1.9.3/bin/ruby.exe d:/RailsInstaller/Ruby1.9.3/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
Error: Out of stack space
(in D:/code/foo/app/assets/javascripts/admin/all.js)
d:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/execjs-.4.0/lib/execjs/external_runtime.rb:68:in `extract_result'
...much more...
The all.js asset is unchanged from the original installation:
Running
rake assets:clean
before the precompile fixed this for somebody else, but not for me.
Browsing around it looks like this might be a windows specific issue? If it matters I used railsinstaller and I'm running Windows 7 on fairly new laptop.
Then, because I need to precompile before I push to Heroku
This is false. By default, Heroku's Cedar stack compiles your assets during slug compilation when you push to Heroku.
For more information see: https://devcenter.heroku.com/articles/rails-asset-pipeline

rails heroku giving me We're sorry something went wrong error, gmaps4rails.css isn't compiled

I checked my heroku logs and the only thing that I can see that's giving me errors is
2013-07-04T21:32:14.887388+00:00 app[web.1]: ActionView::Template::Error (gmaps4rails.css isn't precompiled):
and then a few lines down I get
2013-07-04T21:32:14.888116+00:00 app[web.1]: Completed 500 Internal Server Error in 767ms
I'm not sure what is causing the error but maybe if I fix this issue, I can test to see if I'm still getting the error. In my confi/application.rb file I have this:
config.assets.initialize_on_precompile = false
config.assets.precompile += %w( gmaps4rails.css )
config.assets.compile = true
I'm not sure what is causing the error.
Also, I've tried restarting my heroku server, using heroku rake db:migrate and still getting the same errors.
Thanks
EDIT:
Added output from rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/Users/andrewliu/.rvm/rubies/ruby-1.9.3-p392/bin/ruby /Users/andrewliu/.rvm/gems/ruby-1.9.3-p392#global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
Given the comments above I think you should
Remove this line from enviroment
config.assets.precompile += %w( gmaps4rails.css )
Remove this line from layout
<%= stylesheet_link_tag 'gmaps4rails' %>
Check if in applications.css you have this file included explicitly or require_tree .
because if you have gmaps4rails.css in app/assets/stylesheets then it should be precompiled with other assets.
EDIT: Explanation
What was the problem really? Two things.
First you ask Rails to add a file to asset compilation, but you should only do it if file is outside app/assets directory (ex. in gem)
Second, in layout you wanted to add a stylesheet link to this file but it was not compiled separately but as a part of application.css

Manifest.yml empty

Whenever I run rake assets:precompile, I get a manifest.yml file that looks like:
--- {}
Something must be going wrong. Here is my output for rake assets:precompile --trace:
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/Users/user/.rvm/rubies/ruby-1.9.3-p194/bin/ruby /Users/user/.rvm/gems/ruby-1.9.3-p194#global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
** Invoke assets:precompile:nondigest (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:nondigest
Can someone help?? Thanks!
UPDATE:
Feel free to look at my code at www.github.com/sambaek/novulty
When I run rake assets:precompile for your application I get the following output
➜ novulty git:(master) ✗ rake assets:precompile
/Users/deefour/.rbenv/versions/1.9.3-p125/bin/ruby /Users/deefour/.rbenv/versions/1.9.3-p125/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
Undefined variable: "$baseLineHeight".
(in /Users/deefour/.rbenv/versions/1.9.3-p125/gemsets/novulty/gems/bootstrap-sass-2.1.0.1/vendor/assets/stylesheets/bootstrap/_accordion.scss)
Tasks: TOP => assets:precompile:primary
(See full trace by running task with --trace)
rake aborted!
Command failed with status (1): [/Users/deefour/.rbenv/versions/1.9.3-p125/...]
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
rake assets:precompile 12.50s user 1.21s system 99% cpu 13.744 total
I'm not sure what your intention is, but you have
app/asssets/bootstrap.min.css being included in app/assets/application.css
gem 'bootstrap-sass' in your Gemfile which appears to cause bootstrap-sass-2.1.0.1/vendor/assets/stylesheets/bootstrap/_accordion.scss (among other files) to be added to your pipeline even though they're not included in require lines in app/assets/application.css
It seems like you have the bootstrap gem included to use it's Javascript plugins, but are trying to omit the SASS files in favor of the hard-coded, minified CSS file?
In any event, the error I get says that the $baseLineHeight SASS variable is not defined for a line in that gem file's asset - an asset you otherwise appear to ignore (from the best I can tell).
I then did the following
Commented out the bootstrap-sass gem in your Gemfile
#gem 'bootstrap-sass'
Ran bundle
Ran rake assets:clean
Removed the following require lines from your app/assets/application.js because they were coming from the bootstrap-sass gem I removed in 1. above
//= require bootstrap-transition
//= require bootstrap-button
//= require bootstrap-carousel
//= require bootstrap-collapse
//= require bootstrap-tab
Ran rake assets:precompile
This results in the following public/assets/manifest.yml.
https://gist.github.com/d29f70b047c7b0606263
I think you need to either of the following
Use the bootstrap-sass gem as intended. Include the SASS files in your app/assets/application.css (doing this it a bit outside the scope of this question)
Get rid of the bootstrap-sass gem as I have above, and include the necessary Javascript files as assets in your vendor/assets/javascripts directory (which is where app/assets/stylesheets/bootstrap.min.css belongs too - vendor/assets/stylesheets)

Resources