Bug #20714
open
- Description updated (diff)
One potential problem with setting $VERBOSE
to nil is that's not thread-safe, and so might hide valid warnings from other threads requiring around the same time.
Maybe it should be some explicit method in Gem
or so to suppress that warning, or a keyword argument like require "foo", optional: true
.
The problem is any new API would be rather cumbersome to use (in a way that still works on older Rubies), so probably this fix is good as a quick measure.
I think it would be good to think about a longer-term solution too though.
Thanks for you reply, I was about to write something similar about the older rubies.
At first I was writing about how I don't think a new api is really necessary since the compatibility code can simply be dropped once required_ruby_version
is at or above the ruby version that would error but I don't think that is entirely true. For example, on ruby 3.4 the following script produces this output:
begin
require "webrick"
rescue LoadError
end
if defined?(Webrick)
puts "Do some things"
end
# test.rb:2: warning: webrick was loaded from the standard library, but is not part of the default gems starting from Ruby 3.0.0.
# You can add webrick to your Gemfile or gemspec to silence this warning.
So even though webrick is gone since Ruby 3.0, I'd still have to do some trickery to optionally enhance it without triggering the warning.
I see two solutions:
-
require "foo", optional: true
like you suggested
- Drop warnings when ruby would throw an error here. When the require will raise, it doesn't need to warn. This is how I already imagined it works. I guess it doesn't for visibility?
Maybe a good solution is to make $VERBOSE
thread-local or fiber-local at some point, then at least this concern and related ones would be solved once and for all.
Drop warnings when ruby would throw an error here. When the require will raise, it doesn't need to warn. This is how I already imagined it works. I guess it doesn't for visibility?
I think this is the best solution. The warning is also misleading if the require fails because it suggests that the require succeeded ("webrick was loaded from...").
Is it as simple as https://github.com/ruby/ruby/pull/11550?
- Status changed from Open to Assigned
- Assignee set to hsbt (Hiroshi SHIBATA)
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0