From 6b2b3a745dfa713a8dd7e7ee45a8d0574fb16985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 27 Sep 2018 22:17:36 -0300 Subject: Make relativeness of config a feature flag --- lib/bundler.rb | 8 +++++++- lib/bundler/cli.rb | 12 ++++++++---- lib/bundler/feature_flag.rb | 11 +++++++++++ lib/bundler/shared_helpers.rb | 6 +++++- spec/commands/config_spec.rb | 20 ++++++++++++++++---- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/bundler.rb b/lib/bundler.rb index 9cf6924bda..c264334adb 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -286,7 +286,13 @@ module Bundler end end - current_config_root.join(app_config || ".bundle") + config_root = if Bundler.feature_flag.config_relative_to_cwd? + current_config_root + else + app_config_root + end + + config_root.join(app_config || ".bundle") end def app_cache(custom_path = nil) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 6f7ff90dc2..aef434f569 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -54,10 +54,14 @@ module Bundler def initialize(*args) super - custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile] - if custom_gemfile && !custom_gemfile.empty? - Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile) - Bundler.reset_paths! + if Bundler.feature_flag.config_relative_to_cwd? + Bundler.settings.set_command_option_if_given :gemfile, options[:gemfile] + else + custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile] + if custom_gemfile && !custom_gemfile.empty? + Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile) + Bundler.reset_paths! + end end Bundler.settings.set_command_option_if_given :retry, options[:retry] diff --git a/lib/bundler/feature_flag.rb b/lib/bundler/feature_flag.rb index 01739ec4aa..0e4a0e1217 100644 --- a/lib/bundler/feature_flag.rb +++ b/lib/bundler/feature_flag.rb @@ -25,6 +25,15 @@ module Bundler end private_class_method :settings_method + def self.env_flag(name, &default) + define_method(:"#{name}?") do + value = ENV["BUNDLE_#{name.to_s.upcase}"] + value = instance_eval(&default) if value.nil? + value + end + end + private_class_method :env_flag + (1..10).each {|v| define_method("bundler_#{v}_mode?") { major_version >= v } } settings_flag(:allow_bundler_dependency_conflicts) { bundler_3_mode? } @@ -51,6 +60,8 @@ module Bundler settings_option(:default_cli_command) { bundler_3_mode? ? :cli_help : :install } + env_flag(:config_relative_to_cwd) { bundler_3_mode? } + def initialize(bundler_version) @bundler_version = Gem::Version.create(bundler_version) end diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index e8f9bfd610..960a935cd3 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -238,7 +238,11 @@ module Bundler def find_gemfile require_relative "../bundler" - given = Bundler.settings[:gemfile] + given = if Bundler.feature_flag.config_relative_to_cwd? + Bundler.settings[:gemfile] + else + ENV["BUNDLE_GEMFILE"] + end return given if given && !given.empty? find_file(*gemfile_names) end diff --git a/spec/commands/config_spec.rb b/spec/commands/config_spec.rb index 11fdd1ac78..26e75bbfd6 100644 --- a/spec/commands/config_spec.rb +++ b/spec/commands/config_spec.rb @@ -53,7 +53,19 @@ RSpec.describe ".bundle/config" do expect(the_bundle).to include_gems "rack 1.0.0" end - it "can provide a relative path with the environment variable" do + it "can provide a relative path with the environment variable, and generates config relative to the Gemfile", :bundler => "< 3" do + FileUtils.mkdir_p bundled_app("omg") + + ENV["BUNDLE_APP_CONFIG"] = "../foo" + bundle "config set --local path vendor/bundle", :dir => bundled_app("omg") + bundle "install", :dir => bundled_app("omg") + + expect(bundled_app(".bundle")).not_to exist + expect(bundled_app("../foo/config")).to exist + expect(the_bundle).to include_gems "rack 1.0.0" + end + + it "can provide a relative path with the environment variable, and generates config relative to the cwd", :bundler => "3" do FileUtils.mkdir_p bundled_app("omg") ENV["BUNDLE_APP_CONFIG"] = "../foo" @@ -65,7 +77,7 @@ RSpec.describe ".bundle/config" do expect(the_bundle).to include_gems "rack 1.0.0", :dir => bundled_app("omg") end - it "is relative to the pwd and not to the gemfile" do + it "is relative to the pwd and not to the gemfile", :bundler => "3" do FileUtils.mkdir_p bundled_app("omg/gmo") gemfile bundled_app("omg/gmo/AnotherGemfile"), <<-G @@ -78,7 +90,7 @@ RSpec.describe ".bundle/config" do expect(bundled_app("omg/.bundle")).to exist end - it "uses the first existing local config from the pwd and not from the gemfile" do + it "uses the first existing local config from the pwd and not from the gemfile", :bundler => "3" do bundle "install" FileUtils.mkdir_p bundled_app("omg/gmo") @@ -534,7 +546,7 @@ RSpec.describe "setting gemfile via config" do expect(out).to include("NotGemfile") end - it "gets used when requiring bundler/setup" do + it "gets used when requiring bundler/setup", :bundler => "3" do bundle :install code = "puts $LOAD_PATH.count {|path| path =~ /rack/} == 1" -- cgit v1.2.1