From c485b9f1bcdb5dd8e5510d143a7ec963f321f2c2 Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <jen-shin@gitlab.com>
Date: Thu, 2 Sep 2021 19:42:14 +0800
Subject: [PATCH] Make sure we respect bundler config for Gemfile

---
 bin/bundle               | 7 ++++++-
 bin/rspec                | 2 +-
 bin/rspec-stackprof      | 2 +-
 config/boot.rb           | 5 ++++-
 config/bundler_setup.rb  | 5 +++++
 ee/bin/geo_log_cursor    | 3 +--
 qa/Dockerfile            | 1 +
 qa/qa.rb                 | 2 +-
 scripts/setup-test-env   | 2 +-
 spec/fast_spec_helper.rb | 2 +-
 10 files changed, 22 insertions(+), 9 deletions(-)
 create mode 100644 config/bundler_setup.rb

diff --git a/bin/bundle b/bin/bundle
index f19acf5b5cc6e..eed4bb7738a94 100755
--- a/bin/bundle
+++ b/bin/bundle
@@ -1,3 +1,8 @@
 #!/usr/bin/env ruby
-ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
+
+require 'bundler'
+
+ENV['BUNDLE_GEMFILE'] ||=
+  Bundler.settings[:gemfile] || File.expand_path('../Gemfile', __dir__)
+
 load Gem.bin_path('bundler', 'bundle')
diff --git a/bin/rspec b/bin/rspec
index 4236753c9c143..5aebc7336fed6 100755
--- a/bin/rspec
+++ b/bin/rspec
@@ -5,5 +5,5 @@ begin
 rescue LoadError => e
   raise unless e.message.include?('spring')
 end
-require 'bundler/setup'
+require_relative '../config/bundler_setup'
 load Gem.bin_path('rspec-core', 'rspec')
diff --git a/bin/rspec-stackprof b/bin/rspec-stackprof
index 3bef45c607c2e..018bfe7da4b4e 100755
--- a/bin/rspec-stackprof
+++ b/bin/rspec-stackprof
@@ -1,6 +1,6 @@
 #!/usr/bin/env ruby
 
-require 'bundler/setup'
+require_relative '../config/bundler_setup'
 require 'stackprof'
 $:.unshift 'spec'
 require 'spec_helper'
diff --git a/config/boot.rb b/config/boot.rb
index 41bf7953737ab..2b0c203d6b5fa 100644
--- a/config/boot.rb
+++ b/config/boot.rb
@@ -1,6 +1,9 @@
 # frozen_string_literal: true
 
-ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
+require 'bundler'
+
+ENV['BUNDLE_GEMFILE'] ||=
+  Bundler.settings[:gemfile] || File.expand_path('../Gemfile', __dir__)
 
 # Set up gems listed in the Gemfile.
 require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
diff --git a/config/bundler_setup.rb b/config/bundler_setup.rb
new file mode 100644
index 0000000000000..ff62ec7e4e4f3
--- /dev/null
+++ b/config/bundler_setup.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+require 'bundler'
+ENV['BUNDLE_GEMFILE'] ||= Bundler.settings[:gemfile]
+require 'bundler/setup'
diff --git a/ee/bin/geo_log_cursor b/ee/bin/geo_log_cursor
index b490436867d62..dcafd96395372 100755
--- a/ee/bin/geo_log_cursor
+++ b/ee/bin/geo_log_cursor
@@ -1,8 +1,7 @@
 #!/usr/bin/env ruby
 # frozen_string_literal: true
 
-require 'rubygems'
-require 'bundler/setup'
+require_relative '../../config/bundler_setup'
 require 'optparse'
 
 def rails_path(relname)
diff --git a/qa/Dockerfile b/qa/Dockerfile
index ad4bffb3bf29c..13213c7c8c8b7 100644
--- a/qa/Dockerfile
+++ b/qa/Dockerfile
@@ -76,6 +76,7 @@ COPY ./config/initializers/0_inject_enterprise_edition_module.rb /home/gitlab/co
 # Copy VERSION to ensure the COPY succeeds to copy at least one file since ee/app/models/license.rb isn't present in FOSS
 # The [b] part makes ./ee/app/models/license.r[b] a pattern that is allowed to return no files (which is the case in FOSS)
 COPY VERSION ./ee/app/models/license.r[b] /home/gitlab/ee/app/models/
+COPY ./config/bundler_setup.rb /home/gitlab/config/
 COPY ./lib/gitlab.rb /home/gitlab/lib/
 COPY ./lib/gitlab/utils.rb /home/gitlab/lib/gitlab/
 COPY ./INSTALLATION_TYPE ./VERSION /home/gitlab/
diff --git a/qa/qa.rb b/qa/qa.rb
index 89721445f3941..4d86a29aecf71 100644
--- a/qa/qa.rb
+++ b/qa/qa.rb
@@ -8,7 +8,7 @@
 
 require_relative 'lib/gitlab'
 
-require 'bundler/setup'
+require_relative '../config/bundler_setup'
 Bundler.require(:default)
 
 module QA
diff --git a/scripts/setup-test-env b/scripts/setup-test-env
index ebd3a48ae15f6..a81aaa5cda34b 100755
--- a/scripts/setup-test-env
+++ b/scripts/setup-test-env
@@ -2,7 +2,7 @@
 
 # frozen_string_literal: true
 
-require 'bundler/setup'
+require_relative '../config/bundler_setup'
 
 require 'request_store'
 require 'rake'
diff --git a/spec/fast_spec_helper.rb b/spec/fast_spec_helper.rb
index b06ebba3f6c86..1485edcd97d79 100644
--- a/spec/fast_spec_helper.rb
+++ b/spec/fast_spec_helper.rb
@@ -7,7 +7,7 @@
   return
 end
 
-require 'bundler/setup'
+require_relative '../config/bundler_setup'
 
 ENV['GITLAB_ENV'] = 'test'
 ENV['IN_MEMORY_APPLICATION_SETTINGS'] = 'true'
-- 
GitLab