summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2023-10-12 16:38:41 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2023-10-13 16:22:09 +0900
commitb49346ee7344b6febd69cf2d1dd8161b6a4e774e (patch)
treeeb39f02f923cf0375a2281b8c897e3edbd9f8d00 /lib
parentba4fed47ecdf4bf80cb8be9f1ed5669a6a8a5988 (diff)
Warn only LoadError without Bundler environment
Diffstat (limited to 'lib')
-rw-r--r--lib/bundled_gems.rb30
1 files changed, 17 insertions, 13 deletions
diff --git a/lib/bundled_gems.rb b/lib/bundled_gems.rb
index fa7f77f327..d9e4d7539c 100644
--- a/lib/bundled_gems.rb
+++ b/lib/bundled_gems.rb
@@ -68,6 +68,7 @@ module Gem::BUNDLED_GEMS
EXACT[n] or PREFIXED[n = n[%r[\A[^/]+(?=/)]]] && n
end
+ # for Bundler environment especially Bundler.setup.
def self.warning?(name)
name = name.tr("/", "-")
_t, path = $:.resolve_feature_path(name)
@@ -76,7 +77,7 @@ module Gem::BUNDLED_GEMS
return if find_gem(caller&.absolute_path)
return if WARNED[name]
WARNED[name] = true
- msg = if gem == true
+ if gem == true
gem = name.sub(LIBEXT, "") # assume "foo.rb"/"foo.so" belongs to "foo" gem
elsif gem
return if WARNED[gem]
@@ -84,11 +85,14 @@ module Gem::BUNDLED_GEMS
"#{name} is found in #{gem}"
else
return
- end
- msg += " which #{RUBY_VERSION < SINCE[gem] ? "will be" : "is"} not part of the default gems since Ruby #{SINCE[gem]}."
+ end + build_message(gem)
+ end
+
+ def self.build_message(gem)
+ msg = " which #{RUBY_VERSION < SINCE[gem] ? "will be" : "is"} not part of the default gems since Ruby #{SINCE[gem]}."
if defined?(Bundler)
- msg += " Add #{name} to your Gemfile."
+ msg += " Add #{gem} to your Gemfile."
location = caller_locations(2,2)[0]&.path
if File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
caller_gem = nil
@@ -105,16 +109,16 @@ module Gem::BUNDLED_GEMS
msg
end
- bundled_gems = self
+ freeze
+end
- define_method(:find_unresolved_default_spec) do |name|
- if msg = bundled_gems.warning?(name)
- warn msg, uplevel: 1
+# for RubyGems without Bundler environment.
+# If loading library is not part of the default gems and the bundled gems, warn it.
+class LoadError
+ def message
+ if Gem::BUNDLED_GEMS::SINCE[path]
+ warn path + Gem::BUNDLED_GEMS.build_message(path)
end
- super(name)
+ super
end
-
- freeze
end
-
-Gem.singleton_class.prepend Gem::BUNDLED_GEMS