Rails 3.1 Possible Bug in Asset Pipeline and Uglifier - ruby-on-rails-3.1

I ran into a problem deploying on Heroku do to a failure in the rake task
rake assets:precompile
At the bottom is the error I get if I integrate
Rails 3.1
Jquery calendar: https://github.com/themouette/jquery-week-calendar
Twitter bootstrap
The error happens from uglifier.
I suspect that problem could be related to the inclusion of many localizations for the calendar.
I worked around the error by setting:
# Compress JavaScripts and CSS
config.assets.compress = false
I was not able to examine the files as the temporary files are cleaned up. I also could not get the debugger in RubyMine to stop at a breakpoint.
Any ideas if this is a bug? Any way to get the temporary files to not get deleted? Any way to make the RubyMine debugger work on the rake task (yes, tried the obvious, using EAP 112-291.
rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted! Unexpected character '' (line: 21454, col: 0, pos:
641761)
Error
at new JS_Parse_Error (/tmp/execjs20111231-15374-1fve7h4.js:497:22)
at js_error (/tmp/execjs20111231-15374-1fve7h4.js:505:15)
at parse_error (/tmp/execjs20111231-15374-1fve7h4.js:596:17)
at Object.next_token [as input] (/tmp/execjs20111231-15374-1fve7h4.js:839:17)
at next (/tmp/execjs20111231-15374-1fve7h4.js:943:37)
at Object.semicolon [as 1] (/tmp/execjs20111231-15374-1fve7h4.js:986:38)
at prog1 (/tmp/execjs20111231-15374-1fve7h4.js:1527:28)
at simple_statement (/tmp/execjs20111231-15374-1fve7h4.js:1123:35)
at /tmp/execjs20111231-15374-1fve7h4.js:1031:35
at /tmp/execjs20111231-15374-1fve7h4.js:1510:32

You will probably find that one of you js files has a syntax error somewhere. This could be a missing semicolon at the end of a block, or some other minor problem. Often browsers will still load the js and it will work, but uglifier cannot compress it with those errors. I would start looking in the localisation files first.
One way to find out which file contains the error is to re precompile locally with a minimal set of files and add things one by one until it breaks. If it is due to a missing semicolon, the breakage will the second-last file you added.

Mine precompiled after I removed a stray "debugger" statement. Woops.

If anyone reading this thread encounters issues with unicode characters or "invalid byte sequence in UTF-8" in your rails app, try putting this in your production.rb file:
# override default uglifier options so we don't mangle unicode
config.assets.js_compressor = Uglifier.new(output: {ascii_only: true})
In my case, the uglifier was converting strings in my javascript like \udbff into UTF-8 characters í¯¿ which ultimately was breaking some unicode regex. (This was happening with turbo-sprockets and codemirror but you might encounter it anytime your javascript relies on ASCII representations of unicode characters.)

The I18N file "jquery-ui-i18n.js" has a bad character before each comment.
Looking at the first two lines with "more" in a shell, shows the wrong character:
<U+FEFF>/* Afrikaans initialisation for the jQuery UI date picker plugin. */
/* Written by Renier Pretorius. */
After having removed this character it works.

Related

Sketchup ruby definitionlist load method error: Invalid component file

When I create a script file and load it from the console with:
load '//192.168.0.0/Mağaza/script.rb'
I get 'Invalid component file' error for:
someModel = Sketchup.active_model.definitions.load '//192.168.0.0/Mağaza/Definitions/model.skp'
But when running the code directly in console, it works.
Any idea why?
DefinitionList.load is a completely different method from Ruby's load.
To load a component from a URL you need to use model.definitions.load_from_url:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/definitionlist#load_from_url
After two days, I figured out that the problem was the encoding of 'ğ' in the folder name (mağaza). I tried ANSI and UTF-8 encoding in my script file but nothing changed. But when print the path name in the console, it turned out that the character was not encoding properly.

rake assets:precompile doesn't work (rails 3.1.1)

I am deploying to heroku yet I saw that the css files aren't being served (they also cannot be found on heroku).
I read that I need to do rake assets:precompile locally at first yet when I do it I get:
C:\project>bundle exec rake assets:precompile --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
undefined: Unexpected token: operator (<)
(in C:/project/app/assets/javascripts/application.js)
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
I have nothing in application.js so I don't understand where the error is..
application.js is
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
Thank you
Update
If removing a .js.erb file I get the following error
C:\project>bundle exec rake assets:precompile RAILS_ENV=production --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
706: unexpected token at 'C:\Users\me\AppData\Local\Temp\execjs20111021-6448-ei2nm3.js(2, 3) Microsoft JScript runtime error: Out of memory
'
(in C:/project/app/assets/javascripts/application.js)
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
Still have problems with erb css and js files not compiling...
This doesn't seem to end..
Thanks
I've been struggling with this trying to deploy to a staging server. The solution that works for me is to make sure you have the following in your config/environments/[your_environment].rb file:
config.assets.compress = false
By default, the compressors aren't available in environment other than production, which is why the precompile was failing.
I have the same issue here! In my case, what causes this issue is that, I add a new JS file to javascript folder, and I got an undefined: Unexpected token: operator (<) error while I tried to run precompile command. So I look into the new js file and found there is a HTML style comment <!-- --> in that file. I remove it and life is good now!
So try to find out is there any HTML style comment <!-- --> in your js file and just remove those comments. This is especially true when some JS code is copied from html file!
I think it is caused by an external javascript file which is not well-code-formatted. e.g.
function say_hi(){
// NOTICE that there's no semi-colon there!
name = "Jim"
alert("hi" + name )
}
when under the precompile, your code would put in 1 line, and since there's no necessary semicolon, your generated js file probably contains errors, like
"undefined: Unexpected token: operator (<)"
or something.
so, my suggestion is:
don't compress the js file if it's not well code-formatted, by setting "config.assets.compress = false" in your config file, following #Mike's answer.
use coffeescript as possible, it will help you generate very well formatted code. ( I am not a coffeescript guru, so please correct me if I am wrong )
I was having the same issue and it turned out to be caused by the inclusion of a embed javascript which had comments in the format: <!-- comment --> I've removed them and it worked like a charm! Hopefully this helps.
one thing I noticed is that it should be:
RAILS_ENV=production bundle exec rake assets:precompile
the definition of the RAILS_ENV needs to go before the bundle command, because it's setting the shell (bash) environment variable for the shell that executes the bundle command.
Your problems seems to be related to this:
https://github.com/bradphelan/jasminerice/issues/21
See also:
http://guides.rubyonrails.org/asset_pipeline.html
Heroku rails 3.1 app - compiling assets locally vs compiling assets during slug compilation
Error compiling Rails 3 CSS asset on Heroku
I've spent the last 1 hour scratching my head after encountering the same bug. The problem is the following line in your application.js:
//= require_tree .
This causes all files in your app/assets/javascripts/ directory to get included and it could be that there is some sort of bug in another file in the directory. I removed that line and got my assets to precompile (I wasn't really using application.js). So, look for a bug in a file being included by application.js
I had a similar problem:
Unexpected token: operator (<<)
This turned out to be a left over file from a merge conflict in Git. The conflict leaves a .orig file that contains "<<<<<<<<<<" wherever Git finds a block of code to be merged.
Because of the asset pipeline directive
//= require_tree .
in application.js, all files in the javascript folder (including .orig files) get precompiled on a push to servers like Heroku. The precompiler finds fault with the "<<<<<".
So my solution was to find all the .orig files and delete them from Git, using the 'git rm filename' method.

Sprockets encoding error on js file: invalid UTF-8 byte sequence

A Sprockets::EncodingError exception is thrown when I include a file with characters that are valid utf-8.
The line in question is:
* Copyright (c) 2010 - 2011 Johan Säll Larsson
If I replace the ä character, the problem goes away, but I don't want to have to remember to edit this vendor file everytime I update it.
How can I fix this?
I found the solution via the comments on this Sprockets issue:
I simply saved the file as utf-8, (TextMate has an option to do this when you chose 'Save As'), and the problem went away.
The commenter #shedd also created a useful rake task to find assets which are not encoded properly.
This is fixed in trunk. All files use utf-8 without BOM.

Ruby on Rails 3.1 assets:precompile and images

I can't get the Rails 3.1 asset pipeline precompilation to work in production mode. It always fails on images referenced within SCSS with an error like:
$ bundle exec rake assets:precompile RAILS_ENV=production
rake aborted!
rails.png isn't precompiled
(in /home/florian/AssetTest/app/assets/stylesheets/application.css.scss)
But when I look in the public/assets directory, the image is there, so it is precompiled:
$ ls public/assets | grep rails
rails-dd352fc2630e5f9aa5685ef1d7fe5997.png
The SCSS file in this case just contains some test code:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
body {
background: #ffffff image-url('rails.png') no-repeat 0 center;
}
If I don't use the image-url helper in SCSS, but just url('/assets/rails.png'), precompilation works fine, and a manifest.yml file is generated in public/assets.
The interesting thing is: if I change the SCSS back to image-url('rails.png') and run another precompilation, it still works (I guess because the image is now already listed in the manifest file).
What am I doing wrong here? I don't really want to disregard the helper methods (as using them is The Way You Should Do It, right?), and I definitely don't want to create the manifest file manually...
I've run into the same problem myself. This is apparently a bug in Rails 3.1.0, and will hopefully be fixed in short order...
In any event, in production.rb, you can try this:
config.assets.compile = true
You likely have it set to false, which it should be. However, having it set to false causes issues when you use asset helpers in SCSS as you're trying to do. Setting that value to true seems to properly allow compilation while using those helpers.
Take a look at this issue on github for some details.

Ruby on Rails upload file problem odd utf8 conversion error

I am trying to upload a file and i am getting the following error:
"\xFF" from ASCII-8BIT to UTF-8
I am pretty much following the rails guides in what they are doing. Here is the code I am using.
file = params[:uploaded_file]
File.open(Rails.root.join('public', 'images', file.original_filename), 'w') do |f|
f.write(file.read)
end
I don't get why it doesn't work. What am I doing wrong?
Update -- Here is the application Trace
app/controllers/shows_controller.rb:16:in `write'
app/controllers/shows_controller.rb:16:in `block in create'
app/controllers/shows_controller.rb:15:in `open'
app/controllers/shows_controller.rb:15:in `create'
I believe this is a change in how rails 3 works with ruby 1.9, since 1.9 supports encodings it will attempt to convert all strings to whatever encoding you have set in your app configuration (application.rb), typically this is 'utf-8'.
To avoid the encoding issue open the file in binary mode, so your mode would be 'wb' for binary writeable:
File.open(Rails.root.join('public', 'images', file.original_filename), 'wb') do |f|
f.write(file.read)
end
I had similar issue with uploading binary files and your solution strangely did not work, but this one had, so here is it for anyone else having the same problem
file.tempfile.binmode
put this line before File.open. I think the reason is that the temporary file is opened in nonbinary mode after upload automatically, and this line switches it to binary, so rails does not try any automatic conversion (which is nonsense in case of binary file).
dst_path = Rails.root.join('public', 'images', file.original_filename)
src_path = params[:uploaded_file].path
IO.copy_stream(src_path, dst_path) # http://ruby-doc.org/core-1.9.2/IO.html#method-c-copy_stream

Resources