Simple_form: I18nCache::get_i18n_cache RuntimeError in Rails 3.0.0

Created on 27 Dec 2020  ยท  10Comments  ยท  Source: heartcombo/simple_form

Environment

  • ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
  • Simple Form: master (https://github.com/heartcombo/simple_form/commit/4f829d6966abc6c0d43c784e052e9e695fd4ccdd)

Current behavior

rvm get master # rvm stable doesn't download .tar.gz files yet
rvm install ruby-3.0
rvm use ruby-3.0
bundle
bin/test
# 794 runs, 1098 assertions, 0 failures, 149 errors, 0 skips
Error:
FormHelperTest#test_fields_for_with_a_hash_like_model_yields_an_instance_of_FormBuilder:
RuntimeError: class variable @@translate_required_html of SimpleForm::Inputs::StringInput is overtaken by SimpleForm::Inputs::Base
    /private/tmp/simple_form/lib/simple_form/i18n_cache.rb:13:in `class_variable_get'
    /private/tmp/simple_form/lib/simple_form/i18n_cache.rb:13:in `get_i18n_cache'
    /private/tmp/simple_form/lib/simple_form/i18n_cache.rb:8:in `i18n_cache'
    /private/tmp/simple_form/lib/simple_form/components/labels.rb:9:in `translate_required_html'
    /private/tmp/simple_form/lib/simple_form/components/labels.rb:71:in `required_label_text'
    /private/tmp/simple_form/lib/simple_form/components/labels.rb:43:in `label_text'
    /private/tmp/simple_form/lib/simple_form/components/labels.rb:35:in `label'

This probably comes down to
https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md
"When a class variable is overtaken by the same definition in an ancestor class/module, a RuntimeError is now raised (previously, it only issued a warning in verbose mode. Additionally, accessing a class variable from the toplevel scope is now a RuntimeError. https://bugs.ruby-lang.org/issues/14541"

where SimpleForm::I18nCache uses

    def get_i18n_cache(key)
      if class_variable_defined?(:"@@#{key}")
        class_variable_get(:"@@#{key}")
      else
        reset_i18n_cache(key)
      end
    end

Most helpful comment

Hi @carlosantoniodasilva , I created a fix by using a hash inside a singleton instead of the class variables: https://github.com/heartcombo/simple_form/pull/1722

Happy to hear your thoughts when you have some time!

All 10 comments

@mtmail Thanks for figuring this out! I've been upgrading my apps and had this error message seemingly randomly pop up during test runs, but couldn't figure out. Glad to know I'm not the only one.

I just realized that that is actually your PR, so I'm not telling you anything you don't already know ๐Ÿ˜†

@mtmail Could you rename the issue title to "Compatibility with Ruby 3.0.0"? For more clarity ๐Ÿค

@rafaelfranca @tegon @feliperenan @carlosantoniodasilva

Hey guys! ๐Ÿ‘‹ Happy new Year for all! ๐Ÿป

After upgrade to Ruby 3.0.0, the simple_form gem not works well anymore. It is related to a breaking change in Ruby 3, properly detailed here (for further reading):

Changes in class variable behavior
https://rubyreferences.github.io/rubychanges/3.0.html#changes-in-class-variable-behavior

When class @@variable is overwritten by the parent of the class, or by the module included, the error is raised. In addition, top-level class variable access also raises an error.

In every form builded with simple_form, and using Ruby 3.0.0, it generates a error with this message structure:

RuntimeError: class variable @@translate_required_html of Name::Of::Some::Class is overtaken by SimpleForm::Inputs::Base

@kylekeesling opened a PR to include ruby 3.0 in Travis CI, and the error (as expected) occured there too: PR https://github.com/heartcombo/simple_form/pull/1721

The output of failed travis ci build is here: https://travis-ci.org/github/heartcombo/simple_form/jobs/751644453#L418

We don't have official support for Ruby 3 yet. I'll look into it here and in the other heartcombo projects once I come back from vacation early next year. Thanks.

@carlosantoniodasilva Thanks for feedback! ๐Ÿค Good vacation! ๐Ÿ–๏ธ

After your return (and also the other maintainers), we could help with testing here, in our applications ๐Ÿ† ๐Ÿฅ‡

Hi @carlosantoniodasilva , I created a fix by using a hash inside a singleton instead of the class variables: https://github.com/heartcombo/simple_form/pull/1722

Happy to hear your thoughts when you have some time!

@rafaelfranca @tegon @feliperenan @carlosantoniodasilva

Hi! Happy new year to everyone! ๐ŸŽ‰

Do you guys have any news about this issue?

@WillRadi happy new year! Please see https://github.com/heartcombo/simple_form/pull/1722#issuecomment-756731186, thanks.

Was this page helpful?
0 / 5 - 0 ratings