1 # -*- frozen-string-literal: true -*-
5 module Gem::BUNDLED_GEMS
12 "net-imap" => "3.1.0",
14 "net-smtp" => "3.1.0",
19 "bigdecimal" => "3.4.0",
22 "getoptlong" => "3.4.0",
25 "observer" => "3.4.0",
26 "resolv-replace" => "3.4.0",
32 "win32ole" => "3.5.0",
35 "benchmark" => "3.5.0",
38 # "readline" => "3.5.0", # This is wrapper for reline. We don't warn for this.
41 SINCE_FAST_PATH = SINCE.transform_keys { |g| g.sub(/\A.*\-/, "") }.freeze
59 WARNED = {} # unfrozen
61 conf = ::RbConfig::CONFIG
62 LIBDIR = (conf["rubylibdir"] + "/").freeze
63 ARCHDIR = (conf["rubyarchdir"] + "/").freeze
64 dlext = [conf["DLEXT"], "so"].uniq
65 DLEXT = /\.#{Regexp.union(dlext)}\z/
66 LIBEXT = /\.#{Regexp.union("rb", *dlext)}\z/
68 def self.replace_require(specs)
69 return if [::Kernel.singleton_class, ::Kernel].any? {|klass| klass.respond_to?(:no_warning_require) }
71 spec_names = specs.to_a.each_with_object({}) {|spec, h| h[spec.name] = true }
73 [::Kernel.singleton_class, ::Kernel].each do |kernel_class|
74 kernel_class.send(:alias_method, :no_warning_require, :require)
75 kernel_class.send(:define_method, :require) do |name|
77 message = ::Gem::BUNDLED_GEMS.warning?(name, specs: spec_names)
79 result = kernel_class.send(:no_warning_require, name)
84 if (result || !OPTIONAL[name]) && message
85 if ::Gem::BUNDLED_GEMS.uplevel > 0
86 Kernel.warn message, uplevel: ::Gem::BUNDLED_GEMS.uplevel
92 if result.is_a?(LoadError)
99 if kernel_class == ::Kernel
100 kernel_class.send(:private, :require)
102 kernel_class.send(:public, :require)
111 require_found = false
112 Thread.each_caller_location do |cl|
114 if frames_to_skip >= 1
120 if cl.base_label != "require"
124 if cl.base_label == "require"
128 # Don't show script name when bundle exec and call ruby script directly.
129 if cl.path.end_with?("bundle")
134 require_found ? 1 : frame_count - 1
137 def self.find_gem(path)
140 elsif path.start_with?(ARCHDIR)
141 n = path.delete_prefix(ARCHDIR).sub(DLEXT, "")
142 elsif path.start_with?(LIBDIR)
143 n = path.delete_prefix(LIBDIR).chomp(".rb")
147 (EXACT[n] || !!SINCE[n]) or PREFIXED[n = n[%r[\A[^/]+(?=/)]]] && n
150 def self.warning?(name, specs: nil)
151 # name can be a feature name or a file path with String or Pathname
152 feature = File.path(name)
154 # irb already has reline as a dependency on gemspec, so we don't want to warn about it.
155 # We should update this with a more general solution when we have another case.
156 # ex: Gem.loaded_specs[called_gem].dependencies.any? {|d| d.name == feature }
157 return false if feature.start_with?("reline") && caller_locations(2, 1)[0].to_s.include?("irb")
159 # The actual checks needed to properly identify the gem being required
160 # are costly (see [Bug #20641]), so we first do a much cheaper check
161 # to exclude the vast majority of candidates.
162 if feature.include?("/")
163 # If requiring $LIBDIR/mutex_m.rb, we check SINCE_FAST_PATH["mutex_m"]
164 # We'll fail to warn requires for files that are not the entry point
165 # of the gem, e.g. require "logger/formatter.rb" won't warn.
166 # But that's acceptable because this warning is best effort,
167 # and in the overwhelming majority of cases logger.rb will end
169 return unless SINCE_FAST_PATH[File.basename(feature, ".*")]
171 return unless SINCE_FAST_PATH[feature]
174 # bootsnap expands `require "csv"` to `require "#{LIBDIR}/csv.rb"`,
175 # and `require "syslog"` to `require "#{ARCHDIR}/syslog.so"`.
176 name = feature.delete_prefix(ARCHDIR)
177 name.delete_prefix!(LIBDIR)
179 name.sub!(LIBEXT, "")
180 return if specs.include?(name)
181 _t, path = $:.resolve_feature_path(feature)
182 if gem = find_gem(path)
183 return if specs.include?(gem)
184 caller = caller_locations(3, 3)&.find {|c| c&.absolute_path}
185 return if find_gem(caller&.absolute_path)
186 elsif SINCE[name] && !path
192 return if WARNED[name]
196 "#{feature} was loaded from the standard library, but"
198 return if WARNED[gem]
200 "#{feature} is found in #{gem}, which"
203 end + build_message(gem)
206 def self.build_message(gem)
207 msg = " #{RUBY_VERSION < SINCE[gem] ? "will no longer be" : "is not"} part of the default gems starting from Ruby #{SINCE[gem]}."
210 msg += "\nYou can add #{gem} to your Gemfile or gemspec to silence this warning."
212 # We detect the gem name from caller_locations. First we walk until we find `require`
213 # then take the first frame that's not from `require`.
215 # Additionally, we need to skip Bootsnap and Zeitwerk if present, these
216 # gems decorate Kernel#require, so they are not really the ones issuing
217 # the require call users should be warned about. Those are upwards.
220 require_found = false
221 Thread.each_caller_location do |cl|
222 if frames_to_skip >= 1
228 if cl.base_label != "require"
233 if cl.base_label == "require"
239 if location && File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
241 Gem.path.each do |path|
242 if location =~ %r{#{path}/gems/([\w\-\.]+)}
248 msg += "\nAlso please contact the author of #{caller_gem} to request adding #{gem} into its gemspec."
252 msg += " Install #{gem} from RubyGems."
261 # for RubyGems without Bundler environment.
262 # If loading library is not part of the default gems and the bundled gems, warn it.
265 return super unless path
267 name = path.tr("/", "-")
268 if !defined?(Bundler) && Gem::BUNDLED_GEMS::SINCE[name] && !Gem::BUNDLED_GEMS::WARNED[name]
269 warn name + Gem::BUNDLED_GEMS.build_message(name), uplevel: Gem::BUNDLED_GEMS.uplevel