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

Frozen Hashes with Psych 3.3.2+/4.0 from YAML #676

Open
broksonic21 opened this issue Aug 31, 2023 · 3 comments
Open

Frozen Hashes with Psych 3.3.2+/4.0 from YAML #676

broksonic21 opened this issue Aug 31, 2023 · 3 comments

Comments

@broksonic21
Copy link

broksonic21 commented Aug 31, 2023

Following up on #648

This change happens whenever psych 3.3.2+ (or 4.0/5.0) is loaded, either as a gem, or as part of default gems for Ruby 3.1/Rails 7.1 (or irb 1.8.0 which brought in a new psych via ruby/irb#703, though they are reverting that part ).

Previous ruby's (3.0/2.7.8/etc) had earlier versions of psych as a Default gem so folks wouldn't have run into this unless they intentionally loaded a new psych version.

The change in psych is:
ruby/psych@cb50aa8

This is because unsafe_load_files now exists so the hash is now frozen by i18n.

if YAML.respond_to?(:unsafe_load_file) # Psych 4.0 way
[YAML.unsafe_load_file(filename, symbolize_names: true, freeze: true), true]
else

There's a few scenarios this can break, but as an example, one backend this breaks is https://github.com/annkissam/i18n-recursive-lookup (which admittedly, is pretty old)

due to https://github.com/annkissam/i18n-recursive-lookup/blob/766750f845a4e0509b62fdc6d1be3137be8d1421/lib/i18n/backend/recursive_lookup.rb#L50

which tries to recursively update the backend.

so 2 questions:

  • Any recommendations on alternative backends for recursive lookups that work with modern i18n?
  • or a hook to skip freezing if needed would be great
@radar
Copy link
Collaborator

radar commented Sep 11, 2023

Related to #658.

@mckaysalisbury
Copy link

We have an updated psych version, so this is affecting us.

@mckaysalisbury
Copy link

mckaysalisbury commented Feb 19, 2025

For what it's worth, this monkey patch appears to get us past the problem for the moment (I haven't been through the whole battery of our tests yet, because I've got other issues I'm working on)

# This is a workaround for https://github.com/ruby-i18n/i18n/issues/676
# I think the only substantive change here is the freeze to false.
# (But I'm also removing support for very old versions of psych, which works for us, but maybe shouldn't be part of the original gem)
# The original gem may want to have that as a parameter or something?
# Or detect whether to freeze or not to do based on which versions of which gems are installed.
module I18n
  module Backend
    module Base
      def load_yml(filename)
        begin
          [YAML.unsafe_load_file(filename, symbolize_names: true, freeze: false), true]
        rescue TypeError, ScriptError, StandardError => e
          raise InvalidLocaleData.new(filename, e.inspect)
        end
      end
    end
  end
end

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

No branches or pull requests

3 participants