diff --git a/lib/rubygems/commands/uninstall_command.rb b/lib/rubygems/commands/uninstall_command.rb index 283bc96ce3..3d6e41e49e 100644 --- a/lib/rubygems/commands/uninstall_command.rb +++ b/lib/rubygems/commands/uninstall_command.rb @@ -157,9 +157,14 @@ that is a dependency of an existing gem. You can use the gem_specs = Gem::Specification.find_all_by_name(name, original_gem_version[name]) - say("Gem '#{name}' is not installed") if gem_specs.empty? - gem_specs.each do |spec| - deplist.add spec + if gem_specs.empty? + say("Gem '#{name}' is not installed") + else + gem_specs.reject!(&:default_gem?) if gem_specs.size > 1 + + gem_specs.each do |spec| + deplist.add spec + end end end diff --git a/test/rubygems/test_gem_commands_uninstall_command.rb b/test/rubygems/test_gem_commands_uninstall_command.rb index 5bbb5856a6..32553d1730 100644 --- a/test/rubygems/test_gem_commands_uninstall_command.rb +++ b/test/rubygems/test_gem_commands_uninstall_command.rb @@ -111,6 +111,31 @@ class TestGemCommandsUninstallCommand < Gem::InstallerTestCase assert_equal "There was both a regular copy and a default copy of z-1. The regular copy was successfully uninstalled, but the default copy was left around because default gems can't be removed.", output.shift end + def test_execute_does_not_error_on_shadowed_default_gems + z_1_default = new_default_spec "z", "1" + install_default_gems z_1_default + + z_1 = util_spec "z", "1" do |spec| + spec.date = "2024-01-01" + end + install_gem z_1 + + Gem::Specification.reset + + @cmd.options[:args] = %w[z:1] + + use_ui @ui do + @cmd.execute + end + + output = @ui.output.split "\n" + assert_equal "Successfully uninstalled z-1", output.shift + assert_equal "There was both a regular copy and a default copy of z-1. The regular copy was successfully uninstalled, but the default copy was left around because default gems can't be removed.", output.shift + + error = @ui.error.split "\n" + assert_empty error + end + def test_execute_dependency_order initial_install