Skip to content

Commit

Permalink
Make sure .ruby-lsp directory always exists (#2747)
Browse files Browse the repository at this point in the history
Make sure .ruby-lsp directory always exists

We will need the directory to inform the main
process about the Gemfile path to setup Bundler
with
  • Loading branch information
vinistock authored Oct 22, 2024
1 parent dd376e9 commit f6d2ae3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
15 changes: 5 additions & 10 deletions lib/ruby_lsp/setup_bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def initialize(project_path, **options)
def setup!
raise BundleNotLocked if @gemfile&.exist? && !@lockfile&.exist?

# Automatically create and ignore the .ruby-lsp folder for users
@custom_dir.mkpath unless @custom_dir.exist?
ignore_file = @custom_dir + ".gitignore"
ignore_file.write("*") unless ignore_file.exist?

# Do not set up a custom bundle if LSP dependencies are already in the Gemfile
if @dependencies["ruby-lsp"] &&
@dependencies["debug"] &&
Expand All @@ -69,19 +74,9 @@ def setup!
"Ruby LSP> Skipping custom bundle setup since LSP dependencies are already in #{@gemfile}",
)

# If the user decided to add `ruby-lsp` and `debug` (and potentially `ruby-lsp-rails`) to their Gemfile after
# having already run the Ruby LSP, then we need to remove the `.ruby-lsp` folder, otherwise we will run `bundle
# install` for the top level and try to execute the Ruby LSP using the custom bundle, which will fail since the
# gems are not installed there
@custom_dir.rmtree if @custom_dir.exist?
return run_bundle_install
end

# Automatically create and ignore the .ruby-lsp folder for users
@custom_dir.mkpath unless @custom_dir.exist?
ignore_file = @custom_dir + ".gitignore"
ignore_file.write("*") unless ignore_file.exist?

write_custom_gemfile

unless @gemfile&.exist? && @lockfile&.exist?
Expand Down
33 changes: 6 additions & 27 deletions test/setup_bundler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
require "ruby_lsp/setup_bundler"

class SetupBundlerTest < Minitest::Test
def test_does_nothing_if_both_ruby_lsp_and_debug_are_in_the_bundle
def test_does_not_create_composed_gemfile_if_ruby_lsp_and_debug_are_in_the_bundle
stub_bundle_with_env(bundle_env)
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "ruby-lsp" => true, "debug" => true })
run_script
refute_path_exists(".ruby-lsp")
refute_path_exists(".ruby-lsp/Gemfile")
end

def test_does_nothing_if_both_ruby_lsp_and_debug_are_in_the_bundle2
def test_does_not_create_composed_gemfile_if_all_gems_are_in_the_bundle_for_rails_apps
stub_bundle_with_env(bundle_env)
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({
"ruby-lsp" => true,
Expand All @@ -21,28 +21,7 @@ def test_does_nothing_if_both_ruby_lsp_and_debug_are_in_the_bundle2
"debug" => true,
})
run_script
refute_path_exists(".ruby-lsp")
end

def test_removes_ruby_lsp_folder_if_both_gems_were_added_to_the_bundle
stub_bundle_with_env(bundle_env)
Bundler::LockfileParser.any_instance.expects(:dependencies).returns({ "ruby-lsp" => true, "debug" => true })
FileUtils.mkdir(".ruby-lsp")
run_script
refute_path_exists(".ruby-lsp")
ensure
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
end

def test_in_a_rails_app_removes_ruby_lsp_folder_if_all_gems_were_added_to_the_bundle
stub_bundle_with_env(bundle_env)
Bundler::LockfileParser.any_instance.expects(:dependencies)
.returns({ "ruby-lsp" => true, "ruby-lsp-rails" => true, "debug" => true })
FileUtils.mkdir(".ruby-lsp")
run_script
refute_path_exists(".ruby-lsp")
ensure
FileUtils.rm_r(".ruby-lsp") if Dir.exist?(".ruby-lsp")
refute_path_exists(".ruby-lsp/Gemfile")
end

def test_creates_custom_bundle
Expand Down Expand Up @@ -241,7 +220,7 @@ def test_raises_if_bundle_is_not_locked
end
end

def test_does_nothing_if_both_ruby_lsp_and_debug_are_gemspec_dependencies
def test_does_not_create_composed_gemfile_if_both_ruby_lsp_and_debug_are_gemspec_dependencies
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
# Write a fake Gemfile and gemspec
Expand Down Expand Up @@ -279,7 +258,7 @@ def test_does_nothing_if_both_ruby_lsp_and_debug_are_gemspec_dependencies
run_script(dir)
end

refute_path_exists(".ruby-lsp")
refute_path_exists(".ruby-lsp/Gemfile")
end
end
end
Expand Down

0 comments on commit f6d2ae3

Please sign in to comment.