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",
42 "nkf" => true, "kconv" => "nkf",
44 "resolv-replace" => true,
60 WARNED = {} # unfrozen
62 conf = ::RbConfig::CONFIG
63 LIBDIR = (conf["rubylibdir"] + "/").freeze
64 ARCHDIR = (conf["rubyarchdir"] + "/").freeze
65 dlext = [conf["DLEXT"], "so"].uniq
66 DLEXT = /\.#{Regexp.union(dlext)}\z/
67 LIBEXT = /\.#{Regexp.union("rb", *dlext)}\z/
69 def self.replace_require(specs)
70 return if [::Kernel.singleton_class, ::Kernel].any? {|klass| klass.respond_to?(:no_warning_require) }
72 spec_names = specs.to_a.each_with_object({}) {|spec, h| h[spec.name] = true }
74 [::Kernel.singleton_class, ::Kernel].each do |kernel_class|
75 kernel_class.send(:alias_method, :no_warning_require, :require)
76 kernel_class.send(:define_method, :require) do |name|
77 if message = ::Gem::BUNDLED_GEMS.warning?(name, specs: spec_names) # rubocop:disable Style/HashSyntax
78 warn message, :uplevel => 1
80 kernel_class.send(:no_warning_require, name)
82 if kernel_class == ::Kernel
83 kernel_class.send(:private, :require)
85 kernel_class.send(:public, :require)
90 def self.find_gem(path)
93 elsif path.start_with?(ARCHDIR)
94 n = path.delete_prefix(ARCHDIR).sub(DLEXT, "")
95 elsif path.start_with?(LIBDIR)
96 n = path.delete_prefix(LIBDIR).chomp(".rb")
100 EXACT[n] or PREFIXED[n = n[%r[\A[^/]+(?=/)]]] && n
103 def self.warning?(name, specs: nil)
104 # name can be a feature name or a file path with String or Pathname
105 feature = File.path(name)
106 # bootsnap expands `require "csv"` to `require "#{LIBDIR}/csv.rb"`,
107 # and `require "syslog"` to `require "#{ARCHDIR}/syslog.so"`.
108 name = feature.delete_prefix(ARCHDIR)
109 name.delete_prefix!(LIBDIR)
111 name.sub!(LIBEXT, "")
112 return if specs.include?(name)
113 _t, path = $:.resolve_feature_path(feature)
114 if gem = find_gem(path)
115 return if specs.include?(gem)
116 caller = caller_locations(3, 3)&.find {|c| c&.absolute_path}
117 return if find_gem(caller&.absolute_path)
118 elsif SINCE[name] && !path
124 return if WARNED[name]
128 "#{feature} was loaded from the standard library, but"
130 return if WARNED[gem]
132 "#{feature} is found in #{gem}, which"
135 end + build_message(gem)
138 def self.build_message(gem)
139 msg = " #{RUBY_VERSION < SINCE[gem] ? "will no longer be" : "is not"} part of the default gems since Ruby #{SINCE[gem]}."
142 msg += " Add #{gem} to your Gemfile or gemspec."
144 # We detect the gem name from caller_locations. We need to skip 2 frames like:
145 # lib/ruby/3.3.0+0/bundled_gems.rb:90:in `warning?'",
146 # lib/ruby/3.3.0+0/bundler/rubygems_integration.rb:247:in `block (2 levels) in replace_require'",
148 # Additionally, we need to skip Bootsnap and Zeitwerk if present, these
149 # gems decorate Kernel#require, so they are not really the ones issuing
150 # the require call users should be warned about. Those are upwards.
151 location = Thread.each_caller_location(2) do |cl|
152 break cl.path unless cl.base_label == "require"
155 if location && File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
157 Gem.path.each do |path|
158 if location =~ %r{#{path}/gems/([\w\-\.]+)}
164 msg += " Also contact author of #{caller_gem} to add #{gem} into its gemspec."
168 msg += " Install #{gem} from RubyGems."
177 # for RubyGems without Bundler environment.
178 # If loading library is not part of the default gems and the bundled gems, warn it.
181 return super unless path
183 name = path.tr("/", "-")
184 if !defined?(Bundler) && Gem::BUNDLED_GEMS::SINCE[name] && !Gem::BUNDLED_GEMS::WARNED[name]
185 warn name + Gem::BUNDLED_GEMS.build_message(name)