Skip to content

Commit 5f965db

Browse files
Merge pull request #8059 from rubygems/deivid-rodriguez/inline-preserve-env-changes
Fix `bundler/inline` resetting ENV changes (cherry picked from commit f3e6663)
1 parent 30ee2b5 commit 5f965db

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

bundler/lib/bundler.rb

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -383,28 +383,12 @@ def clean_env
383383

384384
# @return [Hash] Environment with all bundler-related variables removed
385385
def unbundled_env
386-
env = original_env
387-
388-
if env.key?("BUNDLER_ORIG_MANPATH")
389-
env["MANPATH"] = env["BUNDLER_ORIG_MANPATH"]
390-
end
391-
392-
env.delete_if {|k, _| k[0, 7] == "BUNDLE_" }
393-
394-
if env.key?("RUBYOPT")
395-
rubyopt = env["RUBYOPT"].split(" ")
396-
rubyopt.delete("-r#{File.expand_path("bundler/setup", __dir__)}")
397-
rubyopt.delete("-rbundler/setup")
398-
env["RUBYOPT"] = rubyopt.join(" ")
399-
end
400-
401-
if env.key?("RUBYLIB")
402-
rubylib = env["RUBYLIB"].split(File::PATH_SEPARATOR)
403-
rubylib.delete(__dir__)
404-
env["RUBYLIB"] = rubylib.join(File::PATH_SEPARATOR)
405-
end
386+
unbundle_env(original_env)
387+
end
406388

407-
env
389+
# Remove all bundler-related variables from ENV
390+
def unbundle_env!
391+
ENV.replace(unbundle_env(ENV))
408392
end
409393

410394
# Run block with environment present before Bundler was activated
@@ -633,6 +617,30 @@ def self_manager
633617

634618
private
635619

620+
def unbundle_env(env)
621+
if env.key?("BUNDLER_ORIG_MANPATH")
622+
env["MANPATH"] = env["BUNDLER_ORIG_MANPATH"]
623+
end
624+
625+
env.delete_if {|k, _| k[0, 7] == "BUNDLE_" }
626+
env.delete("BUNDLER_SETUP")
627+
628+
if env.key?("RUBYOPT")
629+
rubyopt = env["RUBYOPT"].split(" ")
630+
rubyopt.delete("-r#{File.expand_path("bundler/setup", __dir__)}")
631+
rubyopt.delete("-rbundler/setup")
632+
env["RUBYOPT"] = rubyopt.join(" ")
633+
end
634+
635+
if env.key?("RUBYLIB")
636+
rubylib = env["RUBYLIB"].split(File::PATH_SEPARATOR)
637+
rubylib.delete(__dir__)
638+
env["RUBYLIB"] = rubylib.join(File::PATH_SEPARATOR)
639+
end
640+
641+
env
642+
end
643+
636644
def load_marshal(data, marshal_proc: nil)
637645
Marshal.load(data, marshal_proc)
638646
rescue TypeError => e

bundler/lib/bundler/inline.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def gemfile(install = false, options = {}, &gemfile)
3939
Bundler.ui = ui
4040
raise ArgumentError, "Unknown options: #{opts.keys.join(", ")}" unless opts.empty?
4141

42-
Bundler.with_unbundled_env do
42+
old_gemfile = ENV["BUNDLE_GEMFILE"]
43+
44+
Bundler.unbundle_env!
45+
46+
begin
4347
Bundler.instance_variable_set(:@bundle_path, Pathname.new(Gem.dir))
4448
Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", "Gemfile"
4549

@@ -80,9 +84,11 @@ def gemfile(install = false, options = {}, &gemfile)
8084

8185
runtime.require
8286
end
83-
end
84-
85-
if ENV["BUNDLE_GEMFILE"].nil?
86-
ENV["BUNDLE_GEMFILE"] = ""
87+
ensure
88+
if old_gemfile
89+
ENV["BUNDLE_GEMFILE"] = old_gemfile
90+
else
91+
ENV["BUNDLE_GEMFILE"] = ""
92+
end
8793
end
8894
end

bundler/spec/runtime/with_unbundled_env_spec.rb renamed to bundler/spec/runtime/env_helpers_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
RSpec.describe "Bundler.with_env helpers" do
3+
RSpec.describe "env helpers" do
44
def bundle_exec_ruby(args, options = {})
55
build_bundler_context options.dup
66
bundle "exec '#{Gem.ruby}' #{args}", options
@@ -103,6 +103,15 @@ def run_bundler_script(env, script)
103103
expect(last_command.stdboth).not_to include("-rbundler/setup")
104104
end
105105

106+
it "should delete BUNDLER_SETUP even if it was present in original env" do
107+
create_file("source.rb", <<-RUBY)
108+
print #{modified_env}.has_key?('BUNDLER_SETUP')
109+
RUBY
110+
ENV["BUNDLER_ORIG_BUNDLER_SETUP"] = system_gem_path("gems/bundler-#{Bundler::VERSION}/lib/bundler/setup").to_s
111+
bundle_exec_ruby bundled_app("source.rb")
112+
expect(last_command.stdboth).to include "false"
113+
end
114+
106115
it "should restore RUBYLIB", :ruby_repo do
107116
create_file("source.rb", <<-RUBY)
108117
print #{modified_env}['RUBYLIB']

bundler/spec/runtime/inline_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,22 @@ def confirm(msg, newline = nil)
670670
expect(out).to be_empty
671671
end
672672

673+
it "does not reset ENV" do
674+
script <<-RUBY
675+
require 'bundler/inline'
676+
677+
gemfile do
678+
source "https://gem.repo1"
679+
680+
ENV['FOO'] = 'bar'
681+
end
682+
683+
puts ENV['FOO']
684+
RUBY
685+
686+
expect(out).to eq("bar")
687+
end
688+
673689
it "does not load specified version of psych and stringio", :ruby_repo do
674690
build_repo4 do
675691
build_gem "psych", "999"

0 commit comments

Comments
 (0)