unexpected indentation error with favicon_tag after migration to rails 5.0

北慕城南 提交于 2019-12-25 07:31:26

问题


With this HAML part

!!!
%html
    %head
        %title=document_title 
        = favicon_link_tag 'logo16.png'
        %meta{content: form_authenticity_token, name: "csrf-token"}= ''
        ... 

I get the error

SyntaxError: [stdin]:68:1: unexpected indentation

So I played a bit with wrong Identation; then I get other but correct errors

Inconsistent indentation: "\t\t " used for indentation, but the rest of the document was indented using 1 tab.
Inconsistent indentation: "\t " used for indentation, but the rest of the document was indented using 1 tab.
Illegal nesting: content can't be both given on the same line as %title and nested within it.

and stack trace looks like

app/views/layouts/page_print.html.haml:5
haml (4.0.7) lib/haml/parser.rb:174:in `block in tabs'  

but with the unexpected Indentation I get an interesting stack - what does the coffee_script there?:

compile (C:\Users\halfbit\AppData\Local\Temp\execjs20161121-7752-txwa8bjs:6692:19)
eval (<anonymous>:1:10)
C:\Users\halfbit\AppData\Local\Temp\execjs20161121-7752-txwa8bjs:6703:8
C:\Users\halfbit\AppData\Local\Temp\execjs20161121-7752-txwa8bjs:6709:14
require./helpers.exports (C:\Users\halfbit\AppData\Local\Temp\execjs20161121-7752-txwa8bjs:1:102)
Object.<anonymous> (C:\Users\halfbit\AppData\Local\Temp\execjs20161121-7752-txwa8bjs:1:120)
Module._compile (module.js:409:26)
Object.Module._extensions..js (module.js:416:10)
Module.load (module.js:343:32)
Function.Module._load (module.js:300:12)
Function.Module.runMain (module.js:441:10)
startup (node.js:139:18)
node.js:968:3
execjs (2.7.0) lib/execjs/external_runtime.rb:39:in `exec'
execjs (2.7.0) lib/execjs/external_runtime.rb:21:in `eval'
execjs (2.7.0) lib/execjs/external_runtime.rb:46:in `call'
coffee-script (2.4.1) lib/coffee_script.rb:78:in `compile'
sprockets (3.7.0) lib/sprockets/coffee_script_processor.rb:21:in `block in call'
sprockets (3.7.0) lib/sprockets/cache.rb:85:in `fetch'
sprockets (3.7.0) lib/sprockets/coffee_script_processor.rb:20:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/bundle.rb:23:in `block in call'
sprockets (3.7.0) lib/sprockets/utils.rb:196:in `dfs'
sprockets (3.7.0) lib/sprockets/bundle.rb:24:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/base.rb:66:in `find_asset'
sprockets (3.7.0) lib/sprockets/base.rb:73:in `find_all_linked_assets'
sprockets (3.7.0) lib/sprockets/manifest.rb:142:in `block in find'
sprockets (3.7.0) lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'
sprockets (3.7.0) lib/sprockets/path_utils.rb:228:in `block in stat_tree'
sprockets (3.7.0) lib/sprockets/path_utils.rb:212:in `block in stat_directory'
sprockets (3.7.0) lib/sprockets/path_utils.rb:209:in `each'
sprockets (3.7.0) lib/sprockets/path_utils.rb:209:in `stat_directory'
sprockets (3.7.0) lib/sprockets/path_utils.rb:227:in `stat_tree'
sprockets (3.7.0) lib/sprockets/legacy.rb:105:in `each'
sprockets (3.7.0) lib/sprockets/legacy.rb:105:in `block in logical_paths'
sprockets (3.7.0) lib/sprockets/legacy.rb:104:in `each'
sprockets (3.7.0) lib/sprockets/legacy.rb:104:in `logical_paths'
sprockets (3.7.0) lib/sprockets/manifest.rb:140:in `find'
sprockets-rails (3.2.0) lib/sprockets/railtie.rb:50:in `each'
sprockets-rails (3.2.0) lib/sprockets/railtie.rb:50:in `map'
sprockets-rails (3.2.0) lib/sprockets/railtie.rb:50:in `precompiled_assets'
sprockets-rails (3.2.0) lib/sprockets/railtie.rb:35:in `asset_precompiled?'
sprockets-rails (3.2.0) lib/sprockets/railtie.rb:251:in `block (3 levels) in <class:Railtie>'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:359:in `precompiled?'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:363:in `raise_unless_precompiled_asset'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:337:in `digest_path'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:325:in `asset_path'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:102:in `block in resolve_asset_path'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:242:in `block in resolve_asset'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:241:in `each'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:241:in `detect'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:241:in `resolve_asset'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:101:in `resolve_asset_path'
sprockets-rails (3.2.0) lib/sprockets/rails/helper.rb:79:in `compute_asset_path'
actionview (5.0.0.1) lib/action_view/helpers/asset_url_helper.rb:144:in `asset_path'
actionview (5.0.0.1) lib/action_view/helpers/asset_url_helper.rb:311:in `image_path'
actionview (5.0.0.1) lib/action_view/helpers/asset_tag_helper.rb:176:in `favicon_link_tag'
app/views/layouts/page_print.html.haml:5:in `_app_views_layouts_page_print_html_haml___1809589845_126062940'    

Commenting

#= favicon_link_tag 'logo16.png'

no error

any Ideas?


回答1:


I did it, I again debugged Rails - I hate to do this.

The "solution" or better the explanations are:

1.) Sprockets behaves completely different in Rails 5.0 that in Rails 3.2
2.) Sprockets reads and caches the whole assets path with first touch (favicon in this case)
3.) As far as I can see Sprockets tries to (coffee-) compile manifest.js (?)
4.) Any compile error is catched (rescued) to late (I would say in HAML in this case) 
5.) Its therefor extremely misleading to get a - coffeescript indentation error pointing to .HAML or .RB file  

What did I do?

In Sprockes::path_utlis I changed/added a begin/rescue:

# Public: Stat all the files under a directory.
#
# dir - A String directory
#
# Returns an Enumerator of [path, stat].
def stat_directory(dir)
  return to_enum(__method__, dir) unless block_given?

  self.entries(dir).each do |entry|
    path = File.join(dir, entry)
    # puts path.to_s
    begin
    if stat = self.stat(path)
      yield path, stat
    end
    rescue => e
      puts "error #{e} in: "+path
    end
  end       

the output I got:

error SyntaxError: [stdin]:68:1: unexpected indentation in: C:/rbstart/app/assets/javascripts/application.js
error Undefined variable: "$std_width". in: C:/rbstart/app/assets/stylesheets/application.css

Because of caching the error is not always to reproduce.



来源:https://stackoverflow.com/questions/40726338/unexpected-indentation-error-with-favicon-tag-after-migration-to-rails-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!