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

Deprecate safe_level of ERB.new in Ruby 2.6 #594

Merged

Conversation

koic
Copy link
Contributor

@koic koic commented Mar 18, 2018

The interface of ERB.new will change from Ruby 2.6.

Add :trim_mode and :eoutvar keyword arguments to ERB.new.
Now non-keyword arguments other than first one are softly deprecated
and will be removed when Ruby 2.5 becomes EOL. [Feature #14256]

https://github.com/ruby/ruby/blob/2311087b685e8dc0f21f4a89875f25c22f5c39a9/NEWS#stdlib-updates-outstanding-ones-only

The following address is related Ruby's commit.
ruby/ruby@cc777d0

This PR uses ERB.version to switch ERB.new interface. Because Thor supports multiple Ruby versions, it need to use the appropriate interface.

Using ERB.version instead of RUBY_VERSON is based on the following patch.
ruby/ruby#1826

This patch is built into Ruby.
ruby/ruby@40db89c

@koic koic force-pushed the deprecate_safe_level_of_erb_new_in_ruby_2_6 branch 3 times, most recently from ca4881b to 9be0a15 Compare March 23, 2018 11:08
@yahonda
Copy link
Member

yahonda commented Mar 24, 2018

I would like this pull request merged since there are a bunch of warnings at Rails CI with Ruby 2.6. https://travis-ci.org/rails/rails/jobs/357183623

There are two failures with Ruby 2.6, then I have opened #595, For 1.8.7 failures at https://travis-ci.org/erikhuda/thor/jobs/357338902 I simply wanted to drop Ruby 1.8.7 support.

@yahonda
Copy link
Member

yahonda commented Apr 11, 2018

It looks like this pull request failure with Ruby 1.8.7 is due to "newer" regular expression syntax
introduced in Ruby 1.9.

   Failure/Error: require "thor/actions/file_manipulation"
    SyntaxError:
      /home/travis/build/yahonda/thor/spec/../lib/thor/actions/file_manipulation.rb:120: undefined (?...) sequence: /\Aerb\.rb \[(?<version>[^ ]+) /

I have made a quick and dirty workaround to support Ruby 1.8.7. yahonda@8157118

Now CI is green for all supported versions of Ruby https://travis-ci.org/yahonda/thor/builds/36489840

@koic
I am not good at regular expressions then would you review it and consider to change your pull request.

Thank you.

@koic koic force-pushed the deprecate_safe_level_of_erb_new_in_ruby_2_6 branch from 9be0a15 to 708365d Compare April 11, 2018 06:19
@koic
Copy link
Contributor Author

koic commented Apr 11, 2018

@yahonda Thanks for your investigation. I replaced it with regular expression that you showed 🙏

@@ -117,7 +117,13 @@ def template(source, *args, &block)
context = config.delete(:context) || instance_eval("binding")

create_file destination, nil, config do
content = CapturableERB.new(::File.binread(source), nil, "-", "@output_buffer").tap do |erb|
match = ERB.version.match(/(\d\.\d.\d)/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My previous commit did not have a necessary backslash for the second "."

match = ERB.version.match(/(\d\.\d\.\d)/)

Here the dot should be actual dot character not any one character.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't write a more correctly regular expression because of lack of self review 💦 I fixed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the update. I believe we did our best.

The interface of `ERB.new` will change from Ruby 2.6.

> Add :trim_mode and :eoutvar keyword arguments to ERB.new.
> Now non-keyword arguments other than first one are softly deprecated
> and will be removed when Ruby 2.5 becomes EOL. [Feature #14256]

https://github.com/ruby/ruby/blob/2311087b685e8dc0f21f4a89875f25c22f5c39a9/NEWS#stdlib-updates-outstanding-ones-only

The following address is related Ruby's commit.
ruby/ruby@cc777d0

This PR uses `ERB.version` to switch `ERB.new` interface. Because
Thor supports multiple Ruby versions, it need to use the appropriate interface.

Using `ERB.version` instead of `RUBY_VERSON` is based on the following patch.
ruby/ruby#1826

This patch is built into Ruby.
ruby/ruby@40db89c
@koic koic force-pushed the deprecate_safe_level_of_erb_new_in_ruby_2_6 branch from 708365d to 70021d0 Compare April 12, 2018 12:29
@yahonda
Copy link
Member

yahonda commented Apr 16, 2018

Rails railties testing against ruby-head (aka Ruby 2.6.0 dev) has been failing due to

The log length has exceeded the limit of 4 MB (this usually means that the test suite is raising the same exception over and over).

And the biggest number of warnings will be fixed by this pull request. Here are number of warnings.

$ grep 'warning:' log.txt | sort | uniq -c | sort -rn | head -n 5
6092 /home/travis/.rvm/gems/ruby-head/gems/thor-0.20.0/lib/thor/actions/file_manipulation.rb:120: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
6092 /home/travis/.rvm/gems/ruby-head/gems/thor-0.20.0/lib/thor/actions/file_manipulation.rb:120: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.
5949 /home/travis/.rvm/gems/ruby-head/gems/thor-0.20.0/lib/thor/actions/file_manipulation.rb:120: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
 183 /home/travis/.rvm/gems/ruby-head/gems/sqlite3-1.3.13/lib/sqlite3/pragmas.rb:34: warning: mismatched indentations at 'else' with 'case' at 21
 116 /home/travis/.rvm/gems/ruby-head/gems/sqlite3-1.3.13/lib/sqlite3/pragmas.rb:26: warning: mismatched indentations at 'else' with 'case' at 23
$

Thank you.

@rafaelfranca rafaelfranca merged commit 006832e into rails:master Apr 18, 2018
@koic koic deleted the deprecate_safe_level_of_erb_new_in_ruby_2_6 branch April 18, 2018 18:22
@@ -117,7 +117,13 @@ def template(source, *args, &block)
context = config.delete(:context) || instance_eval("binding")

create_file destination, nil, config do
content = CapturableERB.new(::File.binread(source), nil, "-", "@output_buffer").tap do |erb|
match = ERB.version.match(/(\d\.\d\.\d)/)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work on versions with more numerics. While it's not a problem now, it could cause wierd errors in the future.

example

'2.11.0'.match(/(\d\.\d\.\d)/) -> nil

an old behaviour is used even if '2.11.0' is newer than '2.2.0'

'2.11.0' >= '2.2.0' -> false

utilum added a commit to utilum/thor that referenced this pull request Apr 30, 2018
Fix introduced in rails#594 misidentifies versions with more numerics.
Version "2.11.0", for example, would be called using the deprecated API.

Long live ERB.
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.

None yet

4 participants