7

I'm trying to create a gem that wraps d3.js, Source can be found at https://github.com/iblue/d3-rails

So when I include this gem in my Gemfile

gem "d3-rails", :git => "git://github.com/iblue/d3-rails.git"

And when I include the javascript in my application.js:

//=require d3

Then my asset compilation fails and my compiled application.js just contains

throw Error("Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT")

I am using Rails 3.1.3 and ruby-1.9.3-p125. jQuery uses exactly the same gem structure and it works. What am I doing wrong?

EDIT:

I also saw this question: Ruby 1.9 throwing javascript encoding error. This does not apply here, my file is valid UTF-8:

ruby-1.9.3-p125 :001 > d = File.read("./d3.js")
 => [...]
ruby-1.9.3-p125 :002 > d.encoding
 => #<Encoding:UTF-8> 
ruby-1.9.3-p125 :003 > d.valid_encoding?
 => true 

EDIT 2:

I also tried to insert some voodoo in my config/environment.rb. It does not work:

# -*- encoding : utf-8 -*-
# Load the rails application
require File.expand_path('../application', __FILE__)

# --------- VOODOO BEGINS HERE -----------------
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
# --------- END VOODO --------------------------

# Initialize the rails application
Ratecode::Application.initialize!
Community
  • 1
  • 1
iblue
  • 29,609
  • 19
  • 89
  • 128
  • Ever tried to check presence of http://en.wikipedia.org/wiki/Byte_order_mark ? Also try bisection: remove parts of the file to see, whether things would start working. – kirilloid Mar 06 '12 at 14:28
  • Can you also check the encoding of the application.js? The error says the two files are incompatible, you should check not only that the d3 file is UTF-8, but also that your application.js is. – mixonic Mar 06 '12 at 15:01

1 Answers1

9

Is the error from a WEBrick server or something else? Does rake assets:precompile work? (don't forget to clear the assets after)

If the latter fails, double check the value of your $LANG environment variables is UTF-8 (with env). If the rake task works but the app is failing then it could be the server env vars.

Andrew France
  • 4,758
  • 1
  • 25
  • 27
  • 3
    `rake assets:precompile` works flawlessly. The generated application.js contains the d3 library as expected. I removed the generated `public/assets` directory and restarted WEBrick. And now the error is just gone. I don't have any explanation. – iblue Mar 08 '12 at 12:19
  • 4
    I had a similar issue during development. Solved it with two lines: `RAILS_ENV=development rake assets:precompile` and then `RAILS_ENV=development rake assets:clean` – Yuri Oct 19 '12 at 02:04
  • although it sounds completely silly to me, it works iblue and Yuri say. Any idea how compiling the assets and then removing them solve this issue? – DiegoFrings Feb 22 '13 at 12:42
  • 2
    I've had this happen a couple times, and just `RAILS_ENV=development rake assets:clean` and a server restart is enough to fix it. This most recent time, `public/assets` didn't even exist, so I have no idea how that fixed it. – Ian Greenleaf Young May 01 '13 at 22:36
  • not even using the asset full asset pipeline in development but Yuri's suggestion worked for me. Only explanation is that Rails asset pipeline is pretty insane and gets into a bad state from time to time. forcing encoding issues when in bad state? still seems weird – Dan Bradbury Aug 04 '14 at 17:55