Encoding::CompatibilityError with Ruby 1.9 + ERB when multi-byte string is used in both template and variable #48

1 commit merged into from Dec 6, 2010

5 participants


Here's script to reproduce the issue:

Here's output of the script with tilt 1.1:

/tmp/template20101205-22819-1ljrpwf:2:in `concat': incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
    from /tmp/template20101205-22819-1ljrpwf:2:in `evaluate_source'
    from /usr/local/lib/ruby/gems/1.9.1/gems/tilt-1.1/lib/tilt.rb:254:in `instance_eval'
    from /usr/local/lib/ruby/gems/1.9.1/gems/tilt-1.1/lib/tilt.rb:254:in `evaluate_source'
    from /usr/local/lib/ruby/gems/1.9.1/gems/tilt-1.1/lib/tilt.rb:195:in `evaluate'
    from /usr/local/lib/ruby/gems/1.9.1/gems/tilt-1.1/lib/tilt.rb:128:in `render'
    from test.rb:15:in `<main>'

Issue here is that:

  • ERB#src correctly outputs magic comment e.g. "# coding: utf-8" based on either magic comment in ERB template or encoding of the template string
  • eval() correctly handles magic comment, but it must be in the first line
  • But Tilt add some script (preamble) before the output of ERB#src, so the magic comment no longer works

Solution in this change is to copy the magic comment to the first line.
It looks like #45 solves the same issue, but this patch might be slightly simpler and more general (because it should also work for other template engines which output magic comment if any).

@gimite gimite Fixing Encoding::CompatibilityError with Ruby 1.9 + ERB, by copying m…
…agic comment to the first line so that it works.
rkh commented Dec 5, 2010

Hiroshi: Does this solve the Sinatra issue on Ubuntu?
Ryan: If so, +1 an can we have a new Tilt release some time soon?


Yes, I verified it solves the issue of Encoding::CompatibilityError when multi-byte character is used in both controller and view template (if magic comment is correctly written in both file). I haven't checked original issue reported by Quintus (where one template renders another template), but I guess it's fixed too.


When will this fix be released in a gem?


Marvelous! Gem soon, please!


Was just playing with this patch and Sinatra, and it only seems to work for the first render of the ERB. The second time render() is called, the template seems to have reverted to ASCII-8BIT.

I created a patched script to reproduce the issue:


Fixed in PR #50.

@alexch alexch pushed a commit to alexch/tilt that referenced this pull request Sep 28, 2011
@rkh rkh Add magic comments for encoding to method compilation, too.
Also, add test for encoding issues with erb.

Fixes #48.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment