Skip to content

Commit

Permalink
Add some necessary guards for operating with a partial bundle (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Oct 29, 2024
1 parent 7d27dc6 commit 30e770a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 12 additions & 3 deletions lib/ruby_lsp/global_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GlobalState
attr_reader :encoding

sig { returns(T::Boolean) }
attr_reader :supports_watching_files, :experimental_features, :supports_request_delegation
attr_reader :supports_watching_files, :experimental_features, :supports_request_delegation, :top_level_bundle

sig { returns(T::Array[String]) }
attr_reader :supported_resource_operations
Expand All @@ -46,6 +46,15 @@ def initialize
@addon_settings = T.let({}, T::Hash[String, T.untyped])
@supports_request_delegation = T.let(false, T::Boolean)
@supported_resource_operations = T.let([], T::Array[String])
@top_level_bundle = T.let(
begin
Bundler.with_original_env { Bundler.default_gemfile }
true
rescue Bundler::GemfileNotFound, Bundler::GitError
false
end,
T::Boolean,
)
end

sig { params(addon_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
Expand Down Expand Up @@ -240,15 +249,15 @@ def gather_direct_dependencies

sig { returns(T::Array[String]) }
def gemspec_dependencies
Bundler.locked_gems.sources
(Bundler.locked_gems&.sources || [])
.grep(Bundler::Source::Gemspec)
.flat_map { _1.gemspec&.dependencies&.map(&:name) }
end

sig { returns(T::Array[String]) }
def gather_direct_and_indirect_dependencies
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.specs.map(&:name)
Bundler.locked_gems&.specs&.map(&:name) || []
rescue Bundler::GemfileNotFound
[]
end
Expand Down
12 changes: 11 additions & 1 deletion lib/ruby_lsp/requests/support/rubocop_runner.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
# typed: strict
# frozen_string_literal: true

# If there's no top level Gemfile, don't load RuboCop from a global installation
begin
Bundler.with_original_env { Bundler.default_gemfile }
rescue Bundler::GemfileNotFound
return
end

# Ensure that RuboCop is available
begin
require "rubocop"
rescue LoadError
return
end

# Ensure that RuboCop is at least version 1.4.0
begin
gem("rubocop", ">= 1.4.0")
rescue LoadError
raise StandardError, "Incompatible RuboCop version. Ruby LSP requires >= 1.4.0"
$stderr.puts "Incompatible RuboCop version. Ruby LSP requires >= 1.4.0"
return
end

if RuboCop.const_defined?(:LSP) # This condition will be removed when requiring RuboCop >= 1.61.
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def type_hierarchy_subtypes(message)

sig { params(message: T::Hash[Symbol, T.untyped]).void }
def workspace_dependencies(message)
response = begin
response = if @global_state.top_level_bundle
Bundler.with_original_env do
definition = Bundler.definition
dep_keys = definition.locked_deps.keys.to_set
Expand All @@ -1043,7 +1043,7 @@ def workspace_dependencies(message)
}
end
end
rescue Bundler::GemfileNotFound, Bundler::GitError
else
[]
end

Expand Down

0 comments on commit 30e770a

Please sign in to comment.