rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.14k stars 552 forks source link

incompatible character encodings: ASCII-8BIT and UTF-8 Error #191

Open wbzyl opened 13 years ago

wbzyl commented 13 years ago

Running:

thor test:example Þórr

on README with the content

Ł -- utf-8 encoded L slash
use rake

yields

You supplied the text: Þórr
    gsub  README
    .rvm/gems/ruby-1.9.2-p290/gems/thor-0.14.6/lib/thor/actions/file_manipulation.rb:227:in `gsub!': 
        incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)

Responsible for this is error the file test.thor:

require 'thor'

class Test < Thor
  include Thor::Actions

  desc "example TEXT", "an example task that replaces word 'rake' in README with provided TEXT"
  def example(text)
    puts "You supplied the text: #{text}"
    gsub_file 'README', /rake/ do
      "No more rake. Use #{text}"
    end
  end
end

The above means that I can't gsub a chunk of text with the Polish diacritics in a html.erb file, for example.

znz commented 11 years ago

I met same problem with rails template applications. https://gist.github.com/znz/5117644

aliciacatalina commented 10 years ago

How did you fix it?

znz commented 10 years ago

I used

 content = File.read(path, encoding: 'utf-8')
 content.gsub! /"ほげ"/, '"ふが"'
 File.open(path, 'wb') { |file| file.write(content) }

instead of

 gsub_file "config/locales/hoge.yml", /"ほげ"/, '"ふが"'
bastengao commented 8 years ago

I met same problem using append_to_file, it seems File.binread method return 'ASCII-8BIT' encoding string.

see: https://github.com/erikhuda/thor/blob/master/lib/thor/actions/inject_into_file.rb#L98

Fivell commented 7 years ago

@bastengao , did you fix this somehow ?

jeanfontaine commented 7 years ago

On Version 0.19.4 - November 28, 2016 rvm/gems/ruby-2.2.1/gems/thor-0.19.4/lib/thor/actions 0.19.4/lib/thor/actions/inject_into_file.rb:95 content = File.binread(destination).force_encoding("utf-8") # jjff 2017-05-26 helped for me. It was trial and error, not knowing ruby and gems. Seems to me that as general rule for textfiles, binread should always be avoided as utf-8 is certainly byte-oriented, but definitely not a byte-coding (character-points can span over several bytes)