Skip to content

Commit c3159eb

Browse files
authored
Merge pull request #841 from Shopify/vs/use_environment_for_system_calls
Pass environment as a hash to system or exec calls
2 parents 0598ac5 + d94a7be commit c3159eb

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

exe/ruby-lsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ if ENV["BUNDLE_GEMFILE"].nil? && File.exist?("Gemfile.lock")
2525
# SetupBundler to install the gems
2626
path = Bundler.settings["path"]
2727

28-
command = +"BUNDLE_GEMFILE=#{bundle_gemfile} bundle exec ruby-lsp #{ARGV.join(" ")}"
29-
command.prepend("BUNDLE_PATH=#{File.expand_path(path, Dir.pwd)} ") if path
30-
exit exec(command)
28+
env = { "BUNDLE_GEMFILE" => bundle_gemfile }
29+
env["BUNDLE_PATH"] = File.expand_path(path, Dir.pwd) if path
30+
exit exec(env, "bundle exec ruby-lsp #{ARGV.join(" ")}")
3131
end
3232

3333
require "sorbet-runtime"

lib/ruby_lsp/setup_bundler.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ def run_bundle_install(bundle_gemfile = nil)
120120
# want to install it in the top level `vendor` and not `.ruby-lsp/vendor`
121121
path = Bundler.settings["path"]
122122

123-
command = +""
124123
# Use the absolute `BUNDLE_PATH` to prevent accidentally creating unwanted folders under `.ruby-lsp`
125-
command << "BUNDLE_PATH=#{File.expand_path(path, Dir.pwd)} " if path
126-
command << "BUNDLE_GEMFILE=#{bundle_gemfile} " if bundle_gemfile
124+
env = {}
125+
env["BUNDLE_GEMFILE"] = bundle_gemfile if bundle_gemfile
126+
env["BUNDLE_PATH"] = File.expand_path(path, Dir.pwd) if path
127127

128128
# If both `ruby-lsp` and `debug` are already in the Gemfile, then we shouldn't try to upgrade them or else we'll
129129
# produce undesired source control changes. If the custom bundle was just created and either `ruby-lsp` or `debug`
130130
# weren't a part of the Gemfile, then we need to run `bundle install` for the first time to generate the
131131
# Gemfile.lock with them included or else Bundler will complain that they're missing. We can only update if the
132132
# custom `.ruby-lsp/Gemfile.lock` already exists and includes both gems
133+
command = +""
134+
133135
if (@dependencies["ruby-lsp"] && @dependencies["debug"]) ||
134136
@custom_bundle_dependencies["ruby-lsp"].nil? || @custom_bundle_dependencies["debug"].nil?
135137
# Install gems using the custom bundle
@@ -147,7 +149,7 @@ def run_bundle_install(bundle_gemfile = nil)
147149

148150
# Add bundle update
149151
warn("Ruby LSP> Running bundle install for the custom bundle. This may take a while...")
150-
system(command)
152+
system(env, command)
151153
end
152154
end
153155
end

test/setup_bundler_test.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66

77
class SetupBundlerTest < Minitest::Test
88
def test_does_nothing_when_running_in_the_ruby_lsp
9-
Object.any_instance.expects(:system).with(bundle_install_command)
9+
Object.any_instance.expects(:system).with(bundle_env, "bundle install 1>&2")
1010
run_script("/some/path/ruby-lsp")
1111
refute_path_exists(".ruby-lsp")
1212
end
1313

1414
def test_does_nothing_if_both_ruby_lsp_and_debug_are_in_the_bundle
15-
Object.any_instance.expects(:system).with(bundle_install_command)
15+
Object.any_instance.expects(:system).with(bundle_env, "bundle install 1>&2")
1616
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "ruby-lsp" => true, "debug" => true })
1717
run_script
1818
refute_path_exists(".ruby-lsp")
1919
end
2020

2121
def test_removes_ruby_lsp_folder_if_both_gems_were_added_to_the_bundle
22-
Object.any_instance.expects(:system).with(bundle_install_command)
22+
Object.any_instance.expects(:system).with(bundle_env, "bundle install 1>&2")
2323
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "ruby-lsp" => true, "debug" => true })
2424
FileUtils.mkdir(".ruby-lsp")
2525
run_script
@@ -29,7 +29,7 @@ def test_removes_ruby_lsp_folder_if_both_gems_were_added_to_the_bundle
2929
end
3030

3131
def test_creates_custom_bundle
32-
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
32+
Object.any_instance.expects(:system).with(bundle_env(".ruby-lsp/Gemfile"), "bundle install 1>&2")
3333
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({})
3434
run_script
3535

@@ -43,7 +43,7 @@ def test_creates_custom_bundle
4343
end
4444

4545
def test_copies_gemfile_lock_when_modified
46-
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
46+
Object.any_instance.expects(:system).with(bundle_env(".ruby-lsp/Gemfile"), "bundle install 1>&2")
4747
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({}).twice
4848
FileUtils.mkdir(".ruby-lsp")
4949
FileUtils.touch(".ruby-lsp/Gemfile.lock")
@@ -59,7 +59,7 @@ def test_copies_gemfile_lock_when_modified
5959
end
6060

6161
def test_does_not_copy_gemfile_lock_when_not_modified
62-
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
62+
Object.any_instance.expects(:system).with(bundle_env(".ruby-lsp/Gemfile"), "bundle install 1>&2")
6363
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({}).twice
6464
FileUtils.mkdir(".ruby-lsp")
6565
FileUtils.cp("Gemfile.lock", ".ruby-lsp/Gemfile.lock")
@@ -71,7 +71,7 @@ def test_does_not_copy_gemfile_lock_when_not_modified
7171

7272
def test_uses_absolute_bundle_path_for_bundle_install
7373
Bundler.settings.set_global("path", "vendor/bundle")
74-
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
74+
Object.any_instance.expects(:system).with(bundle_env(".ruby-lsp/Gemfile"), "bundle install 1>&2")
7575
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({})
7676
run_script
7777
ensure
@@ -88,13 +88,12 @@ def run_script(path = "/fake/project/path")
8888
RubyLsp::SetupBundler.new(path).setup!
8989
end
9090

91-
def bundle_install_command(bundle_gemfile = nil)
91+
def bundle_env(bundle_gemfile = nil)
9292
path = Bundler.settings["path"]
9393

94-
command = +""
95-
command << "BUNDLE_PATH=#{File.expand_path(path, Dir.pwd)} " if path
96-
command << "BUNDLE_GEMFILE=#{bundle_gemfile} " if bundle_gemfile
97-
command << "bundle install "
98-
command << "1>&2"
94+
env = {}
95+
env["BUNDLE_PATH"] = File.expand_path(path, Dir.pwd) if path
96+
env["BUNDLE_GEMFILE"] = bundle_gemfile if bundle_gemfile
97+
env
9998
end
10099
end

0 commit comments

Comments
 (0)