Skip to content

Commit 238843e

Browse files
authored
Avoid loading incorrect add-on versions when bundle path is inside project (#3620)
1 parent 939bfe9 commit 238843e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/ruby_lsp/addon.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ def load_addons(global_state, outgoing_queue, include_project_addons: true)
5656
addon_files = Gem.find_files("ruby_lsp/**/addon.rb")
5757

5858
if include_project_addons
59-
addon_files.concat(Dir.glob(File.join(global_state.workspace_path, "**", "ruby_lsp/**/addon.rb")))
59+
project_addons = Dir.glob("#{global_state.workspace_path}/**/ruby_lsp/**/addon.rb")
60+
61+
# Ignore add-ons from dependencies if the bundle is stored inside the project. We already found those with
62+
# `Gem.find_files`
63+
bundle_path = Bundler.bundle_path.to_s
64+
project_addons.reject! { |path| path.start_with?(bundle_path) }
65+
addon_files.concat(project_addons)
6066
end
6167

6268
errors = addon_files.filter_map do |addon_path|

test/addon_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,40 @@ def version
190190
assert_predicate(addon, :hello)
191191
end
192192
end
193+
194+
def test_loading_project_addons_ignores_bundle_path
195+
Dir.mktmpdir do |dir|
196+
addon_dir = File.join(dir, "vendor", "bundle", "ruby_lsp", "test_addon")
197+
FileUtils.mkdir_p(addon_dir)
198+
File.write(File.join(addon_dir, "addon.rb"), <<~RUBY)
199+
class ProjectAddon < RubyLsp::Addon
200+
attr_reader :hello
201+
202+
def activate(global_state, outgoing_queue)
203+
@hello = true
204+
end
205+
206+
def name
207+
"Project Addon"
208+
end
209+
210+
def version
211+
"0.1.0"
212+
end
213+
end
214+
RUBY
215+
216+
@global_state.apply_options({
217+
workspaceFolders: [{ uri: URI::Generic.from_path(path: dir).to_s }],
218+
})
219+
220+
Bundler.stubs(:bundle_path).returns(Pathname.new(File.join(dir, "vendor", "bundle")))
221+
Addon.load_addons(@global_state, @outgoing_queue)
222+
223+
assert_raises(Addon::AddonNotFoundError) do
224+
Addon.get("Project Addon", "0.1.0")
225+
end
226+
end
227+
end
193228
end
194229
end

0 commit comments

Comments
 (0)