Simplify bundled gems warnings implementation

Most of the stuff is not actually necessary.
This commit is contained in:
David Rodríguez 2025-01-29 19:53:54 +01:00 committed by Hiroshi SHIBATA
parent 68bb6ceeaf
commit 433f4e30b3
2 changed files with 20 additions and 50 deletions

View File

@ -37,15 +37,6 @@ module Gem::BUNDLED_GEMS # :nodoc:
"kconv" => "nkf",
}.freeze
PREFIXED = {
"bigdecimal" => true,
"csv" => true,
"drb" => true,
"rinda" => true,
"syslog" => true,
"fiddle" => true,
}.freeze
WARNED = {} # unfrozen
conf = ::RbConfig::CONFIG
@ -108,19 +99,6 @@ module Gem::BUNDLED_GEMS # :nodoc:
require_found ? 1 : frame_count - 1
end
def self.find_gem(path)
if !path
return
elsif path.start_with?(ARCHDIR)
n = path.delete_prefix(ARCHDIR).sub(DLEXT, "").chomp(".rb")
elsif path.start_with?(LIBDIR)
n = path.delete_prefix(LIBDIR).chomp(".rb")
else
return
end
(EXACT[n] || !!SINCE[n]) or PREFIXED[n = n[%r[\A[^/]+(?=/)]]] && n
end
def self.warning?(name, specs: nil)
# name can be a feature name or a file path with String or Pathname
feature = File.path(name)
@ -128,41 +106,35 @@ module Gem::BUNDLED_GEMS # :nodoc:
# The actual checks needed to properly identify the gem being required
# are costly (see [Bug #20641]), so we first do a much cheaper check
# to exclude the vast majority of candidates.
if feature.include?("/")
subfeature = if feature.include?("/")
# bootsnap expands `require "csv"` to `require "#{LIBDIR}/csv.rb"`,
# and `require "syslog"` to `require "#{ARCHDIR}/syslog.so"`.
name = feature.delete_prefix(ARCHDIR).delete_prefix(LIBDIR).sub(LIBEXT, "")
segments = name.split("/")
name = segments.first
name = segments.shift
name = EXACT[name] || name
if !SINCE[name]
name = segments[0..1].join("-")
name = [name, segments.shift].join("-")
return unless SINCE[name]
end
segments.any?
else
name = feature.sub(LIBEXT, "")
name = EXACT[name] || name
return unless SINCE[name]
false
end
return if specs.include?(name)
_t, path = $:.resolve_feature_path(feature)
if gem = find_gem(path)
return if specs.include?(gem)
elsif SINCE[name] && !path
gem = true
else
return
end
return if WARNED[name]
WARNED[name] = true
if gem == true
gem = name
"#{feature} was loaded from the standard library, but"
elsif gem
"#{feature} is found in #{gem}, which"
if subfeature
"#{feature} is found in #{name}, which"
else
return
end + build_message(gem)
"#{feature} was loaded from the standard library, but"
end + build_message(name)
end
def self.build_message(gem)

View File

@ -60,11 +60,9 @@ RSpec.describe "bundled_gems.rb" do
Gem::BUNDLED_GEMS.send(:remove_const, :LIBDIR)
Gem::BUNDLED_GEMS.send(:remove_const, :ARCHDIR)
Gem::BUNDLED_GEMS.send(:remove_const, :SINCE)
Gem::BUNDLED_GEMS.send(:remove_const, :PREFIXED)
Gem::BUNDLED_GEMS.const_set(:LIBDIR, File.expand_path(File.join(__dir__, "../../..", "lib")) + "/")
Gem::BUNDLED_GEMS.const_set(:ARCHDIR, File.expand_path($LOAD_PATH.find{|path| path.include?(".ext/common") }) + "/")
Gem::BUNDLED_GEMS.const_set(:SINCE, { "openssl" => RUBY_VERSION, "fileutils" => RUBY_VERSION, "csv" => "3.4.0", "net-smtp" => "3.1.0" })
Gem::BUNDLED_GEMS.const_set(:PREFIXED, { "openssl" => true })
STUB
}
@ -93,9 +91,9 @@ RSpec.describe "bundled_gems.rb" do
RUBY
expect(err).to include(/csv was loaded from (.*) from Ruby 3.4.0/)
expect(err).to include(/-e:17/)
expect(err).to include(/-e:15/)
expect(err).to include(/openssl was loaded from (.*) from Ruby #{RUBY_VERSION}/)
expect(err).to include(/-e:20/)
expect(err).to include(/-e:18/)
end
it "Show warning when bundled gems called as dependency" do
@ -131,7 +129,7 @@ RSpec.describe "bundled_gems.rb" do
RUBY
expect(err).to include(/net\/smtp was loaded from (.*) from Ruby 3.1.0/)
expect(err).to include(/-e:17/)
expect(err).to include(/-e:15/)
expect(err).to include("You can add net-smtp")
end
@ -147,7 +145,7 @@ RSpec.describe "bundled_gems.rb" do
RUBY
expect(err).to include(/openssl\/bn is found in openssl, (.*) part of the default gems starting from Ruby #{RUBY_VERSION}/)
expect(err).to include(/-e:16/)
expect(err).to include(/-e:14/)
end
it "Show warning when bundle exec with ruby and script" do
@ -161,7 +159,7 @@ RSpec.describe "bundled_gems.rb" do
bundle "exec ruby script.rb"
expect(err).to include(/openssl was loaded from (.*) from Ruby 3.5.0/)
expect(err).to include(/script\.rb:10/)
expect(err).to include(/script\.rb:8/)
end
it "Show warning when bundle exec with shebang's script" do
@ -179,7 +177,7 @@ RSpec.describe "bundled_gems.rb" do
bundle "exec ./script.rb"
expect(err).to include(/openssl was loaded from (.*) from Ruby 3.5.0/)
expect(err).to include(/script\.rb:11/)
expect(err).to include(/script\.rb:9/)
end
it "Show warning when bundle exec with -r option" do
@ -211,7 +209,7 @@ RSpec.describe "bundled_gems.rb" do
RUBY
expect(err).to include(/openssl was loaded from (.*) from Ruby 3.5.0/)
expect(err).to include(/-e:21/)
expect(err).to include(/-e:19/)
end
it "Don't show warning when bundled gems called as dependency" do
@ -322,7 +320,7 @@ RSpec.describe "bundled_gems.rb" do
bundle "exec ruby script.rb"
expect(err).to include(/openssl was loaded from (.*) from Ruby 3.5.0/)
expect(err).to include(/script\.rb:15/)
expect(err).to include(/script\.rb:13/)
end
it "Don't show warning openssl/bn when openssl on Gemfile" do