Skip to content

Commit 0bde387

Browse files
Merge pull request #8764 from rubygems/deivid-rodriguez/fix-local-ext-reinstall
Fix `gem install` sometimes compiling the wrong source files
2 parents a2b51a6 + 1c282d9 commit 0bde387

5 files changed

Lines changed: 80 additions & 72 deletions

File tree

lib/rubygems/request_set.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,10 @@ def install(options, &block) # :yields: request, installer
181181

182182
# Install requested gems after they have been downloaded
183183
sorted_requests.each do |req|
184-
if req.installed?
184+
if req.installed? && @always_install.none? {|spec| spec == req.spec.spec }
185185
req.spec.spec.build_extensions
186-
187-
if @always_install.none? {|spec| spec == req.spec.spec }
188-
yield req, nil if block_given?
189-
next
190-
end
186+
yield req, nil if block_given?
187+
next
191188
end
192189

193190
spec =

test/rubygems/helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,14 @@ def write_file(path)
683683
path
684684
end
685685

686+
def write_dummy_extconf(gem_name)
687+
write_file File.join(@tempdir, "extconf.rb") do |io|
688+
io.puts "require 'mkmf'"
689+
yield io if block_given?
690+
io.puts "create_makefile '#{gem_name}'"
691+
end
692+
end
693+
686694
##
687695
# Load a YAML string, the psych 3 way
688696

test/rubygems/test_gem_commands_install_command.rb

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,10 @@ def test_execute_rdoc
647647
@cmd.options[:args] = %w[a]
648648

649649
use_ui @ui do
650-
# Don't use Dir.chdir with a block, it warnings a lot because
651-
# of a downstream Dir.chdir with a block
652-
old = Dir.getwd
653-
654-
begin
655-
Dir.chdir @tempdir
650+
Dir.chdir @tempdir do
656651
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
657652
@cmd.execute
658653
end
659-
ensure
660-
Dir.chdir old
661654
end
662655
end
663656

@@ -684,17 +677,10 @@ def test_execute_rdoc_with_path
684677
@cmd.options[:args] = %w[a]
685678

686679
use_ui @ui do
687-
# Don't use Dir.chdir with a block, it warnings a lot because
688-
# of a downstream Dir.chdir with a block
689-
old = Dir.getwd
690-
691-
begin
692-
Dir.chdir @tempdir
680+
Dir.chdir @tempdir do
693681
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
694682
@cmd.execute
695683
end
696-
ensure
697-
Dir.chdir old
698684
end
699685
end
700686

@@ -720,17 +706,10 @@ def test_execute_saves_build_args
720706
@cmd.options[:args] = %w[a]
721707

722708
use_ui @ui do
723-
# Don't use Dir.chdir with a block, it warnings a lot because
724-
# of a downstream Dir.chdir with a block
725-
old = Dir.getwd
726-
727-
begin
728-
Dir.chdir @tempdir
709+
Dir.chdir @tempdir do
729710
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
730711
@cmd.execute
731712
end
732-
ensure
733-
Dir.chdir old
734713
end
735714
end
736715

test/rubygems/test_gem_dependency_installer.rb

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,9 @@ def test_install_dependency_existing_extension
382382
FileUtils.mv f1_gem, @tempdir
383383
inst = nil
384384

385-
pwd = Dir.getwd
386-
Dir.chdir @tempdir
387-
begin
385+
Dir.chdir @tempdir do
388386
inst = Gem::DependencyInstaller.new
389387
inst.install "f"
390-
ensure
391-
Dir.chdir pwd
392388
end
393389

394390
assert_equal %w[f-1], inst.installed_gems.map(&:full_name)
@@ -523,6 +519,58 @@ def test_install_local_subdir
523519
assert_equal %w[a-1], inst.installed_gems.map(&:full_name)
524520
end
525521

522+
def test_install_local_with_extensions_already_installed
523+
pend "needs investigation" if Gem.java_platform?
524+
pend "ruby.h is not provided by ruby repo" if ruby_repo?
525+
526+
@spec = quick_gem "a" do |s|
527+
s.extensions << "extconf.rb"
528+
s.files += %w[extconf.rb a.c]
529+
end
530+
531+
write_dummy_extconf "a"
532+
533+
c_source_path = File.join(@tempdir, "a.c")
534+
535+
write_file c_source_path do |io|
536+
io.write <<-C
537+
#include <ruby.h>
538+
void Init_a() { }
539+
C
540+
end
541+
542+
package_path = Gem::Package.build @spec
543+
installer = Gem::Installer.at(package_path)
544+
545+
# Make sure the gem is installed and backup the correct package
546+
547+
installer.install
548+
549+
package_bkp_path = "#{package_path}.bkp"
550+
FileUtils.cp package_path, package_bkp_path
551+
552+
# Break the extension, rebuild it, and try to install it
553+
554+
write_file c_source_path do |io|
555+
io.write "typo"
556+
end
557+
558+
Gem::Package.build @spec
559+
560+
assert_raise Gem::Ext::BuildError do
561+
installer.install
562+
end
563+
564+
# Make sure installing the good package again still works
565+
566+
FileUtils.cp "#{package_path}.bkp", package_path
567+
568+
Dir.chdir @tempdir do
569+
inst = Gem::DependencyInstaller.new domain: :local
570+
inst.install package_path
571+
end
572+
end
573+
526574
def test_install_minimal_deps
527575
util_setup_gems
528576

test/rubygems/test_gem_installer.rb

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,12 +1478,7 @@ def test_install_extension_dir
14781478

14791479
@spec = setup_base_spec
14801480
@spec.extensions << "extconf.rb"
1481-
write_file File.join(@tempdir, "extconf.rb") do |io|
1482-
io.write <<-RUBY
1483-
require "mkmf"
1484-
create_makefile("#{@spec.name}")
1485-
RUBY
1486-
end
1481+
write_dummy_extconf @spec.name
14871482

14881483
@spec.files += %w[extconf.rb]
14891484

@@ -1503,12 +1498,7 @@ def test_install_extension_dir_is_removed_on_reinstall
15031498
@spec = setup_base_spec
15041499

15051500
@spec.extensions << "extconf.rb"
1506-
write_file File.join(@tempdir, "extconf.rb") do |io|
1507-
io.write <<-RUBY
1508-
require "mkmf"
1509-
create_makefile("#{@spec.name}")
1510-
RUBY
1511-
end
1501+
write_dummy_extconf @spec.name
15121502

15131503
@spec.files += %w[extconf.rb]
15141504

@@ -1539,12 +1529,7 @@ def test_install_extension_dir_is_removed_on_reinstall
15391529
def test_install_user_extension_dir
15401530
@spec = setup_base_spec
15411531
@spec.extensions << "extconf.rb"
1542-
write_file File.join(@tempdir, "extconf.rb") do |io|
1543-
io.write <<-RUBY
1544-
require "mkmf"
1545-
create_makefile("#{@spec.name}")
1546-
RUBY
1547-
end
1532+
write_dummy_extconf @spec.name
15481533

15491534
@spec.files += %w[extconf.rb]
15501535

@@ -1571,15 +1556,13 @@ def test_find_lib_file_after_install
15711556

15721557
@spec = setup_base_spec
15731558
@spec.extensions << "extconf.rb"
1574-
write_file File.join(@tempdir, "extconf.rb") do |io|
1559+
write_dummy_extconf @spec.name do |io|
15751560
io.write <<-RUBY
1576-
require "mkmf"
15771561
15781562
CONFIG['CC'] = '$(TOUCH) $@ ||'
15791563
CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
15801564
$ruby = '#{Gem.ruby}'
15811565
1582-
create_makefile("#{@spec.name}")
15831566
RUBY
15841567
end
15851568

@@ -1618,12 +1601,7 @@ def test_install_extension_and_script
16181601

16191602
@spec = setup_base_spec
16201603
@spec.extensions << "extconf.rb"
1621-
write_file File.join(@tempdir, "extconf.rb") do |io|
1622-
io.write <<-RUBY
1623-
require "mkmf"
1624-
create_makefile("#{@spec.name}")
1625-
RUBY
1626-
end
1604+
write_dummy_extconf @spec.name
16271605

16281606
rb = File.join("lib", "#{@spec.name}.rb")
16291607
@spec.files += [rb]
@@ -1663,15 +1641,13 @@ def test_install_extension_flat
16631641

16641642
@spec.extensions << "extconf.rb"
16651643

1666-
write_file File.join(@tempdir, "extconf.rb") do |io|
1644+
write_dummy_extconf @spec.name do |io|
16671645
io.write <<-RUBY
1668-
require "mkmf"
16691646
16701647
CONFIG['CC'] = '$(TOUCH) $@ ||'
16711648
CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
16721649
$ruby = '#{Gem.ruby}'
16731650
1674-
create_makefile("#{@spec.name}")
16751651
RUBY
16761652
end
16771653

@@ -1698,13 +1674,13 @@ def test_install_extension_clean_intermediate_files
16981674
@spec.require_paths = ["."]
16991675
@spec.extensions << "extconf.rb"
17001676

1701-
File.write File.join(@tempdir, "extconf.rb"), <<-RUBY
1702-
require "mkmf"
1703-
CONFIG['CC'] = '$(TOUCH) $@ ||'
1704-
CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
1705-
$ruby = '#{Gem.ruby}'
1706-
create_makefile("#{@spec.name}")
1707-
RUBY
1677+
write_dummy_extconf @spec.name do |io|
1678+
io.write <<~RUBY
1679+
CONFIG['CC'] = '$(TOUCH) $@ ||'
1680+
CONFIG['LDSHARED'] = '$(TOUCH) $@ ||'
1681+
$ruby = '#{Gem.ruby}'
1682+
RUBY
1683+
end
17081684

17091685
# empty depend file for no auto dependencies
17101686
@spec.files += %W[depend #{@spec.name}.c].each do |file|

0 commit comments

Comments
 (0)