Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4ruby-on-rails-3.2ruby-on-rails-3.1

error precompiling assets when moving to production


I am trying to move my project to production,
trying to

RAILS_ENV=production bundle exec rake assets:precompile

gives me an error, without specifying in which file it is.

rake aborted!
Sass::SyntaxError: Invalid CSS after "}": expected selector or at-rule, was "}"
(sass):89
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:1207:in `expected'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:1137:in `expected'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:42:in `parse'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/engine.rb:406:in `_to_tree'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/engine.rb:281:in `render'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/sass_compressor.rb:48:in `call'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/sass_compressor.rb:28:in `call'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in `call_processor'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `reverse_each'

how do I findout in which file that error occurs? even with --trace it doesnt tell me.


Solution

  • Found this issue while researching: https://github.com/rails/sass-rails/issues/368. Apparently, you'll have to check each file to find out which one is failing to compile.

    But you can create a rake task to help with the process:

    Just add the following code to a lib/tasks/assets.rake file

    namespace :assets do
    
      desc "Find Sass::SyntaxError files..."
      task find_scss_with_error: :environment do
        files = Dir.glob( Rails.root.join("app", "assets", "stylesheets", "**/*")).grep(/.*\.[css|scss]/)
        files.each do |file|
            print "Trying to compile #{file}..."
            template = File.read(file)
            sass_engine = Sass::Engine.new(template)
            begin
                sass_engine.render
                print "[OK]"
            rescue
                print "[ERROR]"
            end
            puts
        end
      end
    
    end
    

    and run

    rake assets:find_scss_with_error
    

    Results:

    Trying to compile /data/ruby/scss-comments-failure/app/assets/stylesheets/example.css...[ERROR]
    Trying to compile /data/ruby/scss-comments-failure/app/assets/stylesheets/application.css...[OK]
    

    Some notes:

    • It'll check for *.css and *.scss files, including subdirectories.
    • The script will not output compression to file. It's just a in-memory test.