-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Running fpm
on Ruby 3.1.0 or newer can result in a warning that is caused by Ruby 3.1.0 deprecating a specific way that ERB.new
is invoked by fpm.
Background
For context, ERB is a templating library that comes with Ruby. Fpm uses ERB for templating rpm spec files, package scripts like --after-install
, etc.
At this time, fpm creates a new ERB instance with ERB.new(the_template_code, nil, "-")
where the 2nd and 3rd arguments are describing ERB's "safe_level" and "trim_mode".
It appears as of Ruby 3.1.0, specifying safe_mode
is not a feature anymore and will be removed. Further, specifying trim_mode
now should be done as a keyword argument, such as ERB.new(the_template_code, trim_mode: "-")
Research
When was this introduced?
Here's some testing I did to find out when this warning was introduced:
% for i in 2.6.0 3.0.0 3.1.0; do podman run -it docker.io/ruby:$i ruby -rerb -e 'puts RUBY_VERSION; ERB.new("<%= 123 %>",
nil, "-")'; done
2.6.0
3.0.0
3.1.0
-e:1: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
-e:1: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
Shown above, Ruby 3.1.0 reports these warnings while older versions do not.
Idea: Depending on the version of Ruby being used, invoke ERB.new
in whatever way that works and doesn't print these warnings.
When was the new method syntax allowed?
Testing Ruby versions shown below, we can see that Ruby 2.3.0 and 2.4.0 do not seem to support the keyword argument for trim_mode
and throw an exception when given a keyword argument for trim_mode
% for i in 2.3.0 2.4.0 2.6.0 3.0.0 3.1.0; do podman run -it docker.io/ruby:$i ruby -rerb -e 'puts RUBY_VERSION; erb = ERB.new("<%= 123 %>", trim_mode: "-"); p erb.result(binding)'; done
2.3.0
/usr/local/lib/ruby/2.3.0/erb.rb:860:in `block in result': no implicit conversion of Hash into Integer (TypeError)
from /usr/local/lib/ruby/2.3.0/erb.rb:862:in `result'
from -e:1:in `<main>'
2.4.0
/usr/local/lib/ruby/2.4.0/erb.rb:892:in `block in result': no implicit conversion of Hash into Integer (TypeError)
from /usr/local/lib/ruby/2.4.0/erb.rb:894:in `result'
from -e:1:in `<main>'
2.6.0
"123"
3.0.0
"123"
3.1.0
"123"
Conclusion
- Ruby versions 2.4.0 and older do not support the newer
ERB.new(template, trim_mode: "-")
argument - Ruby versions 3.1.0 and newer print a warning when using the older syntax,
ERB.new(template, safe_level, trim_mode)
In order to support older versions of ruby, I think ERB.new
calls in FPM should be the old syntax for ruby versions 3.0.x and older, and use the new syntax for ruby versions 3.1.x and newer.
The goal is to reduce useless(*) warnings printed to fpm users. By "useless" I mean that there is no obvious action a user can take to resolve these warnings in fpm because the problem is a change in Ruby 3.1.0 and how fpm invokes ERB.
This can be done by checking RUBY_VERSION before calling ERB.new.