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

Fix YAML loading on Ruby 3.1 #39

Merged
merged 2 commits into from
Jan 20, 2022
Merged

Fix YAML loading on Ruby 3.1 #39

merged 2 commits into from
Jan 20, 2022

Conversation

scudelletti
Copy link
Contributor

Hello there!

I've noticed that this library is not working on ruby 3.1 and doing some debug I've found the following:
Ruby 3.1 uses psych 4.0.3 by default and this version doesn't support the deprecated way we call YAML.safe_load.

Before:

YAML.safe_load(ERB.new(File.read("config/database.yml")).result, [], [], true)

After:

YAML.safe_load(ERB.new(File.read("config/database.yml")).result, aliases: true)

This new psych version also allows us to use some of the its defaults when calling safe_load.

Method definition: https://github.com/ruby/psych/blob/e8d262fa108a1605443d7426c185452b538775be/lib/psych.rb#L323

Ruby 2.6.0 and up uses psych 3.1.0 which introduces this new method signature.

If we want to support old psych versions we will need create a conditional based on the gem version like it's done in this blog post.

Please let me know if changes are needed.

Thank you for your time and effort maintaining this library! 🙇 💐

@jhollinger
Copy link
Owner

Lost track of this over the holidays. Thanks for the patch, will be reviewing asap!

@jhollinger
Copy link
Owner

Given that the library support back to Ruby 2.1 and AR 4.2, I think supporting both older and newer Psych version would make sense. Would you mind adding that?

@scudelletti
Copy link
Contributor Author

Given that the library support back to Ruby 2.1 and AR 4.2, I think supporting both older and newer Psych version would make sense. Would you mind adding that?

No problem at all. I'll look into it this week.
I'll let you know when I have the change ready for review.

@scudelletti
Copy link
Contributor Author

@jhollinger I've updated the PR with support to old Psych versions so it should be ready for review.

@jhollinger
Copy link
Owner

I think lines 93 and 95 might be switched? I get this in Ruby 3.1:

image

test.rb

require 'yaml'
puts "Psych #{Psych::VERSION}"

erb_result = "foo: bar"
result = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
  YAML.safe_load(erb_result, [], [], true)
else
  YAML.safe_load(erb_result, aliases: true)
end

@scudelletti
Copy link
Contributor Author

I think lines 93 and 95 might be switched?

Good catch! IDK how I missed that! 🤦
It should be fixed now!

@jhollinger jhollinger merged commit 0ad845d into jhollinger:master Jan 20, 2022
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

Successfully merging this pull request may close these issues.

2 participants