Skip to content

Commit

Permalink
Respect local bundler configuration (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Oct 13, 2023
1 parent 212d8dd commit 4fb4953
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
3 changes: 2 additions & 1 deletion exe/ruby-lsp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ 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)
end

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

Expand Down
13 changes: 8 additions & 5 deletions lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
23 changes: 23 additions & 0 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 4fb4953

Please sign in to comment.