Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18nCache::get_i18n_cache RuntimeError in Rails 3.0.0 #1720

Closed
mtmail opened this issue Dec 27, 2020 · 10 comments
Closed

I18nCache::get_i18n_cache RuntimeError in Rails 3.0.0 #1720

mtmail opened this issue Dec 27, 2020 · 10 comments

Comments

@mtmail
Copy link

mtmail commented Dec 27, 2020

Environment

  • ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
  • Simple Form: master (4f829d6)

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
@kylekeesling
Copy link

@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.

@kylekeesling
Copy link

It also looks as though their Ruby 3.0 testing PR has caught this error: https://travis-ci.org/github/heartcombo/simple_form/jobs/751644453

@kylekeesling
Copy link

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

@pedrofurtado
Copy link
Contributor

pedrofurtado commented Dec 28, 2020

@mtmail Could you rename the issue title to "Compatibility with Ruby 3.0.0"? For more clarity 🤝

@pedrofurtado
Copy link
Contributor

pedrofurtado commented Dec 28, 2020

@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 #1721

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

@carlosantoniodasilva
Copy link
Member

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.

@pedrofurtado
Copy link
Contributor

@carlosantoniodasilva Thanks for feedback! 🤝 Good vacation! 🏖️

After your return (and also the other maintainers), we could help with testing here, in our applications 🏆 🥇

@mtmail mtmail changed the title I18nCache::get_i18n_cache RuntimeError in Rails 3 I18nCache::get_i18n_cache RuntimeError in Rails 3.0.0 Dec 28, 2020
@mcls
Copy link

mcls commented Jan 5, 2021

Hi @carlosantoniodasilva , I created a fix by using a hash inside a singleton instead of the class variables: #1722

Happy to hear your thoughts when you have some time!

@WillRadi
Copy link

@rafaelfranca @tegon @feliperenan @carlosantoniodasilva

Hi! Happy new year to everyone! 🎉

Do you guys have any news about this issue?

@carlosantoniodasilva
Copy link
Member

@WillRadi happy new year! Please see #1722 (comment), thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants