Fix incorrect bundled gems warning for hyphenated gem names

When requiring a file like "benchmark/ips", the warning system would
incorrectly warn about the "benchmark" gem not being a default gem,
even when the user has "benchmark-ips" (a separate third-party gem)
in their Gemfile.

The fix checks if a hyphenated version of the require path exists in
the bundle specs before issuing a warning. For example, requiring
"benchmark/ips" now checks for both "benchmark" and "benchmark-ips"
in the Gemfile.

[Bug #21828]
This commit is contained in:
Chris Hasiński 2026-01-08 01:13:38 +01:00 committed by Hiroshi SHIBATA
parent 080d66beca
commit 7688628684
Notes: git 2026-01-08 04:15:57 +00:00
2 changed files with 23 additions and 0 deletions

View File

@ -124,6 +124,16 @@ module Gem::BUNDLED_GEMS # :nodoc:
return if specs.include?(name)
# Don't warn if a hyphenated gem provides this feature
# (e.g., benchmark-ips provides benchmark/ips, not the benchmark gem)
if subfeature
feature_parts = feature.split("/")
if feature_parts.size >= 2
hyphenated_gem = "#{feature_parts[0]}-#{feature_parts[1]}"
return if specs.include?(hyphenated_gem)
end
end
return if WARNED[name]
WARNED[name] = true

View File

@ -32,4 +32,17 @@ class TestBundlerGem < Gem::TestCase
assert Gem::BUNDLED_GEMS.warning?(path, specs: {})
assert_nil Gem::BUNDLED_GEMS.warning?(path, specs: {})
end
def test_no_warning_for_hyphenated_gem
# When benchmark-ips gem is in specs, requiring "benchmark/ips" should not warn
# about the benchmark gem (Bug #21828)
assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {"benchmark-ips" => true})
end
def test_warning_without_hyphenated_gem
# When benchmark-ips is NOT in specs, requiring "benchmark/ips" should warn
warning = Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {})
assert warning
assert_match(/benchmark/, warning)
end
end