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
3 changes: 3 additions & 0 deletions lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,15 @@ def add_dependency(name, version = nil, options = {})
end

def with_gemfile(gemfile)
previous_evaluating_gemfile = Thread.current[:bundler_evaluating_gemfile]
expanded_gemfile_path = Pathname.new(gemfile).expand_path(@gemfile&.parent)
original_gemfile = @gemfile
@gemfile = expanded_gemfile_path
@gemfiles << expanded_gemfile_path
Thread.current[:bundler_evaluating_gemfile] = @gemfile
yield @gemfile.to_s
ensure
Thread.current[:bundler_evaluating_gemfile] = previous_evaluating_gemfile
@gemfile = original_gemfile
end

Expand Down
13 changes: 13 additions & 0 deletions lib/bundler/rubygems_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ def source

prepend AllowSettingSource

module TrackGemfileActivation
# The Gemfile being evaluated when RubyGems activated this spec.
attr_reader :bundler_activating_gemfile

def activate
activated = super
@bundler_activating_gemfile = Thread.current[:bundler_evaluating_gemfile] if activated
activated
end
end

prepend TrackGemfileActivation

alias_method :rg_full_gem_path, :full_gem_path
alias_method :rg_loaded_from, :loaded_from

Expand Down
6 changes: 5 additions & 1 deletion lib/bundler/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ def check_for_activated_spec!(spec)
return unless activated_spec = Bundler.rubygems.loaded_specs(spec.name)
return if activated_spec.version == spec.version

suggestion = if activated_spec.default_gem?
activating_gemfile = activated_spec.bundler_activating_gemfile if activated_spec.respond_to?(:bundler_activating_gemfile)
suggestion = if @definition.gemfiles.include?(activating_gemfile)
"#{spec.name} was activated while evaluating your Gemfile. If a `require` in your Gemfile loads it, " \
"remove that call or move it outside the Gemfile, after `Bundler.setup`."
elsif activated_spec.default_gem?
"Since #{spec.name} is a default gem, you can either remove your dependency on it" \
" or try updating to a newer version of bundler that supports #{spec.name} as a default gem."
else
Expand Down
65 changes: 65 additions & 0 deletions spec/runtime/setup_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,71 @@
expect(out).to eq("yay")
end

shared_examples "a gem activation conflict" do |default_gem, fallback|
let(:ruby_options) do
{ env: { "BUNDLER_GEM_DEFAULT_DIR" => system_gem_path.to_s }, load_path: [lib_dir], raise_on_error: false }
end

before do
system_gems "myrack-1.0.0", "myrack_middleware-1.0"
system_gems "myrack-0.9.1", default: default_gem
gemfile <<~G
source "https://gem.repo1"
require "myrack_middleware"
gem "myrack", "1.0.0"
G
end

it "points to the require that activated the gem while evaluating the Gemfile" do
ruby 'require "bundler/setup"', ruby_options

expect(last_command).to be_failure
expect(err).to include("You have already activated myrack 0.9.1, but your Gemfile requires myrack 1.0.0. " \
"myrack was activated while evaluating your Gemfile. If a `require` in your Gemfile loads it, " \
"remove that call or move it outside the Gemfile, after `Bundler.setup`.")
end

it "keeps the original advice when the gem was already active before evaluating the Gemfile" do
ruby <<~RUBY, ruby_options
require "myrack_middleware"
require "bundler/setup"
RUBY

expect(last_command).to be_failure
expect(err).to include("You have already activated myrack 0.9.1, but your Gemfile requires myrack 1.0.0. #{fallback}")
end

it "keeps the original advice when another thread activates the gem during evaluation" do
gemfile <<~G
source "https://gem.repo1"
START_WORKER << true
WORKER.value
gem "myrack", "1.0.0"
G

ruby <<~RUBY, ruby_options
START_WORKER = Queue.new
WORKER = Thread.new do
START_WORKER.pop
require "myrack_middleware"
end
require "bundler/setup"
RUBY

expect(last_command).to be_failure
expect(err).to include("You have already activated myrack 0.9.1, but your Gemfile requires myrack 1.0.0. #{fallback}")
end
end

context "with a conflicting system gem" do
it_behaves_like "a gem activation conflict", false, "Prepending `bundle exec` to your command may solve this."
end

context "with a conflicting default gem" do
it_behaves_like "a gem activation conflict", true, "Since myrack is a default gem, you can either remove your dependency on it" \
" or try updating to a newer version of bundler that supports myrack as a default gem."
end

it "should clean $LOAD_PATH properly" do
gem_name = "very_simple_binary"
full_gem_name = gem_name + "-1.0"
Expand Down
Loading