From 4fb4953debc14a9f645bc9d1867b507a6691f365 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Fri, 13 Oct 2023 11:59:07 -0400 Subject: [PATCH] Respect local bundler configuration (#1093) --- exe/ruby-lsp | 3 ++- lib/ruby_lsp/setup_bundler.rb | 13 ++++++++----- test/setup_bundler_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/exe/ruby-lsp b/exe/ruby-lsp index f949c853c..2cd076266 100755 --- a/exe/ruby-lsp +++ b/exe/ruby-lsp @@ -49,7 +49,7 @@ if ENV["BUNDLE_GEMFILE"].nil? require_relative "../lib/ruby_lsp/setup_bundler" begin - bundle_gemfile, bundle_path = RubyLsp::SetupBundler.new(Dir.pwd, branch: options[:branch]).setup! + bundle_gemfile, bundle_path, bundle_app_config = RubyLsp::SetupBundler.new(Dir.pwd, branch: options[:branch]).setup! rescue RubyLsp::SetupBundler::BundleNotLocked warn("Project contains a Gemfile, but no Gemfile.lock. Run `bundle install` to lock gems and restart the server") exit(78) @@ -57,6 +57,7 @@ if ENV["BUNDLE_GEMFILE"].nil? env = { "BUNDLE_GEMFILE" => bundle_gemfile } env["BUNDLE_PATH"] = bundle_path if bundle_path + env["BUNDLE_APP_CONFIG"] = bundle_app_config if bundle_app_config exit exec(env, "bundle exec ruby-lsp #{original_args.join(" ")}") end diff --git a/lib/ruby_lsp/setup_bundler.rb b/lib/ruby_lsp/setup_bundler.rb index 51b68ec67..1f582f237 100644 --- a/lib/ruby_lsp/setup_bundler.rb +++ b/lib/ruby_lsp/setup_bundler.rb @@ -46,9 +46,9 @@ def initialize(project_path, branch: nil) @dependencies = T.let(load_dependencies, T::Hash[String, T.untyped]) end - # Setups up the custom bundle and returns the `BUNDLE_GEMFILE` and `BUNDLE_PATH` that should be used for running the - # server - sig { returns([String, T.nilable(String)]) } + # Sets up the custom bundle and returns the `BUNDLE_GEMFILE`, `BUNDLE_PATH` and `BUNDLE_APP_CONFIG` that should be + # used for running the server + sig { returns([String, T.nilable(String), T.nilable(String)]) } def setup! raise BundleNotLocked if @gemfile&.exist? && !@lockfile&.exist? @@ -156,7 +156,7 @@ def load_dependencies dependencies end - sig { params(bundle_gemfile: T.nilable(Pathname)).returns([String, T.nilable(String)]) } + sig { params(bundle_gemfile: T.nilable(Pathname)).returns([String, T.nilable(String), T.nilable(String)]) } def run_bundle_install(bundle_gemfile = @gemfile) # If the user has a custom bundle path configured, we need to ensure that we will use the absolute and not # relative version of it when running `bundle install`. This is necessary to avoid installing the gems under the @@ -170,6 +170,9 @@ def run_bundle_install(bundle_gemfile = @gemfile) env["BUNDLE_GEMFILE"] = bundle_gemfile.to_s env["BUNDLE_PATH"] = expanded_path if expanded_path + local_config_path = File.join(Dir.pwd, ".bundle") + env["BUNDLE_APP_CONFIG"] = local_config_path if Dir.exist?(local_config_path) + # If both `ruby-lsp` and `debug` are already in the Gemfile, then we shouldn't try to upgrade them or else we'll # produce undesired source control changes. If the custom bundle was just created and either `ruby-lsp` or `debug` # weren't a part of the Gemfile, then we need to run `bundle install` for the first time to generate the @@ -200,7 +203,7 @@ def run_bundle_install(bundle_gemfile = @gemfile) # Add bundle update warn("Ruby LSP> Running bundle install for the custom bundle. This may take a while...") system(env, command) - [bundle_gemfile.to_s, expanded_path] + [bundle_gemfile.to_s, expanded_path, env["BUNDLE_APP_CONFIG"]] end sig { returns(T::Boolean) } diff --git a/test/setup_bundler_test.rb b/test/setup_bundler_test.rb index 3046e55b5..256cb151a 100644 --- a/test/setup_bundler_test.rb +++ b/test/setup_bundler_test.rb @@ -270,6 +270,26 @@ def test_creates_custom_bundle_with_specified_branch end end + def test_returns_bundle_app_config_if_there_is_local_config + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + bundle_gemfile = Pathname.new(".ruby-lsp").expand_path(Dir.pwd) + "Gemfile" + Bundler.with_unbundled_env do + Bundler.settings.set_local("without", "production") + Object.any_instance.expects(:system).with( + bundle_env(bundle_gemfile.to_s), + "(bundle check || bundle install) 1>&2", + ) + + run_script(branch: "test-branch") + end + end + end + ensure + # CI uses a local bundle config and we don't want to delete that + FileUtils.rm_r(File.join(Dir.pwd, ".bundle")) unless ENV["CI"] + end + private # This method runs the script and then immediately unloads it. This allows us to make assertions against the effects @@ -293,6 +313,9 @@ def bundle_env(bundle_gemfile = "Gemfile") env["BUNDLE_PATH"] = File.expand_path(path, Dir.pwd) if path env["BUNDLE_GEMFILE"] = bundle_gemfile_path.absolute? ? bundle_gemfile_path.to_s : bundle_gemfile_path.expand_path(Dir.pwd).to_s + + local_config_path = File.join(Dir.pwd, ".bundle") + env["BUNDLE_APP_CONFIG"] = local_config_path if Dir.exist?(local_config_path) env end end