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

Add support for Ruby 3.1 #2635

Merged
merged 3 commits into from
Apr 12, 2023
Merged

Add support for Ruby 3.1 #2635

merged 3 commits into from
Apr 12, 2023

Conversation

lfdebrux
Copy link
Contributor

@lfdebrux lfdebrux commented Apr 12, 2023

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.

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
@tdreyno
Copy link
Member

tdreyno commented Apr 12, 2023

Thank you @lfdebrux, this is great

Comment on lines +124 to +131
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

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?

Copy link
Member

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.

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.

@markets
Copy link
Member

markets commented Apr 12, 2023

Thanks for that @lfdebrux! Could you please 🙏🏼 link the issue #2614 to automatically close it when this PR is merged? It can be done via the "magic comments" (Closes #2614 or using the Development section in the sidebar).

@lfdebrux
Copy link
Contributor Author

lfdebrux commented Apr 12, 2023

Thanks for that @lfdebrux! Could you please 🙏🏼 link the issue #2614 to automatically close it when this PR is merged? It can be done via the "magic comments" (Closes #2614 or using the Development section in the sidebar).

Done! 👍

@tdreyno tdreyno merged commit df2b799 into middleman:main Apr 12, 2023
5 checks passed
@richardTowers
Copy link

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)

@tdreyno
Copy link
Member

tdreyno commented May 3, 2023

Done. v4.5.0 is released

@richardTowers
Copy link

Thank you!

@lfdebrux lfdebrux deleted the ruby-3.1 branch May 17, 2023 15:40
h-lame added a commit to h-lame/middleman that referenced this pull request Oct 10, 2023
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.
h-lame added a commit to h-lame/middleman that referenced this pull request Nov 18, 2023
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.
tdreyno pushed a commit that referenced this pull request Nov 30, 2023
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.
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.

YAML Exception ...: Tried to load unspecified class: Date
4 participants