Skip to content

Commit

Permalink
[rubygems/rubygems] Use spec.base_dir to remove plugins
Browse files Browse the repository at this point in the history
The plugin loader from `@gem_home` was removed during uninstallation.
However, this could leave behind the plugins for `--user-install`
installed gems.

Use `Gem::Specifictaions#base_dir` instead. This ensures that the plugin
loader for associated .gemspec is uninstalled.

rubygems/rubygems@6047f78210
  • Loading branch information
voxik authored and matzbot committed May 13, 2024
1 parent a86ad47 commit 5880103
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/rubygems/uninstaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def initialize(gem, options = {})
@gem = gem
@version = options[:version] || Gem::Requirement.default
@gem_home = File.realpath(options[:install_dir] || Gem.dir)
@plugins_dir = Gem.plugindir(@gem_home)
@force_executables = options[:executables]
@force_all = options[:all]
@force_ignore = options[:ignore]
Expand Down Expand Up @@ -284,7 +283,7 @@ def remove(spec)
def remove_plugins(spec) # :nodoc:
return if spec.plugins.empty?

remove_plugins_for(spec, @plugins_dir)
remove_plugins_for(spec, plugin_dir_for(spec))
end

##
Expand All @@ -294,7 +293,7 @@ def regenerate_plugins
latest = Gem::Specification.latest_spec_for(@spec.name)
return if latest.nil?

regenerate_plugins_for(latest, @plugins_dir)
regenerate_plugins_for(latest, plugin_dir_for(@spec))
end

##
Expand Down Expand Up @@ -406,4 +405,8 @@ def warn_cannot_uninstall_default_gems(specs)
say "Gem #{spec.full_name} cannot be uninstalled because it is a default gem"
end
end

def plugin_dir_for(spec)
Gem.plugindir(spec.base_dir)
end
end
19 changes: 19 additions & 0 deletions test/rubygems/test_gem_uninstaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ def test_uninstall_with_install_dir_removes_plugins
assert File.exist?(plugin_path), "plugin unintentionally removed"
end

def test_remove_plugins_user_installed
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
io.write "# do nothing"
end

@spec.files += %w[lib/rubygems_plugin.rb]

Gem::Installer.at(Gem::Package.build(@spec), force: true, user_install: true).install

plugin_path = File.join Gem.user_dir, "plugins/a_plugin.rb"
assert File.exist?(plugin_path), "plugin not written"

Gem::Specification.dirs = [Gem.dir, Gem.user_dir]

Gem::Uninstaller.new(@spec.name, executables: true, force: true, user_install: true).uninstall

refute File.exist?(plugin_path), "plugin not removed"
end

def test_regenerate_plugins_for
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
io.write "# do nothing"
Expand Down

0 comments on commit 5880103

Please sign in to comment.