From feaab3e01ecb8b79aef2ae071435837601db850a Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 7 Nov 2024 14:17:24 -0500 Subject: [PATCH] Start returning composed bundle environment from initialize request --- exe/ruby-lsp-launcher | 3 --- lib/ruby_lsp/internal.rb | 1 + lib/ruby_lsp/server.rb | 8 ++++++++ project-words | 1 + test/server_test.rb | 23 +++++++++++++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/exe/ruby-lsp-launcher b/exe/ruby-lsp-launcher index 44b915421..a89095c71 100755 --- a/exe/ruby-lsp-launcher +++ b/exe/ruby-lsp-launcher @@ -74,9 +74,6 @@ rescue StandardError => e # If Bundler.setup fails, we need to restore the original $LOAD_PATH so that we can still require the Ruby LSP server # in degraded mode $LOAD_PATH.unshift(File.expand_path("../lib", __dir__)) -ensure - require "fileutils" - FileUtils.rm(bundle_env_path) if File.exist?(bundle_env_path) end error_path = File.join(".ruby-lsp", "install_error") diff --git a/lib/ruby_lsp/internal.rb b/lib/ruby_lsp/internal.rb index 78823fcfe..540e93f08 100644 --- a/lib/ruby_lsp/internal.rb +++ b/lib/ruby_lsp/internal.rb @@ -21,6 +21,7 @@ require "prism/visitor" require "language_server-protocol" require "rbs" +require "fileutils" require "ruby-lsp" require "ruby_lsp/base_server" diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index d0c24451b..5f939190c 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -216,6 +216,13 @@ def run_initialize(message) Hash.new(true) end + bundle_env_path = File.join(".ruby-lsp", "bundle_env") + bundle_env = if File.exist?(bundle_env_path) + env = File.readlines(bundle_env_path).to_h { |line| T.cast(line.chomp.split("=", 2), [String, String]) } + FileUtils.rm(bundle_env_path) + env + end + document_symbol_provider = Requests::DocumentSymbol.provider if enabled_features["documentSymbols"] document_link_provider = Requests::DocumentLink.provider if enabled_features["documentLink"] code_lens_provider = Requests::CodeLens.provider if enabled_features["codeLens"] @@ -269,6 +276,7 @@ def run_initialize(message) }, formatter: @global_state.formatter, degraded_mode: !!(@install_error || @setup_error), + bundle_env: bundle_env, } send_message(Result.new(id: message[:id], response: response)) diff --git a/project-words b/project-words index 5f182c097..47ceaf33c 100644 --- a/project-words +++ b/project-words @@ -58,6 +58,7 @@ quxx quux qorge rdbg +readlines realpath reparsing requireds diff --git a/test/server_test.rb b/test/server_test.rb index 44e469624..bbeed3e0b 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -33,6 +33,29 @@ def test_initialize_enabled_features_with_array assert_includes(capabilities, "semanticTokensProvider") end + def test_initialize_returns_bundle_env + bundle_env_path = File.join(".ruby-lsp", "bundle_env") + FileUtils.mkdir(".ruby-lsp") unless File.exist?(".ruby-lsp") + File.write(bundle_env_path, "BUNDLE_PATH=vendor/bundle") + @server.process_message({ + id: 1, + method: "initialize", + params: { + initializationOptions: { enabledFeatures: [] }, + capabilities: { general: { positionEncodings: ["utf-8"] } }, + }, + }) + + result = find_message(RubyLsp::Result, id: 1) + hash = JSON.parse(result.response.to_json) + + begin + assert_equal({ "BUNDLE_PATH" => "vendor/bundle" }, hash["bundle_env"]) + ensure + FileUtils.rm(bundle_env_path) if File.exist?(bundle_env_path) + end + end + def test_initialize_enabled_features_with_hash capture_subprocess_io do @server.process_message({