Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix double-sync #1

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

37 changes: 36 additions & 1 deletion 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 All @@ -346,7 +349,16 @@ def inject_preamble
end

builder = Bundler::Plugin::DSL.new
builder.eval_gemfile(Bundler.default_gemfile)
# this method is called as part of the plugin loading, but @loaded_plugin_names
# hasn't been set yet, so avoid re-entrancy issues
plugins = Bundler::Plugin.instance_variable_get(:@loaded_plugin_names)
original_plugins = plugins.dup
plugins << "bundler-multilock"
begin
builder.eval_gemfile(Bundler.default_gemfile)
ensure
plugins.replace(original_plugins)
end
gemfiles = builder.instance_variable_get(:@gemfiles).map(&:read)

modified = inject_specific_preamble(gemfile, gemfiles, injection_point, bundle_preamble2, add_newline: true)
Expand Down Expand Up @@ -448,3 +460,26 @@ 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 "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
23 changes: 0 additions & 23 deletions plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,6 @@

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