8000 Improve bundled gem warning messages by deivid-rodriguez · Pull Request #12669 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Improve bundled gem warning messages 8000 #12669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify bundled gems warnings implementation
Most of the stuff is not actually necessary.
  • Loading branch information
deivid-rodriguez committed Feb 4, 2025
commit 451a1a0d3d0f89e4e26c5711859c79fce667a510
52 changes: 12 additions & 40 deletions lib/bundled_gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,61 +99,42 @@ def self.uplevel
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)

# 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)
Expand Down
18 changes: 8 additions & 10 deletions spec/bundled_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ def self.ruby=(ruby)
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
}

Expand Down Expand Up @@ -93,9 +91,9 @@ def script(code, options = {})
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
Expand Down Expand Up @@ -131,7 +129,7 @@ def script(code, options = {})
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

Expand All @@ -147,7 +145,7 @@ def script(code, options = {})
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
Expand All @@ -161,7 +159,7 @@ def script(code, options = {})
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
Expand All @@ -179,7 +177,7 @@ def script(code, options = {})
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
Expand Down Expand Up @@ -211,7 +209,7 @@ def my
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
Expand Down Expand Up @@ -322,7 +320,7 @@ def my
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
Expand Down
0