Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot set encoding on non-encoding capable object

migrated from cloud 9 to AWS cloud 9. Now I'm receiving an error on GET...

searched the web got some information on postgresql changing template1 to template0 etc. no success.

Started GET "/" for 167.102.190.106 at 2019-06-13 19:52:07 +0000

ArgumentError (cannot set encoding on non-encoding capable object)

Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-4.2.11.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
  Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-4.2.11.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.7ms)
  Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-4.2.11.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
  Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-4.2.11.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (12.0ms)


Process exited with code: 0
[2019-06-13 19:52:44] INFO  going to shutdown ...

Pane is dead

like image 292
M Bakhtiari Avatar asked Oct 17 '25 16:10

M Bakhtiari


1 Answers

The short answer:

Change to a different Ruby version and reinstall your gems for that version.

Ruby 2.4.7 with Rails 4.2.11.1 worked for me, and the only other gem version I needed to change was bundler (from 2.0.2 to 1.17.3)

The long answer:

I found the same error after making a series of version upgrades to Ubuntu, Ruby, Rails, and my project gemset. Steps that worked (note: your exact versions may vary, based on your project gems. Go to https://rubygems.org/ and find the latest stable versions/subversions that match your needs, as outlined by any warnings when you run bundle install)

switch to Ruby 2.4.7

rvm install 2.4.7
rvm --default use 2.4.7 #use Ruby 2.4.7 and set it as the default

uninstall bundler (run bundle install after changing Ruby versions and it will show that bundler < 2.0 is required)

gem uninstall bundler 

you may also need to uninstall a global installation of bundler if prompted, e.g.

gem uninstall -i /usr/local/rvm/gems/ruby-2.4.7@global bundler

install a compatible bundler version and run it. If you have any remaining conflicts, tweak the versions in your Gemfile using https://rubygems.org/ as a guide.

gem install bundler -v 1.17.3
bundle install

I had the same Ruby/Rails pairing as you - 2.6.3/4.2.11.1, and got the same error message. No trace is generated so it's very hard to debug with the stock compiled Ruby libraries.

In searching for help, I came across a bug report for a different gem, iconv, (that I don't have in my project) here: https://github.com/ruby/iconv/issues/15 where the developer notes that their gem started throwing that error when used under specific Ruby versions:

Works in ruby 2.6.0 preview1 and 2, does not in preview3 and greater, so the commit is between preview2 and preview3

Using this as a guide, I went back to the last stable Ruby minor version, 2.4.7

Before the version changes, I also found that the location set in my system was null, so I set LANG=en_CA.utf8, but this had no effect.

Other attempts:

I searched within my project files and didn't find the string "cannot set encoding on non-encoding capable object", meaning the error is being thrown from the Ruby core files or from a globally installed gem. So I searched:

grep -iRl "cannot set encoding on non-encoding capable object" /usr

and got back a set of Ruby core files, as expected. They're compiled though, so I downloaded the Ruby source files from https://github.com/ruby/ruby and searched the source code.

The error is thrown in a function in encoding.c:

enc_set_index(VALUE obj, int idx)
{
    if (!enc_capable(obj)) {
        rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object");
    }

    if (idx < ENCODING_INLINE_MAX) {
    ENCODING_SET_INLINED(obj, idx);
    return;
    }
    ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
    rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
}

based on an object type test elsewhere in encoding.c

static inline int
enc_capable(VALUE obj)
{
    if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
    switch (BUILTIN_TYPE(obj)) {
      case T_STRING:
      case T_REGEXP:
      case T_FILE:
      case T_SYMBOL:
    return TRUE;
      case T_DATA:
    if (is_data_encoding(obj)) return TRUE;
      default:
    return FALSE;
    }
}

If you wanted to completely diagnose the issue, you could follow the trail of calls and definitions in the Ruby source code to see what's happening, and/or add a custom backtrace logging call within the enc_set_index function, compile this custom version of Ruby, and install it for your project to give a more fleshed out error message. But... y tho?

Conclusion:

There's some incompatible versions of Ruby, Rails, and some gem causing the error. It wasn't worth hunting down the exact gem – much easier to change the Ruby version itself. The aforementioned iconv gem fixed the error with an updated version (iconv-1.0.6), and presumably as I continue gem and system version updates I'll find even higher-version combinations that don't throw the error.

like image 170
Rick Gladwin Avatar answered Oct 20 '25 04:10

Rick Gladwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!