diff --git a/bundler/lib/bundler/source/rubygems.rb b/bundler/lib/bundler/source/rubygems.rb index e1e030ffc899..e679010e97dc 100644 --- a/bundler/lib/bundler/source/rubygems.rb +++ b/bundler/lib/bundler/source/rubygems.rb @@ -504,7 +504,12 @@ def download_cache_path(spec) return unless remote = spec.remote return unless cache_slug = remote.cache_slug - Bundler.user_cache.join("gems", cache_slug) + if Gem.respond_to?(:global_gem_cache_path) + Pathname.new(Gem.global_gem_cache_path).join(cache_slug) + else + # Fall back to old location for older RubyGems versions + Bundler.user_cache.join("gems", cache_slug) + end end def extension_cache_slug(spec) diff --git a/bundler/spec/install/global_cache_spec.rb b/bundler/spec/install/global_cache_spec.rb index 0a7daf173c8b..77ab94609ee5 100644 --- a/bundler/spec/install/global_cache_spec.rb +++ b/bundler/spec/install/global_cache_spec.rb @@ -8,7 +8,13 @@ let(:source2) { "http://gemserver.example.org" } def cache_base - home(".bundle", "cache", "gems") + # Use the unified global gem cache path if available (from RubyGems), + # otherwise fall back to the Bundler-specific cache location + if Gem.respond_to?(:global_gem_cache_path) + Pathname.new(Gem.global_gem_cache_path) + else + home(".bundle", "cache", "gems") + end end def source_global_cache(*segments) diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index e58a83f6b75a..f2c90cce3cb7 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -49,6 +49,7 @@ class Gem::ConfigFile DEFAULT_IPV4_FALLBACK_ENABLED = false # TODO: Use false as default value for this option in RubyGems 4.0 DEFAULT_INSTALL_EXTENSION_IN_LIB = true + DEFAULT_GLOBAL_GEM_CACHE = false ## # For Ruby packagers to set configuration defaults. Set in @@ -155,6 +156,12 @@ class Gem::ConfigFile attr_accessor :ipv4_fallback_enabled + ## + # Use a global cache for .gem files shared across all Ruby installations. + # When enabled, gems are cached to ~/.cache/gem/gems (or XDG_CACHE_HOME/gem/gems). + + attr_accessor :global_gem_cache + ## # Path name of directory or file of openssl client certificate, used for remote https connection with client authentication @@ -192,6 +199,7 @@ def initialize(args) @cert_expiration_length_days = DEFAULT_CERT_EXPIRATION_LENGTH_DAYS @install_extension_in_lib = DEFAULT_INSTALL_EXTENSION_IN_LIB @ipv4_fallback_enabled = ENV["IPV4_FALLBACK_ENABLED"] == "true" || DEFAULT_IPV4_FALLBACK_ENABLED + @global_gem_cache = ENV["RUBYGEMS_GLOBAL_GEM_CACHE"] == "true" || DEFAULT_GLOBAL_GEM_CACHE operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS) platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS) @@ -213,8 +221,8 @@ def initialize(args) @hash.transform_keys! do |k| # gemhome and gempath are not working with symbol keys if %w[backtrace bulk_threshold verbose update_sources cert_expiration_length_days - install_extension_in_lib ipv4_fallback_enabled sources disable_default_gem_server - ssl_verify_mode ssl_ca_cert ssl_client_cert].include?(k) + install_extension_in_lib ipv4_fallback_enabled global_gem_cache sources + disable_default_gem_server ssl_verify_mode ssl_ca_cert ssl_client_cert].include?(k) k.to_sym else k @@ -230,6 +238,7 @@ def initialize(args) @cert_expiration_length_days = @hash[:cert_expiration_length_days] if @hash.key? :cert_expiration_length_days @install_extension_in_lib = @hash[:install_extension_in_lib] if @hash.key? :install_extension_in_lib @ipv4_fallback_enabled = @hash[:ipv4_fallback_enabled] if @hash.key? :ipv4_fallback_enabled + @global_gem_cache = @hash[:global_gem_cache] if @hash.key? :global_gem_cache @home = @hash[:gemhome] if @hash.key? :gemhome @path = @hash[:gempath] if @hash.key? :gempath diff --git a/lib/rubygems/defaults.rb b/lib/rubygems/defaults.rb index 90f09fc19174..2247c49c81ec 100644 --- a/lib/rubygems/defaults.rb +++ b/lib/rubygems/defaults.rb @@ -148,6 +148,15 @@ def self.cache_home @cache_home ||= ENV["XDG_CACHE_HOME"] || File.join(Gem.user_home, ".cache") end + ## + # The path to the global gem cache directory. + # This is used when global_gem_cache is enabled to share .gem files + # across all Ruby installations. + + def self.global_gem_cache_path + File.join(cache_home, "gem", "gems") + end + ## # The path to standard location of the user's data directory. diff --git a/lib/rubygems/remote_fetcher.rb b/lib/rubygems/remote_fetcher.rb index 805f7aaf82ed..0321600ffb74 100644 --- a/lib/rubygems/remote_fetcher.rb +++ b/lib/rubygems/remote_fetcher.rb @@ -111,9 +111,13 @@ def download_to_cache(dependency) # always replaced. def download(spec, source_uri, install_dir = Gem.dir) + gem_file_name = File.basename spec.cache_file + install_cache_dir = File.join install_dir, "cache" cache_dir = - if Dir.pwd == install_dir # see fetch_command + if Gem.configuration.global_gem_cache + Gem.global_gem_cache_path + elsif Dir.pwd == install_dir # see fetch_command install_dir elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && !File.exist?(install_cache_dir)) install_cache_dir @@ -121,7 +125,6 @@ def download(spec, source_uri, install_dir = Gem.dir) File.join Gem.user_dir, "cache" end - gem_file_name = File.basename spec.cache_file local_gem_path = File.join cache_dir, gem_file_name require "fileutils" diff --git a/test/rubygems/test_gem_config_file.rb b/test/rubygems/test_gem_config_file.rb index 4230eda4d399..79bf5f582c94 100644 --- a/test/rubygems/test_gem_config_file.rb +++ b/test/rubygems/test_gem_config_file.rb @@ -83,6 +83,33 @@ def test_initialize_ipv4_fallback_enabled_env util_config_file %W[--config-file #{@temp_conf}] assert_equal true, @cfg.ipv4_fallback_enabled + ensure + ENV.delete("IPV4_FALLBACK_ENABLED") + end + + def test_initialize_global_gem_cache_default + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal false, @cfg.global_gem_cache + end + + def test_initialize_global_gem_cache_env + ENV["RUBYGEMS_GLOBAL_GEM_CACHE"] = "true" + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal true, @cfg.global_gem_cache + ensure + ENV.delete("RUBYGEMS_GLOBAL_GEM_CACHE") + end + + def test_initialize_global_gem_cache_gemrc + File.open @temp_conf, "w" do |fp| + fp.puts "global_gem_cache: true" + end + + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal true, @cfg.global_gem_cache end def test_initialize_handle_arguments_config_file diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb index 9badd75b4271..922819315224 100644 --- a/test/rubygems/test_gem_remote_fetcher.rb +++ b/test/rubygems/test_gem_remote_fetcher.rb @@ -575,6 +575,74 @@ def test_yaml_error_on_size end end + def test_download_with_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = true + + # Use the real RemoteFetcher with stubbed fetch_path + fetcher = Gem::RemoteFetcher.fetcher + def fetcher.fetch_path(uri, *rest) + File.binread File.join(@test_gem_dir, "a-1.gem") + end + fetcher.instance_variable_set(:@test_gem_dir, File.dirname(@a1_gem)) + + # With global cache enabled, gem goes directly to global cache + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + assert_equal global_cache_gem, fetcher.download(@a1, "http://gems.example.com") + assert File.exist?(global_cache_gem), "Gem should be in global cache" + end + ensure + Gem.configuration.global_gem_cache = false + end + + def test_download_uses_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = true + + # Pre-populate global cache + FileUtils.mkdir_p test_cache_dir + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + FileUtils.cp @a1_gem, global_cache_gem + + fetcher = Gem::RemoteFetcher.fetcher + + # Should return global cache path without downloading + result = fetcher.download(@a1, "http://gems.example.com") + assert_equal global_cache_gem, result + end + ensure + Gem.configuration.global_gem_cache = false + end + + def test_download_without_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = false + + # Use the real RemoteFetcher with stubbed fetch_path + fetcher = Gem::RemoteFetcher.fetcher + def fetcher.fetch_path(uri, *rest) + File.binread File.join(@test_gem_dir, "a-1.gem") + end + fetcher.instance_variable_set(:@test_gem_dir, File.dirname(@a1_gem)) + + a1_cache_gem = @a1.cache_file + assert_equal a1_cache_gem, fetcher.download(@a1, "http://gems.example.com") + + # Verify gem was NOT copied to global cache + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + refute File.exist?(global_cache_gem), "Gem should not be copied to global cache when disabled" + end + end + private def assert_error(exception_class = Exception)