12

Started working on a new application this week that is running the latest rails 3.2.2 with the asset pipeline. For the assets I would like the assets to be compiled when pushing the app to heroku (rather than having to manually compile and store compiled assets in the repo). The docs suggest this should happen automatically.

The problem I have is when I run git push heroku master it does not seem to run the rake assets:precompile task at all. The assets are never compiled. Here is the output I get:

-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.1.rc.7
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       Using rake (0.9.2.2)
       .......
       .......
     Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
-----> Discovering process types
       Procfile declares types      -> web
       Default types for Ruby/Rails -> console, rake, worker
-----> Compiled slug size is 61.7MB
-----> Launching... done, v30
       [appname] deployed to Heroku

I have the asset pipeline enabled in my application.rb

require File.expand_path('../boot', __FILE__)

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"

Bundler.require *Rails.groups(:assets) if defined?(Bundler)

module Appname
  class Application < Rails::Application
    # ...

    # Enable the asset pipeline
    config.assets.enabled = true

    # Do not boot the app when precompiling assets
    config.assets.initialize_on_precompile = false
  end
end

Anyone know what might be causing the problem? I'm happy to gist more code if need be. If I run heroku info it shows that i'm running on the Cedar stack.

I had to turn on config.assets.compile = true to stop receiving 500 errors in production, although this compiles the assets at runtime (which I do not want).

Jak
  • 2,893
  • 3
  • 19
  • 33
Mario Visic
  • 2,653
  • 1
  • 22
  • 29
  • 1
    If Heroku find a manifest.yml file in public/assets/ then they will assume you are managing assets yourself and not try to do a compile - could this be the case here? – John Beynon Mar 13 '12 at 16:15
  • I have a ticket open with Heroku on this. I suggest you do the same. Mine has been escalated to the Ruby team. The more noise, the faster this gets attention, hopefully. – Mike Mar 14 '12 at 14:34
  • @JohnBeynon oh yeah, i checked this before creating the question, I believe it is in the docs somewhere. no manifest.yml files in the repo anywhere unfortunately, must be something else. – Mario Visic Mar 14 '12 at 15:58
  • @Mike have created a ticket, see how we go :) – Mario Visic Mar 14 '12 at 16:01
  • not that it helps here but I have a Rails 3.2.2 that works just as I'd expect with assets compilation on Heroku Cedar. – John Beynon Mar 14 '12 at 16:29
  • @JohnBeynon odd; is there anything in your application.rb that differs from mine? – Mario Visic Mar 14 '12 at 17:34
  • Can you run this in a fresh `rvm gemset` ? With only development gems installed? `bundle install --without development test` – Schneems Mar 15 '12 at 00:59
  • only bit I can see that is different is https://gist.github.com/2042899 – John Beynon Mar 15 '12 at 08:18

8 Answers8

10

This seems to be solved now. I believe it was caused by the Rakefile not loading the assets:precompile task in production.

The rakefile was loading up tasks for both the cucumber and rspec gems which are not bundled into production. As a result the assets:precompile task was not available in production so heroku didn't attempt to run it.

I wrapped the test tasks in a environment check conditional like so:

if %(development test).include?(Rails.env)
  require 'rspec/core'
  require 'rspec/core/rake_task'
end
Mario Visic
  • 2,653
  • 1
  • 22
  • 29
  • sry, that didn't make any sense to me. Did you make any code changes? – oma May 14 '12 at 12:24
  • Thanks for this discovery!!.. I was wondering what was going on myself as it worked fine 30 minutes ago on my project!. But I had added a new rake task that depended on rspec which caused this issue. For me I simply wrapped the contents of the file in a begin/rescue block. – Urkle Jun 16 '12 at 05:33
6

If there's anything wrong in your Rakefile then the precompile step will be skipped entirely. For me, I was referring to a dependency that was only being loaded in my test and development environments and not production so the rakefile was bombing.

Using the suggestion from @Lecky-Lao worked for me.

heroku run rake -T --app staging-hawkmo
Running `rake -T` attached to terminal... up, run.1
rake aborted!
cannot load such file -- jasmine-headless-webkit

Once I saw that it was as simple as wrapping the Rakefile require in an environment test.

Andrew Chase
  • 91
  • 1
  • 5
4

I normally add this to production.rb:

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w( style.css grid.css ... geo.js jquery.flexslider-min.js)

and set:

config.assets.compile = false
Simon Bagreev
  • 2,879
  • 1
  • 23
  • 24
  • If I set compile to false in production then I just get 500 errors as the assets are not compiled. Adding additional assets to the precompile list did not help unfortunately. – Mario Visic Mar 13 '12 at 15:23
  • 1
    Sorry, I think you need to run `bundle exec rake assets:precompile` with the code above. And then push it to master. But you gonna have to do it every time you change anything in your assets. – Simon Bagreev Mar 13 '12 at 16:12
  • 1
    Heroku is supposed to auto-compile assets, as stated by @mario, but doesn't. That's also how I read the documentation. – awendt Mar 13 '12 at 16:28
  • 1
    Interestingly, I'm upgrading my app to Rails 3.2 from 3.1 today, also. With 3.1, the assets:precompile phase was executed during slug compilation. With 3.2, I'm not seeing that ever being executed. – Mike Mar 13 '12 at 20:27
  • 1
    @awendt is right. I don't want to have to manually compile the assets and then add them to the git repo, the docs suggest heroku should automatically do this. Mike perhaps it is the same issue? – Mario Visic Mar 14 '12 at 15:57
1

Just having the same issue on both Rails 3.1.3 and Rails 3.2.3. Following Mario's idea, The rake task do exist while I run "heroku run rake -T". So anyone got feedback from Heroku's feedback yet? I might just raise ticket for this as well then...

Lecky Lao
  • 359
  • 3
  • 7
  • My problem was because gems like rspec and cucumber were not being bundled in production, the Rakefile would not load correctly. I had to rescue load errors in production so the rakefile would load correctly. – Mario Visic Apr 05 '12 at 07:10
  • +1 for the heroku run rake -T suggestion. Tracked my deploy problem to a wonky rake task. – Andy Davis Mar 28 '13 at 16:08
1

It appears that this results from something dying because of a fatal error when heroku attempts to start/check for assets. This may not actually be related to Rails 3.2. I don't know.

I didn't have time to wait for heroku to help so I went through the painful process below to determine what the problem was. I recommend you do it also.

  1. Create a new, separate rails 3.2.2 app. There is nothing in it besides the default files. Create a heroku app: heroku create --stack cedar. Just for your sanity, push out this app as it stands and see that the precompilation step happens during slug compilation.

  2. Copy into the new app, each logical block of your application. I started with app/assets. (thinking it was something to do with assets and precompilation... nah) cp -R ../ProblemApp/app/assets/* app/assets/ Then add, commit, and push this to the heroku git remote.

  3. At some point, you are going to copy over a group of files that will stop the assets:precompile message from appearing during slug compilation. Now, one by one, revert those files to the original raw state or comment out suspicious lines and push back out to heroku. Once you see the precompilation message again, you've found your problem.

It turned out that for us, I had several false alarms. When I copied the config directory over to the new app, the app module name was our old app and not the new one. This also prevented the precompilation from happening, but it was not the original cause I was looking for.

In the end, we had a rake task that was responsible for loading CSV data into the app during development. It had one line that was causing a problem:

require "#{Rails.root.to_s}/config/environment"

Commenting out this line in our original app fixed our problem. So is this Rails 3.2 related? Why is heroku running unrelated rake tasks? I dunno. But shit's working. :)

Mike
  • 8,853
  • 3
  • 35
  • 44
  • Thanks @Mike i'll see if I can give some of this a go until I can figure it out. – Mario Visic Mar 15 '12 at 07:13
  • This worked for me. I found that the right way to do it is making tasks depend on :environment, as discussed here: http://stackoverflow.com/questions/876396/do-rails-rake-tasks-provide-access-to-activerecord-models – wiz Aug 16 '12 at 10:18
0

I had the same problem I following this guide https://devcenter.heroku.com/articles/rails-asset-pipeline fixed it.

Regards.

pabloverd
  • 614
  • 8
  • 8
0

Try to add the following into production.rb

config.assets.precompile = ['*.js', '*.css']

I also found this site https://coderwall.com/p/ga9l-a

stanicmail
  • 743
  • 7
  • 19
0

Rename your css to .css.erb and use the asset_path tag in your rule.

E.g.

background: url(<%= asset_path 'bg.png'%>); 

As mentioned in 2.3.1 http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

H.Rabiee
  • 4,747
  • 3
  • 23
  • 35