Skip to content

Commit 33a6da5

Browse files
committed
Fix pacman install within a parallel bundler install -jX
pacman invocation must be serialized to avoid locking error. Fixes #397
1 parent 0064fe7 commit 33a6da5

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

lib/ruby_installer/build/msys2_installation.rb

+21-14
Original file line numberDiff line numberDiff line change
@@ -319,29 +319,36 @@ def disable_msys_apps_per_ps1
319319
end.join(";")
320320
end
321321

322+
@@pacman_lock = Mutex.new
323+
private def with_pacman_lock(&block)
324+
@@pacman_lock.synchronize(&block)
325+
end
326+
322327
def install_packages(packages, verbose: false)
323328
return if packages.empty?
324329

325330
with_msys_apps_enabled do
326-
# Find packages that are already installed
327-
skips, installs = packages.partition do |package|
328-
IO.popen(["pacman", "-Q", package], err: :out, &:read)
329-
$?.success?
330-
end
331+
with_pacman_lock do
332+
# Find packages that are already installed
333+
skips, installs = packages.partition do |package|
334+
IO.popen(["pacman", "-Q", package], err: :out, &:read)
335+
$?.success?
336+
end
331337

332-
Gem.ui.say("Using msys2 packages: #{skips.join(" ")}") if verbose && skips.any?
338+
Gem.ui.say("Using msys2 packages: #{skips.join(" ")}") if verbose && skips.any?
333339

334-
# Install required packages
335-
if installs.any?
336-
Gem.ui.say("Installing required msys2 packages: #{installs.join(" ")}") if verbose
340+
# Install required packages
341+
if installs.any?
342+
Gem.ui.say("Installing required msys2 packages: #{installs.join(" ")}") if verbose
337343

338-
args = ["pacman", "-S", "--needed", "--noconfirm", *installs]
339-
Gem.ui.say("> #{args.join(" ")}") if verbose==1
344+
args = ["pacman", "-S", "--needed", "--noconfirm", *installs]
345+
Gem.ui.say("> #{args.join(" ")}") if verbose==1
340346

341-
res = IO.popen(args, &:read)
342-
raise CommandError, "pacman failed with the following output:\n#{res}" if !$? || $?.exitstatus != 0
347+
res = IO.popen(args, &:read)
348+
raise CommandError, "pacman failed with the following output:\n#{res}" if !$? || $?.exitstatus != 0
343349

344-
Gem.ui.say(res) if verbose==1
350+
Gem.ui.say(res) if verbose==1
351+
end
345352
end
346353
end
347354
end

test/helper/testgem/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
source "https://dummy-url.nonexit"
2+
gem "testgem2", "1.0.0"
23
gem "testgem", "1.0.0"

test/helper/testgem/testgem2.gemspec

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Gem::Specification.new do |s|
2+
s.name = 'testgem2'
3+
s.version = '1.0.0'
4+
s.author = 'Lars Kanis'
5+
s.email = '[email protected]'
6+
s.homepage = 'https://github.com/larskanis/rubyinstaller2'
7+
s.summary = 'RubyInstaller2 testgem'
8+
s.description = 'A gem to test gem installation with RubyInstaller2'
9+
s.files = `git ls-files`.split("\n")
10+
s.extensions << 'ext/extconf.rb'
11+
s.license = 'BSD-3-Clause'
12+
s.require_paths << 'lib'
13+
s.required_ruby_version = '>= 2.1.0'
14+
s.metadata['msys2_dependencies'] = 'ed>=1.0'
15+
s.metadata['msys2_mingw_dependencies'] = 'libguess>=1.0 gcc>=8.0'
16+
end

test/test_gem_install.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ def test_bundle_install
3333
res = system <<-EOT.gsub("\n", "&")
3434
cd test/helper/testgem
3535
gem build testgem.gemspec
36+
gem build testgem2.gemspec
3637
copy /b testgem-1.0.0.gem "vendor/cache/"
37-
bundle install --local
38+
copy /b testgem2-1.0.0.gem "vendor/cache/"
39+
bundle install --local -j16
3840
EOT
3941
assert res, "shell commands should succeed"
4042

4143
out = IO.popen("bundle exec ruby -rtestgem -e \"puts Libguess.determine_encoding('abc', 'Greek')\"", chdir: "test/helper/testgem", &:read)
4244
assert_match(/UTF-8/, out.scrub, "call the ruby API of the testgem")
4345

44-
assert system("gem uninstall testgem --executables --force"), "uninstall testgem"
46+
assert system("gem uninstall testgem testgem2 --executables --force"), "uninstall testgem"
4547
FileUtils.rm("test/helper/testgem/testgem-1.0.0.gem")
4648
end
4749

0 commit comments

Comments
 (0)