Skip to content

Commit a738d5b

Browse files
authored
Start returning composed bundle environment from initialize request (#2880)
### Motivation Currently, we are always spawning the debug client with the incorrect environment. The reason is because the composed bundle process does a lot of heavy lifting to determine the exact environment variables that need to be set to apply an accurate Bundler configuration. We need the debug client to have access to the composed environment if we want to be able to launch the debugger properly for all scenarios. ### Implementation Started reading the bundle env and returning it as part of the initialize request. ### Automated Tests Added a test to verify that we're returning the environment.
1 parent afe3705 commit a738d5b

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

exe/ruby-lsp-launcher

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ rescue StandardError => e
7474
# If Bundler.setup fails, we need to restore the original $LOAD_PATH so that we can still require the Ruby LSP server
7575
# in degraded mode
7676
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
77-
ensure
78-
require "fileutils"
79-
FileUtils.rm(bundle_env_path) if File.exist?(bundle_env_path)
8077
end
8178

8279
error_path = File.join(".ruby-lsp", "install_error")

lib/ruby_lsp/internal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require "prism/visitor"
2222
require "language_server-protocol"
2323
require "rbs"
24+
require "fileutils"
2425

2526
require "ruby-lsp"
2627
require "ruby_lsp/base_server"

lib/ruby_lsp/server.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ def run_initialize(message)
216216
Hash.new(true)
217217
end
218218

219+
bundle_env_path = File.join(".ruby-lsp", "bundle_env")
220+
bundle_env = if File.exist?(bundle_env_path)
221+
env = File.readlines(bundle_env_path).to_h { |line| T.cast(line.chomp.split("=", 2), [String, String]) }
222+
FileUtils.rm(bundle_env_path)
223+
env
224+
end
225+
219226
document_symbol_provider = Requests::DocumentSymbol.provider if enabled_features["documentSymbols"]
220227
document_link_provider = Requests::DocumentLink.provider if enabled_features["documentLink"]
221228
code_lens_provider = Requests::CodeLens.provider if enabled_features["codeLens"]
@@ -269,6 +276,7 @@ def run_initialize(message)
269276
},
270277
formatter: @global_state.formatter,
271278
degraded_mode: !!(@install_error || @setup_error),
279+
bundle_env: bundle_env,
272280
}
273281

274282
send_message(Result.new(id: message[:id], response: response))

project-words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ quxx
5858
quux
5959
qorge
6060
rdbg
61+
readlines
6162
realpath
6263
reparsing
6364
requireds

test/server_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ def test_initialize_enabled_features_with_array
3333
assert_includes(capabilities, "semanticTokensProvider")
3434
end
3535

36+
def test_initialize_returns_bundle_env
37+
bundle_env_path = File.join(".ruby-lsp", "bundle_env")
38+
FileUtils.mkdir(".ruby-lsp") unless File.exist?(".ruby-lsp")
39+
File.write(bundle_env_path, "BUNDLE_PATH=vendor/bundle")
40+
@server.process_message({
41+
id: 1,
42+
method: "initialize",
43+
params: {
44+
initializationOptions: { enabledFeatures: [] },
45+
capabilities: { general: { positionEncodings: ["utf-8"] } },
46+
},
47+
})
48+
49+
result = find_message(RubyLsp::Result, id: 1)
50+
hash = JSON.parse(result.response.to_json)
51+
52+
begin
53+
assert_equal({ "BUNDLE_PATH" => "vendor/bundle" }, hash["bundle_env"])
54+
ensure
55+
FileUtils.rm(bundle_env_path) if File.exist?(bundle_env_path)
56+
end
57+
end
58+
3659
def test_initialize_enabled_features_with_hash
3760
capture_subprocess_io do
3861
@server.process_message({

0 commit comments

Comments
 (0)