[rubygems/rubygems] Remove temporary .lock files left around by gem installer

https://github.com/rubygems/rubygems/commit/edbb2e3475
This commit is contained in:
David Rodríguez 2024-09-11 16:42:24 +02:00 committed by git
parent 7411caa103
commit fab01b15e9
5 changed files with 49 additions and 17 deletions

View File

@ -30,9 +30,12 @@ module Gem
end end
end end
# Can be removed once RubyGems 3.5.14 support is dropped # Can be removed once RubyGems 3.5.18 support is dropped
unless Gem.respond_to?(:open_file_with_flock) unless Gem.respond_to?(:open_file_with_lock)
def self.open_file_with_flock(path, &block) class << self
remove_method :open_file_with_flock if Gem.respond_to?(:open_file_with_flock)
def open_file_with_flock(path, &block)
mode = IO::RDONLY | IO::APPEND | IO::CREAT | IO::BINARY mode = IO::RDONLY | IO::APPEND | IO::CREAT | IO::BINARY
mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE) mode |= IO::SHARE_DELETE if IO.const_defined?(:SHARE_DELETE)
@ -46,6 +49,14 @@ module Gem
yield io yield io
end end
end end
def open_file_with_lock(path, &block)
file_lock = "#{path}.lock"
open_file_with_flock(file_lock, &block)
ensure
FileUtils.rm_f file_lock
end
end
end end
require "rubygems/specification" require "rubygems/specification"

View File

@ -81,11 +81,11 @@ module Bundler
end end
end end
if Bundler.rubygems.provides?("< 3.5.15") if Bundler.rubygems.provides?("< 3.5.19")
def generate_bin_script(filename, bindir) def generate_bin_script(filename, bindir)
bin_script_path = File.join bindir, formatted_program_filename(filename) bin_script_path = File.join bindir, formatted_program_filename(filename)
Gem.open_file_with_flock("#{bin_script_path}.lock") do Gem.open_file_with_lock(bin_script_path) do
require "fileutils" require "fileutils"
FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers

View File

@ -794,6 +794,16 @@ An Array (#{env.inspect}) was passed in from #{caller[3]}
File.open(path, flags, &block) File.open(path, flags, &block)
end end
##
# Open a file with given flags, and protect access with a file lock
def self.open_file_with_lock(path, &block)
file_lock = "#{path}.lock"
open_file_with_flock(file_lock, &block)
ensure
FileUtils.rm_f file_lock
end
## ##
# Open a file with given flags, and protect access with flock # Open a file with given flags, and protect access with flock

View File

@ -538,7 +538,7 @@ class Gem::Installer
def generate_bin_script(filename, bindir) def generate_bin_script(filename, bindir)
bin_script_path = File.join bindir, formatted_program_filename(filename) bin_script_path = File.join bindir, formatted_program_filename(filename)
Gem.open_file_with_flock("#{bin_script_path}.lock") do |lock| Gem.open_file_with_lock(bin_script_path) do
require "fileutils" require "fileutils"
FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers
@ -546,8 +546,6 @@ class Gem::Installer
file.write app_script_text(filename) file.write app_script_text(filename)
file.chmod(options[:prog_mode] || 0o755) file.chmod(options[:prog_mode] || 0o755)
end end
ensure
FileUtils.rm_f lock.path
end end
verbose bin_script_path verbose bin_script_path

View File

@ -1591,4 +1591,17 @@ RSpec.describe "bundle install with gem sources" do
expect(err).to include("The running version of Bundler (9.99.9) does not match the version of the specification installed for it (9.99.8)") expect(err).to include("The running version of Bundler (9.99.9) does not match the version of the specification installed for it (9.99.8)")
end end
end end
it "only installs executable files in bin" do
bundle "config set --local path vendor/bundle"
install_gemfile <<~G
source "https://gem.repo1"
gem "myrack"
G
expected_executables = [vendored_gems("bin/myrackup").to_s]
expected_executables << vendored_gems("bin/myrackup.bat").to_s if Gem.win_platform?
expect(Dir.glob(vendored_gems("bin/*"))).to eq(expected_executables)
end
end end