Skip to content
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

Merge https://github.com/rubygems/rubygems/pull/7379 #9604

Merged
merged 3 commits into from Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/rubygems.rb
Expand Up @@ -1216,6 +1216,13 @@ def register_default_spec(spec)
##
# Find a Gem::Specification of default gem from +path+

def find_default_spec(path)
@path_to_default_spec_map[path]
end

##
# Find an unresolved Gem::Specification of default gem from +path+

def find_unresolved_default_spec(path)
default_spec = @path_to_default_spec_map[path]
default_spec if default_spec && loaded_specs[default_spec.name] != default_spec
Expand Down
10 changes: 8 additions & 2 deletions lib/rubygems/core_ext/kernel_require.rb
Expand Up @@ -42,7 +42,11 @@ def require(path) # :doc:
# If +path+ belongs to a default gem, we activate it and then go straight
# to normal require

if spec = Gem.find_unresolved_default_spec(path)
if spec = Gem.find_default_spec(path)
name = spec.name

next if Gem.loaded_specs[name]

# Ensure -I beats a default gem
resolved_path = begin
rp = nil
Expand All @@ -60,8 +64,10 @@ def require(path) # :doc:
rp
end

Kernel.send(:gem, spec.name, Gem::Requirement.default_prerelease) unless
Kernel.send(:gem, name, Gem::Requirement.default_prerelease) unless
resolved_path

next
end

# If there are no unresolved deps, then we can use just try
Expand Down
59 changes: 59 additions & 0 deletions test/rubygems/test_require.rb
Expand Up @@ -540,6 +540,65 @@ def test_default_gem_prerelease
assert_equal %w[default-3.0.0.rc2], loaded_spec_names
end

def test_default_gem_with_unresolved_gems_depending_on_it
my_http_old = util_spec "my-http", "0.1.1", nil, "lib/my/http.rb"
install_gem my_http_old

my_http_default = new_default_spec "my-http", "0.3.0", nil, "my/http.rb"
install_default_gems my_http_default

faraday_1 = util_spec "faraday", "1", { "my-http" => ">= 0" }
install_gem faraday_1

faraday_2 = util_spec "faraday", "2", { "my-http" => ">= 0" }
install_gem faraday_2

chef = util_spec "chef", "1", { "faraday" => [">= 1", "< 3"] }, "lib/chef.rb"
install_gem chef

assert_require "chef"
assert_require "my/http"
end

def test_default_gem_required_circulary_with_unresolved_gems_depending_on_it
my_http_old = util_spec "my-http", "0.1.1", nil, "lib/my/http.rb"
install_gem my_http_old

my_http_default = new_default_spec "my-http", "0.3.0", nil, "my/http.rb"
my_http_default_path = File.join(@tempdir, "default_gems", "lib", "my/http.rb")
install_default_gems my_http_default
File.write(my_http_default_path, 'require "my/http"')

faraday_1 = util_spec "faraday", "1", { "my-http" => ">= 0" }
install_gem faraday_1

faraday_2 = util_spec "faraday", "2", { "my-http" => ">= 0" }
install_gem faraday_2

chef = util_spec "chef", "1", { "faraday" => [">= 1", "< 3"] }, "lib/chef.rb"
install_gem chef

assert_require "chef"

out, err = capture_output do
assert_require "my/http"
end

assert_empty out

circular_require_warning = false

err_lines = err.split("\n").reject do |line|
if line.include?("circular require")
circular_require_warning = true
elsif circular_require_warning # ignore backtrace lines for circular require warning
circular_require_warning = line.start_with?(/[\s]/)
end
end

assert_empty err_lines
end

def loaded_spec_names
Gem.loaded_specs.values.map(&:full_name).sort
end
Expand Down