Skip to content

Commit ff6e7ed

Browse files
authored
Merge pull request #822 from Shopify/vs/remove_old_ruby_lsp_folder
Remove old .ruby-lsp folder if `ruby-lsp` and `debug` are in the Gemfile
2 parents 2c2d029 + 375dce1 commit ff6e7ed

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-lsp (0.7.2)
4+
ruby-lsp (0.7.3)
55
language_server-protocol (~> 3.17.0)
66
sorbet-runtime
77
syntax_tree (>= 6.1.1, < 7)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.2
1+
0.7.3

lib/ruby_lsp/setup_bundler.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class SetupBundler
1717
def initialize(project_path)
1818
@project_path = project_path
1919
@dependencies = T.let(load_dependencies, T::Hash[String, T.untyped])
20+
@custom_bundle_dependencies = T.let(
21+
if File.exist?(".ruby-lsp/Gemfile.lock")
22+
Bundler::LockfileParser.new(Bundler.read_file(".ruby-lsp/Gemfile.lock")).dependencies
23+
else
24+
{}
25+
end,
26+
T::Hash[String, T.untyped],
27+
)
2028
end
2129

2230
sig { void }
@@ -31,6 +39,11 @@ def setup!
3139
# Do not setup a custom bundle if both `ruby-lsp` and `debug` are already in the Gemfile
3240
if @dependencies["ruby-lsp"] && @dependencies["debug"]
3341
warn("Ruby LSP> Skipping custom bundle setup since both `ruby-lsp` and `debug` are already in the Gemfile")
42+
43+
# If the user decided to add the `ruby-lsp` and `debug` to their Gemfile after having already run the Ruby LSP,
44+
# then we need to remove the `.ruby-lsp` folder, otherwise we will run `bundle install` for the top level and
45+
# try to execute the Ruby LSP using the custom bundle, which will fail since the gems are not installed there
46+
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
3447
run_bundle_install
3548
return
3649
end
@@ -112,7 +125,13 @@ def run_bundle_install(bundle_gemfile = nil)
112125
command << "BUNDLE_PATH=#{File.expand_path(path, Dir.pwd)} " if path
113126
command << "BUNDLE_GEMFILE=#{bundle_gemfile} " if bundle_gemfile
114127

115-
if @dependencies["ruby-lsp"] && @dependencies["debug"]
128+
# If both `ruby-lsp` and `debug` are already in the Gemfile, then we shouldn't try to upgrade them or else we'll
129+
# produce undesired source control changes. If the custom bundle was just created and either `ruby-lsp` or `debug`
130+
# weren't a part of the Gemfile, then we need to run `bundle install` for the first time to generate the
131+
# Gemfile.lock with them included or else Bundler will complain that they're missing. We can only update if the
132+
# custom `.ruby-lsp/Gemfile.lock` already exists and includes both gems
133+
if (@dependencies["ruby-lsp"] && @dependencies["debug"]) ||
134+
@custom_bundle_dependencies["ruby-lsp"].nil? || @custom_bundle_dependencies["debug"].nil?
116135
# Install gems using the custom bundle
117136
command << "bundle install "
118137
else

test/setup_bundler_test.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@
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(update: false))
9+
Object.any_instance.expects(:system).with(bundle_install_command)
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(update: false))
15+
Object.any_instance.expects(:system).with(bundle_install_command)
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

21+
def test_removes_ruby_lsp_folder_if_both_gems_were_added_to_the_bundle
22+
Object.any_instance.expects(:system).with(bundle_install_command)
23+
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "ruby-lsp" => true, "debug" => true })
24+
FileUtils.mkdir(".ruby-lsp")
25+
run_script
26+
refute_path_exists(".ruby-lsp")
27+
ensure
28+
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
29+
end
30+
2131
def test_creates_custom_bundle
2232
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
2333
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({})
@@ -34,7 +44,7 @@ def test_creates_custom_bundle
3444

3545
def test_copies_gemfile_lock_when_modified
3646
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
37-
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({})
47+
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({}).twice
3848
FileUtils.mkdir(".ruby-lsp")
3949
FileUtils.touch(".ruby-lsp/Gemfile.lock")
4050
# Wait a little bit so that the modified timestamps don't match
@@ -50,7 +60,7 @@ def test_copies_gemfile_lock_when_modified
5060

5161
def test_does_not_copy_gemfile_lock_when_not_modified
5262
Object.any_instance.expects(:system).with(bundle_install_command(".ruby-lsp/Gemfile"))
53-
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({})
63+
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({}).twice
5464
FileUtils.mkdir(".ruby-lsp")
5565
FileUtils.cp("Gemfile.lock", ".ruby-lsp/Gemfile.lock")
5666

@@ -78,17 +88,13 @@ def run_script(path = "/fake/project/path")
7888
RubyLsp::SetupBundler.new(path).setup!
7989
end
8090

81-
def bundle_install_command(bundle_gemfile = nil, update: true)
91+
def bundle_install_command(bundle_gemfile = nil)
8292
path = Bundler.settings["path"]
8393

8494
command = +""
8595
command << "BUNDLE_PATH=#{File.expand_path(path, Dir.pwd)} " if path
8696
command << "BUNDLE_GEMFILE=#{bundle_gemfile} " if bundle_gemfile
87-
command << if update
88-
"bundle update ruby-lsp debug "
89-
else
90-
"bundle install "
91-
end
97+
command << "bundle install "
9298
command << "1>&2"
9399
end
94100
end

0 commit comments

Comments
 (0)