summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2024-03-25 15:04:26 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2024-03-25 15:04:56 +0900
commit675dcf9153fdf9041c1220d827fe6e6ebbf14cc8 (patch)
tree426bdb2acac5328e849eab9a15f1ed71f6b32f49
parente720a6b48582900efd1bd326210644b2fc0f2c5d (diff)
Partly Revert "Show the chilled status of a String [ci skip]"
This reverts commit e720a6b48582900efd1bd326210644b2fc0f2c5d.
-rw-r--r--lib/bundled_gems.rb35
-rwxr-xr-xtool/test_for_warn_bundled_gems/test.sh20
-rw-r--r--tool/test_for_warn_bundled_gems/test_warn_bootsnap.rb (renamed from tool/test_for_warn_bundled_gems/test_no_warn_bootsnap.rb)0
-rw-r--r--tool/test_for_warn_bundled_gems/test_warn_zeitwerk.rb12
4 files changed, 54 insertions, 13 deletions
diff --git a/lib/bundled_gems.rb b/lib/bundled_gems.rb
index 55286725c0..e756af61ea 100644
--- a/lib/bundled_gems.rb
+++ b/lib/bundled_gems.rb
@@ -95,8 +95,10 @@ module Gem::BUNDLED_GEMS
end
def self.warning?(name, specs: nil)
- feature = File.path(name) # name can be a feature name or a file path with String or Pathname
- name = feature.tr("/", "-")
+ # name can be a feature name or a file path with String or Pathname
+ feature = File.path(name)
+ # bootsnap expand `require "csv"` to `require "#{LIBDIR}/csv.rb"`
+ name = feature.delete_prefix(LIBDIR).chomp(".rb").tr("/", "-")
name.sub!(LIBEXT, "")
return if specs.include?(name)
_t, path = $:.resolve_feature_path(feature)
@@ -109,12 +111,7 @@ module Gem::BUNDLED_GEMS
else
return
end
- # Warning feature is not working correctly with Bootsnap.
- # caller_locations returns:
- # lib/ruby/3.3.0+0/bundled_gems.rb:65:in `block (2 levels) in replace_require'
- # $GEM_HOME/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'"
- # ...
- return if caller_locations(2).find {|c| c&.path.match?(/bootsnap/) }
+
return if WARNED[name]
WARNED[name] = true
if gem == true
@@ -134,11 +131,29 @@ module Gem::BUNDLED_GEMS
if defined?(Bundler)
msg += " Add #{gem} to your Gemfile or gemspec."
+
# We detect the gem name from caller_locations. We need to skip 2 frames like:
# lib/ruby/3.3.0+0/bundled_gems.rb:90:in `warning?'",
# lib/ruby/3.3.0+0/bundler/rubygems_integration.rb:247:in `block (2 levels) in replace_require'",
- location = caller_locations(3,1)[0]&.path
- if File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
+ #
+ # Additionally, we need to skip Bootsnap and Zeitwerk if present, these
+ # gems decorate Kernel#require, so they are not really the ones issuing
+ # the require call users should be warned about. Those are upwards.
+ frames_to_skip = 2
+ location = nil
+ Thread.each_caller_location do |cl|
+ if frames_to_skip >= 1
+ frames_to_skip -= 1
+ next
+ end
+
+ unless cl.path.match?(/bootsnap|zeitwerk/)
+ location = cl.path
+ break
+ end
+ end
+
+ if location && File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
caller_gem = nil
Gem.path.each do |path|
if location =~ %r{#{path}/gems/([\w\-\.]+)}
diff --git a/tool/test_for_warn_bundled_gems/test.sh b/tool/test_for_warn_bundled_gems/test.sh
index ce714c7e13..ef5007f320 100755
--- a/tool/test_for_warn_bundled_gems/test.sh
+++ b/tool/test_for_warn_bundled_gems/test.sh
@@ -2,30 +2,44 @@
echo "* Show warning require and LoadError"
ruby test_warn_bundled_gems.rb
+echo
echo "* Show warning when bundled gems called as dependency"
ruby test_warn_dependency.rb
+echo
echo "* Show warning sub-feature like bigdecimal/util"
ruby test_warn_sub_feature.rb
+echo
echo "* Show warning dash gem like net/smtp"
ruby test_warn_dash_gem.rb
+echo
echo "* Show warning when bundle exec with ruby and script"
bundle exec ruby test_warn_bundle_exec.rb
+echo
echo "* Show warning when bundle exec with shebang's script"
bundle exec ./test_warn_bundle_exec_shebang.rb
+echo
+
+echo "* Show warning with bootsnap"
+ruby test_warn_bootsnap.rb
+echo
+
+echo "* Show warning with zeitwerk"
+ruby test_warn_zeitwerk.rb
+echo
echo "* Don't show warning bundled gems on Gemfile"
ruby test_no_warn_dependency.rb
-
-echo "* Don't show warning with bootsnap"
-ruby test_no_warn_bootsnap.rb
+echo
echo "* Don't show warning with net/smtp when net-smtp on Gemfile"
ruby test_no_warn_dash_gem.rb
+echo
echo "* Don't show warning bigdecimal/util when bigdecimal on Gemfile"
ruby test_no_warn_sub_feature.rb
+echo
diff --git a/tool/test_for_warn_bundled_gems/test_no_warn_bootsnap.rb b/tool/test_for_warn_bundled_gems/test_warn_bootsnap.rb
index eac58de974..eac58de974 100644
--- a/tool/test_for_warn_bundled_gems/test_no_warn_bootsnap.rb
+++ b/tool/test_for_warn_bundled_gems/test_warn_bootsnap.rb
diff --git a/tool/test_for_warn_bundled_gems/test_warn_zeitwerk.rb b/tool/test_for_warn_bundled_gems/test_warn_zeitwerk.rb
new file mode 100644
index 0000000000..d554a0e675
--- /dev/null
+++ b/tool/test_for_warn_bundled_gems/test_warn_zeitwerk.rb
@@ -0,0 +1,12 @@
+require "bundler/inline"
+
+gemfile do
+ source "https://rubygems.org"
+ gem "zeitwerk", require: false
+end
+
+require "zeitwerk"
+loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
+loader.setup
+
+require 'csv'