ExecJS coffee script not showing line numbers for

2019-03-15 11:55发布

问题:

In one of my Rails apps, ExecJS is not showing line numbers for coffeescript compilation errors. My compile error message will look like this:

ExecJS::RuntimeError in Referrals#new

Showing ~/MyApp/app/views/layouts/application.html.erb where line #6 raised:

SyntaxError: unexpected IDENTIFIER
  (in ~/MyApp/assets/javascripts/utils.js.coffee)

Note that there's no line number for the coffee script source (line #6 is for the erb file).

On another one of my apps, where I'm still getting the line numbers, a syntax error looks like this:

ExecJS::ProgramError in Projects#show

Showing ~/OtherApp/app/views/layouts/application.html.erb where     line #17 raised:

Error: Parse error on line 6: Unexpected 'STRING'
(in ~/OtherApp/app/assets/javascripts/projects.js.coffee)

So it seems the difference is that ExecJS::ProgramError will give me the line numbers, where as ExecJS::RuntimeError will not.

Anyone have any idea how to get the line numbers back? Why is my app creating RuntimeErrors on coffee asset compilation, while the other is giving ProgramErrors? I've checked Rails and they seem to match.

Note that if I fix the compilation errors, the app runs fine (e.g., the coffee files are actually do get compiled) -- but it'd be nice to have those line numbers point me to the compilation errors!.

EDIT I realized that whether it's an ExecJS Runtimeerror or a ProgramError doesn't seem to matter -- in the first app, there's never a line number given and in the second, there always is.

回答1:

I figured it out -- it's due to the coffee-script-source gem version. In the app that was giving line numbers, bundle show gives coffee-script-source of 1.4.0, whereas the other app had a coffee-script-source version of 1.6.1.

I didn't notice this because the coffee-rails gem doesn't tightly specify this dependency (in both my Gemfiles, I was using gem 'coffee-rails', '3.2'). To get around this, just explicitly specify the coffee-script-source gem version:

gem 'coffee-rails', '3.2.2'
gem 'coffee-script-source', '1.5.0'

and bundle update coffee-script-source.

Hopefully this helps anyone else who might run into this discrepancy.



回答2:

This can also be fixed with this patch that embeds the line information in the error message for the ruby-coffee-script gem.

If you're using source maps, this patch fixes the coffee-rails-source-maps gem.