Skip to content
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
14 changes: 13 additions & 1 deletion lib/bundler/multilock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,16 @@ def cli
CLI.instance
end

# @!visibility private
# Whether plugins.rb is being loaded by `bundle plugin install` (or a
# Gemfile's `plugin` command), as opposed to being loaded from the plugin
# index for some other bundle command
def installing_plugin?
caller_locations.any? do |location|
location.base_label == "register_plugin" && location.path.end_with?("bundler/plugin.rb")
end
end

# @!visibility private
def cli_loaded?
Bundler.autoload?(:CLI).nil? && !defined?(Bundler::CLI).nil?
Expand Down Expand Up @@ -1226,7 +1236,9 @@ def write_lockfile(lockfile_definition,
end
end

Bundler::Multilock::Preamble.inject unless Bundler::Multilock.loaded? || Bundler.frozen_bundle?
if Bundler::Multilock.installing_plugin? && !Bundler::Multilock.loaded? && !Bundler.frozen_bundle?
Bundler::Multilock::Preamble.inject
end

if Bundler::Multilock.cli_loaded?
require_relative "multilock/ext/cli"
Expand Down
26 changes: 26 additions & 0 deletions spec/bundler/multilock/preamble_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,30 @@
expect(File.read("Gemfile")).to eq gemfile
end
end

it "does not inject the preamble into an alternate Gemfile that's loaded from the plugin index" do
with_gemfile(<<~RUBY) do
lockfile("alt", gemfile: "alt/Gemfile", lockfile: "alt/Gemfile.lock")
RUBY
FileUtils.mkdir_p("alt")
alt_gemfile = <<~RUBY
source "https://rubygems.org"
RUBY
File.write("alt/Gemfile", alt_gemfile)

# the preamble is expected in the main Gemfile after installing the plugin
local_path = Shellwords.escape(plugin_path)
invoke_bundler("plugin install bundler-multilock --path=#{local_path}")
expect(File.read("Gemfile")).to include("bundler-multilock")

# the plugin index lives in the app config directory, which is normally next to the Gemfile.
# Pointing at the main one makes the plugin load for every bundle command, even when
# the Gemfile doesn't mention it
invoke_bundler("list",
env: { "BUNDLE_GEMFILE" => "alt/Gemfile",
"BUNDLE_APP_CONFIG" => File.join(Dir.pwd, ".bundle") })

expect(File.read("alt/Gemfile")).to eq alt_gemfile
end
end
end
Loading