Rails: finding out which file causes Sass::SyntaxE

2019-08-13 11:11发布

问题:

When I run:

rake assets:precompile RAILS_ENV=production --trace

I get the following exception:

rake aborted!
Sass::SyntaxError: Invalid CSS after " */": expected "}", was ""
(sass):19419
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:1179:in `expected'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:1115:in `expected'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:1110:in `tok!'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:656:in `block'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:647:in `ruleset'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:673:in `block_child'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:666:in `block_contents'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:123:in `stylesheet'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb:39:in `parse'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/engine.rb:403:in `_to_tree'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/engine.rb:278:in `render'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/sass_compressor.rb:48:in `call'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/sass_compressor.rb:28:in `call'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:60:in `block in load'
/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'
....

How can I find out the file that causes this exception? Is there an option in sass-rails to debug this issue?

Note: the CSS/Javascript of this application is working in development mode. This exception only happens when precompiling assets in production or when running rspec features tests.

Note 2: this issue only happens when config.css_compressor is enabled. If I disable it, the assets are precompiled without issues.

回答1:

I had the exact same problem. The output only tells you what line it fails on and not the actual filename. Really simple quick & dirty fix is to open the file throwing the error "/Users/fernando/.rvm/gems/ruby-2.3.0/gems/sass-3.4.21/lib/sass/scss/parser.rb" and add a puts to the initializer:

  def initialize(str, filename, importer, line = 1, offset = 1)
   puts filename # ADD THIS LINE.
   @template = str
    @filename = filename
    @importer = importer
    @line = line
    @offset = offset
    @strs = []
    @expected = nil
    @throw_error = false
  end

This will create a ton of output but when it fails you will have the filename. Alternatively, since the filename variable gets put in to the instance variable @filename you can get the line number from the first line in the backtrace (in your case 1179) and add the puts statement using the instance variable to the line before the error:

 line 1178: puts @filename


回答2:

I couldn't find a way to make Rails give me more information about which file was causing the problem.

So the solution was to write a script that removes a CSS file, run assets:precompile and check the return code. I ran this script looping on all css/scss files until assets:precompile finally worked. It was a 3rd party CSS file that caused the issue.

I had to manually check the file until I found some comments with // which aren't valid for CSS.

After removing those comments, rake assets:precompile worked with all files.