-
-
Notifications
You must be signed in to change notification settings - Fork 747
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
Add support for Ruby 3.1 #2635
Add support for Ruby 3.1 #2635
Conversation
In Ruby >= 3.1 it is an error to call `SimpleCov.start` more than once (see simplecov-ruby/simplecov#1003). This commit changes the shared configuration files to call `SimpleCov.configure` instead of `SimpleCov.start`, to work around this issue.
This commit fixes a issue middleman#2614. In middleman <= 4.4.3, if your YAML config file has something that looks like an date (in ISO format), then you get the error YAML Exception ...: Tried to load unspecified class: Date Including a properly formatted date in the frontmatter is a fairly common use-case. This happens Ruby 3.1 upgraded the included YAML library [Psych] to the 4.x branch, which changes `YAML.load` to call `Psych.safe_load` instead of `Psych.unsafe_load`. This means it no longer parses dates unless it's provided with `permitted_classes: [Date, ...]`. The currently released versions of middleman (4.4.3 and 5.0.0-rc1) call `YAML.load`, so the behavour depends on your ruby version. This commit fixes things by using `YAML.safe_load` instead of `YAML.load`, and allowing dates. Note that we have to do some introspection of the arguments to YAML.safe_load to name the keyword argument correctly, because in older versions of Ruby this argument had a different name or wasn't a keyword argument [[1]], and we want to retain support for those old versions. [Psych]: https://github.com/ruby/psych [1]: ruby/psych@682abf2
Thank you @lfdebrux, this is great |
allowed_parameters = ::YAML.method(:safe_load).parameters | ||
if allowed_parameters.include? [:key, :permitted_classes] | ||
::YAML.safe_load(content, permitted_classes: permitted_classes) | ||
elsif allowed_parameters.include? [:key, :whitelist_classes] | ||
::YAML.safe_load(content, whitelist_classes: permitted_classes) | ||
else | ||
::YAML.safe_load(content, permitted_classes) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How many ruby versions would we need to drop support for to replace this with the current version (::YAML.safe_load(content, permitted_classes: permitted_classes)
)?
Is it just ruby 2.5? Or 2.6 too?
Given that 2.6 and 2.5 are end of life I wonder if the middleman maintainers would consider dropping support for them so we could avoid this compatibility workaround?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, we won't be dropping any Ruby support. The vast majority of middleman apps are ancient and I prefer to keep those projects running smoothly over new Ruby 3.0 features which really don't come into play with a static site generator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, the workaround looks fine to me - worth a little bit of complexity if there are still folks on older ruby.
Hey folks, would it be possible to cut a new release of middleman with these changes in? It looks like this is the changeset since 4.4.3 was prepped: a44adc4...main I'm happy to help if you like (but not sure how much I can as a non-maintainer) |
Done. v4.5.0 is released |
Thank you! |
For platforms that use YAML::safe_load we have to say which types are supported otherwise the data is rejected when loaded. In adding ruby 3.1 support (see middleman#2635) we allowed Date and Symbol classes, however in the 5.x branch we support Date, DateTime, Time, Symbol and Regexp by default (see middleman#2528 - specifically 74b01d7). This commit brings the same set of types into the 4.x branch.
For platforms that use YAML::safe_load we have to say which types are supported otherwise the data is rejected when loaded. In adding ruby 3.1 support (see middleman#2635) we allowed Date and Symbol classes, however in the 5.x branch we support Date, DateTime, Time, Symbol and Regexp by default (see middleman#2528 - specifically 74b01d7). This commit brings the same set of types into the 4.x branch.
For platforms that use YAML::safe_load we have to say which types are supported otherwise the data is rejected when loaded. In adding ruby 3.1 support (see #2635) we allowed Date and Symbol classes, however in the 5.x branch we support Date, DateTime, Time, Symbol and Regexp by default (see #2528 - specifically 74b01d7). This commit brings the same set of types into the 4.x branch.
This PR adds Ruby 3.1 to the continuous integration tests, and fixes issue #2614, so that Middleman will work with Ruby 3.1.
Support for older versions of Ruby is retained; a bit of messing around is needed with the arguments to
YAML.safe_load
but otherwise things work fine.Closes #2614.