Skip to content

Commit

Permalink
fix double-sync
Browse files Browse the repository at this point in the history
if the preamble already exists, injecting the preamble would evaluate
the gemfile, and recursively load the plugin (and plugins.rb).
avoid the problems of that by only adding our hooks in multilock.rb,
which will only be required once
  • Loading branch information
ccutrer committed Oct 5, 2023
1 parent c60670f commit 1bb0f26
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 0 additions & 1 deletion .rspec-local

This file was deleted.

30 changes: 30 additions & 0 deletions lib/bundler/multilock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def after_install_all(install: true)
require "tempfile"
require_relative "multilock/lockfile_generator"

Bundler.ui.debug("Syncing to alternate lockfiles")
Bundler.ui.info ""

default_lockfile_contents = Bundler.default_lockfile.read.freeze
Expand Down Expand Up @@ -321,6 +322,8 @@ def loaded?

# @!visibility private
def inject_preamble
Bundler.ui.debug("Injecting multilock preamble")

minor_version = Gem::Version.new(::Bundler::Multilock::VERSION).segments[0..1].join(".")
bundle_preamble1_match = %(plugin "bundler-multilock")
bundle_preamble1 = <<~RUBY
Expand Down Expand Up @@ -448,3 +451,30 @@ def write_lockfile(lockfile_definition, lockfile, install:, dependency_changes:
end

Bundler::Multilock.inject_preamble unless Bundler::Multilock.loaded?

# this is terrible, but we can't prepend into these modules because we only load
# _inside_ of the CLI commands already running
if defined?(Bundler::CLI::Check)
require_relative "lib/bundler/multilock/check"
at_exit do
next unless $!.nil?
next if $!.is_a?(SystemExit) && !$!.success?

next if Bundler::Multilock::Check.run

Bundler.ui.warn("You can attempt to fix by running `bundle install`")
exit 1
end
end
if defined?(Bundler::CLI::Lock)
at_exit do
next unless $!.nil?
next if $!.is_a?(SystemExit) && !$!.success?

Bundler::Multilock.after_install_all(install: false)
end
end

Bundler::Plugin.add_hook(Bundler::Plugin::Events::GEM_AFTER_INSTALL_ALL) do |_|
Bundler::Multilock.after_install_all
end
27 changes: 0 additions & 27 deletions plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,3 @@
#

require_relative "lib/bundler/multilock"

# this is terrible, but we can't prepend into these modules because we only load
# _inside_ of the CLI commands already running
if defined?(Bundler::CLI::Check)
require_relative "lib/bundler/multilock/check"
at_exit do
next unless $!.nil?
next if $!.is_a?(SystemExit) && !$!.success?

next if Bundler::Multilock::Check.run

Bundler.ui.warn("You can attempt to fix by running `bundle install`")
exit 1
end
end
if defined?(Bundler::CLI::Lock)
at_exit do
next unless $!.nil?
next if $!.is_a?(SystemExit) && !$!.success?

Bundler::Multilock.after_install_all(install: false)
end
end

Bundler::Plugin.add_hook(Bundler::Plugin::Events::GEM_AFTER_INSTALL_ALL) do |_|
Bundler::Multilock.after_install_all
end
31 changes: 27 additions & 4 deletions spec/bundler/multilock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
gem "concurrent-ruby", "1.2.2"
RUBY
create_local_gem("test_local", "")
create_local_gem("test_local")

invoke_bundler("install")

Expand Down Expand Up @@ -611,11 +611,32 @@
end
end

it "only syncs once per lockfile" do
with_gemfile(<<~RUBY) do
gemspec
lockfile("rails-7.0", default: true) do
gem "activesupport", "~> 7.0.0"
end
RUBY
create_local_gem("test", subdirectory: false)
invoke_bundler("install")
output = invoke_bundler("install", env: { "DEBUG" => "1" })

expect(output.split("\n").grep(/Syncing to alternate lockfiles/).length).to be 1
end
end

private

def create_local_gem(name, content)
FileUtils.mkdir_p(name)
File.write("#{name}/#{name}.gemspec", <<~RUBY)
def create_local_gem(name, content = "", subdirectory: true)
if subdirectory
FileUtils.mkdir_p(name)
subdirectory = "#{name}/"
else
subdirectory = nil
end
File.write("#{subdirectory}#{name}.gemspec", <<~RUBY)
Gem::Specification.new do |spec|
spec.name = #{name.inspect}
spec.version = "0.0.1"
Expand All @@ -626,6 +647,8 @@ def create_local_gem(name, content)
end
RUBY

return unless subdirectory

File.write("#{name}/Gemfile", <<~RUBY)
source "https://rubygems.org"
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "debug"

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.max_formatted_output_length = nil
Expand Down

0 comments on commit 1bb0f26

Please sign in to comment.