diff --git a/lib/ruby_lsp/addon.rb b/lib/ruby_lsp/addon.rb index 230b87a65..c03300094 100644 --- a/lib/ruby_lsp/addon.rb +++ b/lib/ruby_lsp/addon.rb @@ -56,9 +56,16 @@ def load_addons(global_state, outgoing_queue, include_project_addons: true) addon_files = Gem.find_files("ruby_lsp/**/addon.rb") if include_project_addons - bundle_path = Bundler.bundle_path.to_s + bundle_path = begin + Bundler.bundle_path.to_s + rescue Bundler::GemfileNotFound + nil + end + project_addons = Dir.glob("#{global_state.workspace_path}/**/ruby_lsp/**/addon.rb") - project_addons.reject! { |path| path.start_with?(bundle_path) || gem_installation_path?(path) } + project_addons.reject! do |path| + (bundle_path && path.start_with?(bundle_path)) || gem_installation_path?(path) + end addon_files.concat(project_addons) end diff --git a/test/addon_test.rb b/test/addon_test.rb index 75ec3687e..8e88ba8d8 100644 --- a/test/addon_test.rb +++ b/test/addon_test.rb @@ -191,6 +191,39 @@ def version end end + def test_loading_project_addons_when_the_project_has_no_gemfile + Dir.mktmpdir do |dir| + addon_dir = File.join(dir, "lib", "ruby_lsp", "test_addon") + FileUtils.mkdir_p(addon_dir) + File.write(File.join(addon_dir, "addon.rb"), <<~RUBY) + class NoGemfileProjectAddon < RubyLsp::Addon + def activate(global_state, outgoing_queue) + end + + def name + "No Gemfile Project Addon" + end + + def version + "0.1.0" + end + end + RUBY + + @global_state.apply_options({ + workspaceFolders: [{ uri: URI::Generic.from_path(path: dir).to_s }], + }) + + # Projects without a Gemfile have no bundle path at all and asking Bundler for one raises + Bundler.stubs(:bundle_path).raises(Bundler::GemfileNotFound) + + Addon.load_addons(@global_state, @outgoing_queue) + + addon = Addon.get("No Gemfile Project Addon", "0.1.0") + assert_equal("No Gemfile Project Addon", addon.name) + end + end + def test_loading_project_addons_ignores_bundle_path Dir.mktmpdir do |dir| addon_dir = File.join(dir, "vendor", "bundle", "ruby_lsp", "test_addon")