Skip to content

Commit 604fe8a

Browse files
authored
Defer to Bundler to get gemfile and lockfile names (#1139)
1 parent 6bf97fb commit 604fe8a

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

lib/ruby_lsp/requests/code_lens.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,23 @@ class CodeLens < ExtensibleListener
2424

2525
ResponseType = type_member { { fixed: T::Array[Interface::CodeLens] } }
2626

27-
BASE_COMMAND = T.let((File.exist?("Gemfile.lock") ? "bundle exec ruby" : "ruby") + " -Itest ", String)
27+
BASE_COMMAND = T.let(
28+
begin
29+
Bundler.with_original_env { Bundler.default_lockfile }
30+
"bundle exec ruby"
31+
rescue Bundler::GemfileNotFound
32+
"ruby"
33+
end + " -Itest ",
34+
String,
35+
)
36+
GEMFILE_NAME = T.let(
37+
begin
38+
Bundler.with_original_env { Bundler.default_gemfile.basename.to_s }
39+
rescue Bundler::GemfileNotFound
40+
"Gemfile"
41+
end,
42+
String,
43+
)
2844
ACCESS_MODIFIERS = T.let([:public, :private, :protected], T::Array[Symbol])
2945
SUPPORTED_TEST_LIBRARIES = T.let(["minitest", "test-unit"], T::Array[String])
3046

@@ -111,7 +127,7 @@ def on_call_node_enter(node)
111127
return
112128
end
113129

114-
if @path&.include?("Gemfile") && name == :gem && arguments
130+
if @path&.include?(GEMFILE_NAME) && name == :gem && arguments
115131
first_argument = arguments.arguments.first
116132
return unless first_argument.is_a?(Prism::StringNode)
117133

lib/ruby_lsp/setup_bundler.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ def initialize(project_path, branch: nil)
2525
@project_path = project_path
2626
@branch = branch
2727

28-
# Custom bundle paths
29-
@custom_dir = T.let(Pathname.new(".ruby-lsp").expand_path(Dir.pwd), Pathname)
30-
@custom_gemfile = T.let(@custom_dir + "Gemfile", Pathname)
31-
@custom_lockfile = T.let(@custom_dir + "Gemfile.lock", Pathname)
32-
@lockfile_hash_path = T.let(@custom_dir + "main_lockfile_hash", Pathname)
33-
@last_updated_path = T.let(@custom_dir + "last_updated", Pathname)
34-
3528
# Regular bundle paths
3629
@gemfile = T.let(
3730
begin
@@ -43,6 +36,15 @@ def initialize(project_path, branch: nil)
4336
)
4437
@lockfile = T.let(@gemfile ? Bundler.default_lockfile : nil, T.nilable(Pathname))
4538

39+
@gemfile_name = T.let(@gemfile&.basename&.to_s || "Gemfile", String)
40+
41+
# Custom bundle paths
42+
@custom_dir = T.let(Pathname.new(".ruby-lsp").expand_path(Dir.pwd), Pathname)
43+
@custom_gemfile = T.let(@custom_dir + @gemfile_name, Pathname)
44+
@custom_lockfile = T.let(@custom_dir + (@lockfile&.basename || "Gemfile.lock"), Pathname)
45+
@lockfile_hash_path = T.let(@custom_dir + "main_lockfile_hash", Pathname)
46+
@last_updated_path = T.let(@custom_dir + "last_updated", Pathname)
47+
4648
@dependencies = T.let(load_dependencies, T::Hash[String, T.untyped])
4749
end
4850

@@ -118,7 +120,7 @@ def write_custom_gemfile
118120
# If there's a top level Gemfile, we want to evaluate from the custom bundle. We get the source from the top level
119121
# Gemfile, so if there isn't one we need to add a default source
120122
if @gemfile&.exist?
121-
parts << "eval_gemfile(File.expand_path(\"../Gemfile\", __dir__))"
123+
parts << "eval_gemfile(File.expand_path(\"../#{@gemfile_name}\", __dir__))"
122124
else
123125
parts.unshift('source "https://rubygems.org"')
124126
end

test/setup_bundler_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ def test_returns_bundle_app_config_if_there_is_local_config
290290
FileUtils.rm_r(File.join(Dir.pwd, ".bundle")) unless ENV["CI"]
291291
end
292292

293+
def test_custom_bundle_uses_alternative_gemfiles
294+
Dir.mktmpdir do |dir|
295+
Dir.chdir(dir) do
296+
File.write(File.join(dir, "gems.rb"), <<~GEMFILE)
297+
source "https://rubygems.org"
298+
gem "rdoc"
299+
GEMFILE
300+
301+
Bundler.with_unbundled_env do
302+
capture_subprocess_io do
303+
system("bundle install")
304+
run_script
305+
end
306+
end
307+
308+
assert_path_exists(".ruby-lsp")
309+
assert_path_exists(".ruby-lsp/gems.rb")
310+
assert_path_exists(".ruby-lsp/gems.locked")
311+
assert_match("debug", File.read(".ruby-lsp/gems.rb"))
312+
assert_match("ruby-lsp", File.read(".ruby-lsp/gems.rb"))
313+
assert_match("eval_gemfile(File.expand_path(\"../gems.rb\", __dir__))", File.read(".ruby-lsp/gems.rb"))
314+
end
315+
end
316+
end
317+
293318
private
294319

295320
# This method runs the script and then immediately unloads it. This allows us to make assertions against the effects

0 commit comments

Comments
 (0)