From adb2f5cbd6e520d85ac293f3bd011bea3a3bf3a3 Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Mon, 8 Aug 2016 22:41:06 -0700 Subject: [PATCH] Set loaded_from before using extension_dir to ensure it is correct for --user or other alternate install path modes. Fixes https://github.com/rubygems/rubygems/issues/1680 --- lib/rubygems/installer.rb | 11 ++++++++--- test/rubygems/test_gem_installer.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/rubygems/installer.rb b/lib/rubygems/installer.rb index 6e771855476c..f4d3e728deec 100644 --- a/lib/rubygems/installer.rb +++ b/lib/rubygems/installer.rb @@ -282,18 +282,23 @@ def install run_pre_install_hooks + # Set loaded_from to ensure extension_dir is correct + if @options[:install_as_default] then + spec.loaded_from = default_spec_file + else + spec.loaded_from = spec_file + end + # Completely remove any previous gem files FileUtils.rm_rf gem_dir FileUtils.rm_rf spec.extension_dir FileUtils.mkdir_p gem_dir - if @options[:install_as_default] - spec.loaded_from = default_spec_file + if @options[:install_as_default] then extract_bin write_default_spec else - spec.loaded_from = spec_file extract_files build_extensions diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb index 2d747bed7288..6ceb2c6dfc0a 100644 --- a/test/rubygems/test_gem_installer.rb +++ b/test/rubygems/test_gem_installer.rb @@ -1141,6 +1141,35 @@ def test_install_extension_dir_is_removed_on_reinstall refute_path_exists should_be_removed end + def test_install_user_extension_dir + @spec.extensions << "extconf.rb" + write_file File.join(@tempdir, "extconf.rb") do |io| + io.write <<-RUBY + require "mkmf" + create_makefile("#{@spec.name}") + RUBY + end + + @spec.files += %w[extconf.rb] + + # Create the non-user ext dir + expected_extension_dir = @spec.extension_dir.dup + FileUtils.mkdir_p expected_extension_dir + + use_ui @ui do + path = Gem::Package.build @spec + + installer = Gem::Installer.at path, :user_install => true + installer.install + end + + expected_makefile = File.join Gem.user_dir, 'gems', @spec.full_name, 'Makefile' + + assert_path_exists expected_makefile + assert_path_exists expected_extension_dir + refute_path_exists File.join expected_extension_dir, 'gem_make.out' + end + # ruby core repository needs to `depend` file for extension build. # but 1.9.2 and earlier mkmf.rb does not create TOUCH file like depend. if RUBY_VERSION < '1.9.3'