From 48fdb9faa0a408c0855e84cfd6451393c8b67180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 8 Oct 2024 23:56:32 +0200 Subject: [PATCH] [rubygems/rubygems] Fix `gem contents` for default gems A default gem does not always live in the same place. For example, Bundler may be installed to `site_dir` when RubyGems have been upgraded. A more reliable way seems to actually activate the default gem, so that we can know for sure where it lives. https://github.com/rubygems/rubygems/commit/c69f6dfb18 --- lib/rubygems/commands/contents_command.rb | 23 ++++++++++++------- .../test_gem_commands_contents_command.rb | 6 ++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/rubygems/commands/contents_command.rb b/lib/rubygems/commands/contents_command.rb index 807158d9c9..bd0cbce8ba 100644 --- a/lib/rubygems/commands/contents_command.rb +++ b/lib/rubygems/commands/contents_command.rb @@ -103,16 +103,23 @@ prefix or only the files that are requireable. def files_in_default_gem(spec) spec.files.map do |file| - case file - when %r{\A#{spec.bindir}/} - # $' is POSTMATCH - [RbConfig::CONFIG["bindir"], $'] - when /\.so\z/ - [RbConfig::CONFIG["archdir"], file] + if file.start_with?("#{spec.bindir}/") + [RbConfig::CONFIG["bindir"], file.delete_prefix("#{spec.bindir}/")] else - [RbConfig::CONFIG["rubylibdir"], file] + gem spec.name, spec.version + + require_path = spec.require_paths.find do |path| + file.start_with?("#{path}/") + end + + requirable_part = file.delete_prefix("#{require_path}/") + + resolve = $LOAD_PATH.resolve_feature_path(requirable_part)&.last + next unless resolve + + [resolve.delete_suffix(requirable_part), requirable_part] end - end + end.compact end def gem_contents(name) diff --git a/test/rubygems/test_gem_commands_contents_command.rb b/test/rubygems/test_gem_commands_contents_command.rb index d8e6ba3dec..049afe8a01 100644 --- a/test/rubygems/test_gem_commands_contents_command.rb +++ b/test/rubygems/test_gem_commands_contents_command.rb @@ -227,7 +227,6 @@ lib/foo.rb default_gem_spec = new_default_spec("default", "2.0.0.0", nil, "default/gem.rb") default_gem_spec.executables = ["default_command"] - default_gem_spec.files += ["default_gem.so"] install_default_gems(default_gem_spec) @cmd.options[:args] = %w[default] @@ -237,9 +236,8 @@ lib/foo.rb end expected = [ - [RbConfig::CONFIG["bindir"], "default_command"], - [RbConfig::CONFIG["rubylibdir"], "default/gem.rb"], - [RbConfig::CONFIG["archdir"], "default_gem.so"], + [File.join(@gemhome, "bin"), "default_command"], + [File.join(@tempdir, "default_gems", "lib"), "default/gem.rb"], ].sort.map {|a|File.join a }.join "\n" assert_equal expected, @ui.output.chomp