Only warn fiddle as optional dependency
[ruby.git] / lib / bundled_gems.rb
blob087f94a9acc69b7f45e110a0118764cf84dd398b
1 # -*- frozen-string-literal: true -*-
3 # :stopdoc:
5 module Gem::BUNDLED_GEMS
6   SINCE = {
7     "rexml" => "3.0.0",
8     "rss" => "3.0.0",
9     "webrick" => "3.0.0",
10     "matrix" => "3.1.0",
11     "net-ftp" => "3.1.0",
12     "net-imap" => "3.1.0",
13     "net-pop" => "3.1.0",
14     "net-smtp" => "3.1.0",
15     "prime" => "3.1.0",
16     "racc" => "3.3.0",
17     "abbrev" => "3.4.0",
18     "base64" => "3.4.0",
19     "bigdecimal" => "3.4.0",
20     "csv" => "3.4.0",
21     "drb" => "3.4.0",
22     "getoptlong" => "3.4.0",
23     "mutex_m" => "3.4.0",
24     "nkf" => "3.4.0",
25     "observer" => "3.4.0",
26     "resolv-replace" => "3.4.0",
27     "rinda" => "3.4.0",
28     "syslog" => "3.4.0",
29     "ostruct" => "3.5.0",
30     "pstore" => "3.5.0",
31     "rdoc" => "3.5.0",
32     "win32ole" => "3.5.0",
33     "fiddle" => "3.5.0",
34     "logger" => "3.5.0",
35     "benchmark" => "3.5.0",
36     "irb" => "3.5.0",
37     "reline" => "3.5.0",
38     # "readline" => "3.5.0", # This is wrapper for reline. We don't warn for this.
39   }.freeze
41   SINCE_FAST_PATH = SINCE.transform_keys { |g| g.sub(/\A.*\-/, "") }.freeze
43   EXACT = {
44     "kconv" => "nkf",
45   }.freeze
47   PREFIXED = {
48     "bigdecimal" => true,
49     "csv" => true,
50     "drb" => true,
51     "rinda" => true,
52     "syslog" => true,
53   }.freeze
55   OPTIONAL = {
56     "fiddle" => true,
57   }.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)
78         begin
79           result = kernel_class.send(:no_warning_require, name)
80         rescue LoadError => e
81           result = e
82         end
84         if (result || !OPTIONAL[name]) && message
85           if ::Gem::BUNDLED_GEMS.uplevel > 0
86             Kernel.warn message, uplevel: ::Gem::BUNDLED_GEMS.uplevel
87           else
88             Kernel.warn message
89           end
90         end
92         if result.is_a?(LoadError)
93           raise result
94         else
95           result
96         end
97       end
99       if kernel_class == ::Kernel
100         kernel_class.send(:private, :require)
101       else
102         kernel_class.send(:public, :require)
103       end
104     end
105   end
107   def self.uplevel
108     frame_count = 0
109     frames_to_skip = 3
110     uplevel = 0
111     require_found = false
112     Thread.each_caller_location do |cl|
113       frame_count += 1
114       if frames_to_skip >= 1
115         frames_to_skip -= 1
116         next
117       end
118       uplevel += 1
119       if require_found
120         if cl.base_label != "require"
121           return uplevel
122         end
123       else
124         if cl.base_label == "require"
125           require_found = true
126         end
127       end
128       # Don't show script name when bundle exec and call ruby script directly.
129       if cl.path.end_with?("bundle")
130         frame_count = 0
131         break
132       end
133     end
134     require_found ? 1 : frame_count - 1
135   end
137   def self.find_gem(path)
138     if !path
139       return
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")
144     else
145       return
146     end
147     (EXACT[n] || !!SINCE[n]) or PREFIXED[n = n[%r[\A[^/]+(?=/)]]] && n
148   end
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
168       # up required.
169       return unless SINCE_FAST_PATH[File.basename(feature, ".*")]
170     else
171       return unless SINCE_FAST_PATH[feature]
172     end
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)
178     name.tr!("/", "-")
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
187       gem = true
188     else
189       return
190     end
192     return if WARNED[name]
193     WARNED[name] = true
194     if gem == true
195       gem = name
196       "#{feature} was loaded from the standard library, but"
197     elsif gem
198       return if WARNED[gem]
199       WARNED[gem] = true
200       "#{feature} is found in #{gem}, which"
201     else
202       return
203     end + build_message(gem)
204   end
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]}."
209     if defined?(Bundler)
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`.
214       #
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.
218       frames_to_skip = 3
219       location = nil
220       require_found = false
221       Thread.each_caller_location do |cl|
222         if frames_to_skip >= 1
223           frames_to_skip -= 1
224           next
225         end
227         if require_found
228           if cl.base_label != "require"
229             location = cl.path
230             break
231           end
232         else
233           if cl.base_label == "require"
234             require_found = true
235           end
236         end
237       end
239       if location && File.file?(location) && !location.start_with?(Gem::BUNDLED_GEMS::LIBDIR)
240         caller_gem = nil
241         Gem.path.each do |path|
242           if location =~ %r{#{path}/gems/([\w\-\.]+)}
243             caller_gem = $1
244             break
245           end
246         end
247         if caller_gem
248           msg += "\nAlso please contact the author of #{caller_gem} to request adding #{gem} into its gemspec."
249         end
250       end
251     else
252       msg += " Install #{gem} from RubyGems."
253     end
255     msg
256   end
258   freeze
261 # for RubyGems without Bundler environment.
262 # If loading library is not part of the default gems and the bundled gems, warn it.
263 class LoadError
264   def message
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
270     end
271     super
272   end
275 # :startdoc: