solargraph 0.58.0 → 0.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +3 -0
  3. data/.gitattributes +2 -0
  4. data/.github/workflows/linting.yml +6 -5
  5. data/.github/workflows/plugins.yml +48 -35
  6. data/.github/workflows/rspec.yml +15 -28
  7. data/.github/workflows/typecheck.yml +3 -2
  8. data/.gitignore +1 -0
  9. data/.rubocop.yml +38 -6
  10. data/.rubocop_todo.yml +53 -966
  11. data/CHANGELOG.md +24 -0
  12. data/Gemfile +3 -1
  13. data/README.md +3 -3
  14. data/Rakefile +26 -23
  15. data/bin/solargraph +9 -8
  16. data/lib/solargraph/api_map/cache.rb +110 -110
  17. data/lib/solargraph/api_map/constants.rb +288 -279
  18. data/lib/solargraph/api_map/index.rb +204 -193
  19. data/lib/solargraph/api_map/source_to_yard.rb +110 -97
  20. data/lib/solargraph/api_map/store.rb +395 -384
  21. data/lib/solargraph/api_map.rb +1029 -945
  22. data/lib/solargraph/bench.rb +44 -45
  23. data/lib/solargraph/complex_type/conformance.rb +176 -0
  24. data/lib/solargraph/complex_type/type_methods.rb +240 -228
  25. data/lib/solargraph/complex_type/unique_type.rb +641 -482
  26. data/lib/solargraph/complex_type.rb +557 -444
  27. data/lib/solargraph/convention/data_definition/data_assignment_node.rb +61 -61
  28. data/lib/solargraph/convention/data_definition/data_definition_node.rb +93 -91
  29. data/lib/solargraph/convention/data_definition.rb +108 -105
  30. data/lib/solargraph/convention/gemfile.rb +15 -15
  31. data/lib/solargraph/convention/gemspec.rb +23 -23
  32. data/lib/solargraph/convention/rakefile.rb +17 -17
  33. data/lib/solargraph/convention/struct_definition/struct_assignment_node.rb +62 -61
  34. data/lib/solargraph/convention/struct_definition/struct_definition_node.rb +103 -102
  35. data/lib/solargraph/convention/struct_definition.rb +168 -164
  36. data/lib/solargraph/convention.rb +78 -78
  37. data/lib/solargraph/converters/dd.rb +19 -17
  38. data/lib/solargraph/converters/dl.rb +17 -15
  39. data/lib/solargraph/converters/dt.rb +17 -15
  40. data/lib/solargraph/converters/misc.rb +3 -1
  41. data/lib/solargraph/diagnostics/require_not_found.rb +54 -53
  42. data/lib/solargraph/diagnostics/rubocop.rb +119 -118
  43. data/lib/solargraph/diagnostics/rubocop_helpers.rb +70 -68
  44. data/lib/solargraph/diagnostics/type_check.rb +56 -55
  45. data/lib/solargraph/diagnostics/update_errors.rb +37 -41
  46. data/lib/solargraph/doc_map.rb +438 -439
  47. data/lib/solargraph/equality.rb +34 -34
  48. data/lib/solargraph/gem_pins.rb +98 -98
  49. data/lib/solargraph/language_server/error_codes.rb +20 -20
  50. data/lib/solargraph/language_server/host/diagnoser.rb +89 -89
  51. data/lib/solargraph/language_server/host/dispatch.rb +130 -130
  52. data/lib/solargraph/language_server/host/message_worker.rb +113 -112
  53. data/lib/solargraph/language_server/host/sources.rb +100 -99
  54. data/lib/solargraph/language_server/host.rb +886 -878
  55. data/lib/solargraph/language_server/message/base.rb +97 -97
  56. data/lib/solargraph/language_server/message/client/register_capability.rb +13 -15
  57. data/lib/solargraph/language_server/message/completion_item/resolve.rb +58 -60
  58. data/lib/solargraph/language_server/message/extended/check_gem_version.rb +108 -114
  59. data/lib/solargraph/language_server/message/extended/document.rb +24 -23
  60. data/lib/solargraph/language_server/message/extended/document_gems.rb +32 -32
  61. data/lib/solargraph/language_server/message/extended/download_core.rb +20 -19
  62. data/lib/solargraph/language_server/message/extended/search.rb +20 -20
  63. data/lib/solargraph/language_server/message/initialize.rb +197 -191
  64. data/lib/solargraph/language_server/message/text_document/completion.rb +58 -56
  65. data/lib/solargraph/language_server/message/text_document/definition.rb +49 -40
  66. data/lib/solargraph/language_server/message/text_document/document_highlight.rb +23 -16
  67. data/lib/solargraph/language_server/message/text_document/document_symbol.rb +36 -26
  68. data/lib/solargraph/language_server/message/text_document/formatting.rb +150 -148
  69. data/lib/solargraph/language_server/message/text_document/hover.rb +58 -58
  70. data/lib/solargraph/language_server/message/text_document/prepare_rename.rb +18 -11
  71. data/lib/solargraph/language_server/message/text_document/references.rb +23 -16
  72. data/lib/solargraph/language_server/message/text_document/rename.rb +26 -19
  73. data/lib/solargraph/language_server/message/text_document/signature_help.rb +25 -24
  74. data/lib/solargraph/language_server/message/text_document/type_definition.rb +33 -25
  75. data/lib/solargraph/language_server/message/workspace/did_change_configuration.rb +41 -35
  76. data/lib/solargraph/language_server/message/workspace/did_change_watched_files.rb +48 -40
  77. data/lib/solargraph/language_server/message/workspace/did_change_workspace_folders.rb +32 -26
  78. data/lib/solargraph/language_server/message/workspace/workspace_symbol.rb +33 -23
  79. data/lib/solargraph/language_server/message.rb +94 -94
  80. data/lib/solargraph/language_server/request.rb +29 -27
  81. data/lib/solargraph/language_server/transport/data_reader.rb +72 -74
  82. data/lib/solargraph/language_server/uri_helpers.rb +49 -49
  83. data/lib/solargraph/library.rb +702 -683
  84. data/lib/solargraph/location.rb +85 -82
  85. data/lib/solargraph/logging.rb +55 -37
  86. data/lib/solargraph/page.rb +92 -92
  87. data/lib/solargraph/parser/comment_ripper.rb +84 -69
  88. data/lib/solargraph/parser/flow_sensitive_typing.rb +471 -255
  89. data/lib/solargraph/parser/node_processor/base.rb +122 -92
  90. data/lib/solargraph/parser/node_processor.rb +63 -62
  91. data/lib/solargraph/parser/parser_gem/class_methods.rb +165 -149
  92. data/lib/solargraph/parser/parser_gem/flawed_builder.rb +19 -19
  93. data/lib/solargraph/parser/parser_gem/node_chainer.rb +191 -166
  94. data/lib/solargraph/parser/parser_gem/node_methods.rb +507 -486
  95. data/lib/solargraph/parser/parser_gem/node_processors/and_node.rb +22 -22
  96. data/lib/solargraph/parser/parser_gem/node_processors/args_node.rb +61 -59
  97. data/lib/solargraph/parser/parser_gem/node_processors/begin_node.rb +24 -15
  98. data/lib/solargraph/parser/parser_gem/node_processors/block_node.rb +45 -46
  99. data/lib/solargraph/parser/parser_gem/node_processors/def_node.rb +60 -53
  100. data/lib/solargraph/parser/parser_gem/node_processors/defs_node.rb +38 -37
  101. data/lib/solargraph/parser/parser_gem/node_processors/if_node.rb +53 -23
  102. data/lib/solargraph/parser/parser_gem/node_processors/ivasgn_node.rb +42 -40
  103. data/lib/solargraph/parser/parser_gem/node_processors/lvasgn_node.rb +30 -29
  104. data/lib/solargraph/parser/parser_gem/node_processors/masgn_node.rb +61 -59
  105. data/lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb +98 -98
  106. data/lib/solargraph/parser/parser_gem/node_processors/or_node.rb +22 -0
  107. data/lib/solargraph/parser/parser_gem/node_processors/orasgn_node.rb +17 -17
  108. data/lib/solargraph/parser/parser_gem/node_processors/resbody_node.rb +39 -38
  109. data/lib/solargraph/parser/parser_gem/node_processors/sclass_node.rb +51 -52
  110. data/lib/solargraph/parser/parser_gem/node_processors/send_node.rb +302 -291
  111. data/lib/solargraph/parser/parser_gem/node_processors/until_node.rb +29 -29
  112. data/lib/solargraph/parser/parser_gem/node_processors/when_node.rb +23 -0
  113. data/lib/solargraph/parser/parser_gem/node_processors/while_node.rb +33 -29
  114. data/lib/solargraph/parser/parser_gem/node_processors.rb +74 -70
  115. data/lib/solargraph/parser/parser_gem.rb +14 -12
  116. data/lib/solargraph/parser/region.rb +75 -69
  117. data/lib/solargraph/parser/snippet.rb +19 -17
  118. data/lib/solargraph/parser.rb +25 -23
  119. data/lib/solargraph/pin/base.rb +773 -729
  120. data/lib/solargraph/pin/base_variable.rb +375 -126
  121. data/lib/solargraph/pin/block.rb +127 -104
  122. data/lib/solargraph/pin/breakable.rb +15 -9
  123. data/lib/solargraph/pin/callable.rb +275 -231
  124. data/lib/solargraph/pin/closure.rb +69 -72
  125. data/lib/solargraph/pin/common.rb +97 -79
  126. data/lib/solargraph/pin/compound_statement.rb +55 -0
  127. data/lib/solargraph/pin/constant.rb +43 -45
  128. data/lib/solargraph/pin/conversions.rb +129 -123
  129. data/lib/solargraph/pin/delegated_method.rb +131 -120
  130. data/lib/solargraph/pin/documenting.rb +116 -114
  131. data/lib/solargraph/pin/instance_variable.rb +38 -34
  132. data/lib/solargraph/pin/keyword.rb +16 -20
  133. data/lib/solargraph/pin/local_variable.rb +31 -75
  134. data/lib/solargraph/pin/method.rb +724 -672
  135. data/lib/solargraph/pin/method_alias.rb +42 -34
  136. data/lib/solargraph/pin/namespace.rb +122 -115
  137. data/lib/solargraph/pin/parameter.rb +339 -275
  138. data/lib/solargraph/pin/proxy_type.rb +42 -39
  139. data/lib/solargraph/pin/reference/override.rb +47 -47
  140. data/lib/solargraph/pin/reference/superclass.rb +17 -15
  141. data/lib/solargraph/pin/reference/type_alias.rb +16 -0
  142. data/lib/solargraph/pin/reference.rb +59 -39
  143. data/lib/solargraph/pin/search.rb +62 -61
  144. data/lib/solargraph/pin/signature.rb +64 -61
  145. data/lib/solargraph/pin/symbol.rb +54 -53
  146. data/lib/solargraph/pin/until.rb +16 -18
  147. data/lib/solargraph/pin/while.rb +16 -18
  148. data/lib/solargraph/pin.rb +46 -44
  149. data/lib/solargraph/pin_cache.rb +248 -245
  150. data/lib/solargraph/position.rb +139 -119
  151. data/lib/solargraph/range.rb +113 -112
  152. data/lib/solargraph/rbs_map/conversions.rb +952 -823
  153. data/lib/solargraph/rbs_map/core_fills.rb +91 -84
  154. data/lib/solargraph/rbs_map/core_map.rb +65 -58
  155. data/lib/solargraph/rbs_map/stdlib_map.rb +71 -43
  156. data/lib/solargraph/rbs_map.rb +207 -163
  157. data/lib/solargraph/server_methods.rb +16 -16
  158. data/lib/solargraph/shell.rb +555 -352
  159. data/lib/solargraph/source/chain/array.rb +39 -37
  160. data/lib/solargraph/source/chain/call.rb +377 -337
  161. data/lib/solargraph/source/chain/class_variable.rb +13 -13
  162. data/lib/solargraph/source/chain/constant.rb +30 -26
  163. data/lib/solargraph/source/chain/global_variable.rb +13 -13
  164. data/lib/solargraph/source/chain/hash.rb +37 -34
  165. data/lib/solargraph/source/chain/if.rb +30 -28
  166. data/lib/solargraph/source/chain/instance_variable.rb +36 -13
  167. data/lib/solargraph/source/chain/link.rb +99 -109
  168. data/lib/solargraph/source/chain/literal.rb +51 -48
  169. data/lib/solargraph/source/chain/or.rb +29 -23
  170. data/lib/solargraph/source/chain/q_call.rb +13 -11
  171. data/lib/solargraph/source/chain/variable.rb +15 -13
  172. data/lib/solargraph/source/chain/z_super.rb +28 -30
  173. data/lib/solargraph/source/chain.rb +304 -291
  174. data/lib/solargraph/source/change.rb +89 -82
  175. data/lib/solargraph/source/cursor.rb +172 -166
  176. data/lib/solargraph/source/encoding_fixes.rb +22 -23
  177. data/lib/solargraph/source/source_chainer.rb +218 -194
  178. data/lib/solargraph/source/updater.rb +59 -55
  179. data/lib/solargraph/source.rb +522 -498
  180. data/lib/solargraph/source_map/clip.rb +245 -226
  181. data/lib/solargraph/source_map/data.rb +37 -34
  182. data/lib/solargraph/source_map/mapper.rb +286 -259
  183. data/lib/solargraph/source_map.rb +224 -212
  184. data/lib/solargraph/type_checker/problem.rb +34 -32
  185. data/lib/solargraph/type_checker/rules.rb +157 -84
  186. data/lib/solargraph/type_checker.rb +888 -814
  187. data/lib/solargraph/version.rb +5 -5
  188. data/lib/solargraph/workspace/config.rb +258 -255
  189. data/lib/solargraph/workspace/gemspecs.rb +367 -0
  190. data/lib/solargraph/workspace/require_paths.rb +98 -97
  191. data/lib/solargraph/workspace.rb +242 -220
  192. data/lib/solargraph/yard_map/helpers.rb +49 -44
  193. data/lib/solargraph/yard_map/mapper/to_method.rb +136 -130
  194. data/lib/solargraph/yard_map/mapper/to_namespace.rb +32 -31
  195. data/lib/solargraph/yard_map/mapper.rb +84 -79
  196. data/lib/solargraph/yard_map.rb +17 -18
  197. data/lib/solargraph/yard_tags.rb +20 -20
  198. data/lib/solargraph/yardoc.rb +90 -87
  199. data/lib/solargraph.rb +128 -105
  200. data/rbs/fills/rubygems/0/dependency.rbs +193 -0
  201. data/rbs/fills/tuple/tuple.rbs +28 -0
  202. data/rbs/shims/ast/0/node.rbs +5 -0
  203. data/rbs/shims/diff-lcs/1.5/diff-lcs.rbs +11 -0
  204. data/rbs_collection.yaml +1 -1
  205. data/solargraph.gemspec +37 -35
  206. metadata +52 -51
  207. data/lib/solargraph/type_checker/checks.rb +0 -124
  208. data/lib/solargraph/type_checker/param_def.rb +0 -37
  209. data/lib/solargraph/yard_map/to_method.rb +0 -89
  210. data/sig/shims/ast/0/node.rbs +0 -5
  211. /data/{sig → rbs}/shims/ast/2.4/.rbs_meta.yaml +0 -0
  212. /data/{sig → rbs}/shims/ast/2.4/ast.rbs +0 -0
  213. /data/{sig → rbs}/shims/parser/3.2.0.1/builders/default.rbs +0 -0
  214. /data/{sig → rbs}/shims/parser/3.2.0.1/manifest.yaml +0 -0
  215. /data/{sig → rbs}/shims/parser/3.2.0.1/parser.rbs +0 -0
  216. /data/{sig → rbs}/shims/parser/3.2.0.1/polyfill.rbs +0 -0
  217. /data/{sig → rbs}/shims/thor/1.2.0.1/.rbs_meta.yaml +0 -0
  218. /data/{sig → rbs}/shims/thor/1.2.0.1/manifest.yaml +0 -0
  219. /data/{sig → rbs}/shims/thor/1.2.0.1/thor.rbs +0 -0
@@ -1,439 +1,438 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
- require 'benchmark'
5
- require 'open3'
6
-
7
- module Solargraph
8
- # A collection of pins generated from required gems.
9
- #
10
- class DocMap
11
- include Logging
12
-
13
- # @return [Array<String>]
14
- attr_reader :requires
15
- alias required requires
16
-
17
- # @return [Array<Gem::Specification>]
18
- attr_reader :preferences
19
-
20
- # @return [Array<Pin::Base>]
21
- attr_reader :pins
22
-
23
- # @return [Array<Gem::Specification>]
24
- def uncached_gemspecs
25
- uncached_yard_gemspecs.concat(uncached_rbs_collection_gemspecs)
26
- .sort
27
- .uniq { |gemspec| "#{gemspec.name}:#{gemspec.version}" }
28
- end
29
-
30
- # @return [Array<Gem::Specification>]
31
- attr_reader :uncached_yard_gemspecs
32
-
33
- # @return [Array<Gem::Specification>]
34
- attr_reader :uncached_rbs_collection_gemspecs
35
-
36
- # @return [String, nil]
37
- attr_reader :rbs_collection_path
38
-
39
- # @return [String, nil]
40
- attr_reader :rbs_collection_config_path
41
-
42
- # @return [Workspace, nil]
43
- attr_reader :workspace
44
-
45
- # @return [Environ]
46
- attr_reader :environ
47
-
48
- # @param requires [Array<String>]
49
- # @param preferences [Array<Gem::Specification>]
50
- # @param workspace [Workspace, nil]
51
- def initialize(requires, preferences, workspace = nil)
52
- @requires = requires.compact
53
- @preferences = preferences.compact
54
- @workspace = workspace
55
- @rbs_collection_path = workspace&.rbs_collection_path
56
- @rbs_collection_config_path = workspace&.rbs_collection_config_path
57
- @environ = Convention.for_global(self)
58
- @requires.concat @environ.requires if @environ
59
- load_serialized_gem_pins
60
- pins.concat @environ.pins
61
- end
62
-
63
- # @param out [IO]
64
- # @return [void]
65
- def cache_all!(out)
66
- # if we log at debug level:
67
- if logger.info?
68
- gem_desc = uncached_gemspecs.map { |gemspec| "#{gemspec.name}:#{gemspec.version}" }.join(', ')
69
- logger.info "Caching pins for gems: #{gem_desc}" unless uncached_gemspecs.empty?
70
- end
71
- logger.debug { "Caching for YARD: #{uncached_yard_gemspecs.map(&:name)}" }
72
- logger.debug { "Caching for RBS collection: #{uncached_rbs_collection_gemspecs.map(&:name)}" }
73
- load_serialized_gem_pins
74
- uncached_gemspecs.each do |gemspec|
75
- cache(gemspec, out: out)
76
- end
77
- load_serialized_gem_pins
78
- @uncached_rbs_collection_gemspecs = []
79
- @uncached_yard_gemspecs = []
80
- end
81
-
82
- # @param gemspec [Gem::Specification]
83
- # @param out [IO]
84
- # @return [void]
85
- def cache_yard_pins(gemspec, out)
86
- pins = GemPins.build_yard_pins(yard_plugins, gemspec)
87
- PinCache.serialize_yard_gem(gemspec, pins)
88
- logger.info { "Cached #{pins.length} YARD pins for gem #{gemspec.name}:#{gemspec.version}" } unless pins.empty?
89
- end
90
-
91
- # @param gemspec [Gem::Specification]
92
- # @param out [IO]
93
- # @return [void]
94
- def cache_rbs_collection_pins(gemspec, out)
95
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
96
- pins = rbs_map.pins
97
- rbs_version_cache_key = rbs_map.cache_key
98
- # cache pins even if result is zero, so we don't retry building pins
99
- pins ||= []
100
- PinCache.serialize_rbs_collection_gem(gemspec, rbs_version_cache_key, pins)
101
- logger.info { "Cached #{pins.length} RBS collection pins for gem #{gemspec.name} #{gemspec.version} with cache_key #{rbs_version_cache_key.inspect}" unless pins.empty? }
102
- end
103
-
104
- # @param gemspec [Gem::Specification]
105
- # @param rebuild [Boolean] whether to rebuild the pins even if they are cached
106
- # @param out [IO, nil] output stream for logging
107
- # @return [void]
108
- def cache(gemspec, rebuild: false, out: nil)
109
- build_yard = uncached_yard_gemspecs.include?(gemspec) || rebuild
110
- build_rbs_collection = uncached_rbs_collection_gemspecs.include?(gemspec) || rebuild
111
- if build_yard || build_rbs_collection
112
- type = []
113
- type << 'YARD' if build_yard
114
- type << 'RBS collection' if build_rbs_collection
115
- out.puts("Caching #{type.join(' and ')} pins for gem #{gemspec.name}:#{gemspec.version}") if out
116
- end
117
- cache_yard_pins(gemspec, out) if build_yard
118
- cache_rbs_collection_pins(gemspec, out) if build_rbs_collection
119
- end
120
-
121
- # @return [Array<Gem::Specification>]
122
- def gemspecs
123
- @gemspecs ||= required_gems_map.values.compact.flatten
124
- end
125
-
126
- # @return [Array<String>]
127
- def unresolved_requires
128
- @unresolved_requires ||= required_gems_map.select { |_, gemspecs| gemspecs.nil? }.keys
129
- end
130
-
131
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
132
- def self.all_yard_gems_in_memory
133
- @yard_gems_in_memory ||= {}
134
- end
135
-
136
- # @return [Hash{String => Hash{Array(String, String) => Array<Pin::Base>}}] stored by RBS collection path
137
- def self.all_rbs_collection_gems_in_memory
138
- @rbs_collection_gems_in_memory ||= {}
139
- end
140
-
141
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
142
- def yard_pins_in_memory
143
- self.class.all_yard_gems_in_memory
144
- end
145
-
146
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
147
- def rbs_collection_pins_in_memory
148
- self.class.all_rbs_collection_gems_in_memory[rbs_collection_path] ||= {}
149
- end
150
-
151
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
152
- def self.all_combined_pins_in_memory
153
- @combined_pins_in_memory ||= {}
154
- end
155
-
156
- # @todo this should also include an index by the hash of the RBS collection
157
- # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
158
- def combined_pins_in_memory
159
- self.class.all_combined_pins_in_memory
160
- end
161
-
162
- # @return [Array<String>]
163
- def yard_plugins
164
- @environ.yard_plugins
165
- end
166
-
167
- # @return [Set<Gem::Specification>]
168
- def dependencies
169
- @dependencies ||= (gemspecs.flat_map { |spec| fetch_dependencies(spec) } - gemspecs).to_set
170
- end
171
-
172
- private
173
-
174
- # @return [void]
175
- def load_serialized_gem_pins
176
- @pins = []
177
- @uncached_yard_gemspecs = []
178
- @uncached_rbs_collection_gemspecs = []
179
- with_gemspecs, without_gemspecs = required_gems_map.partition { |_, v| v }
180
- # @sg-ignore Need support for RBS duck interfaces like _ToHash
181
- # @type [Array<String>]
182
- paths = Hash[without_gemspecs].keys
183
- # @sg-ignore Need support for RBS duck interfaces like _ToHash
184
- # @type [Array<Gem::Specification>]
185
- gemspecs = Hash[with_gemspecs].values.flatten.compact + dependencies.to_a
186
-
187
- paths.each do |path|
188
- rbs_pins = deserialize_stdlib_rbs_map path
189
- end
190
-
191
- logger.debug { "DocMap#load_serialized_gem_pins: Combining pins..." }
192
- time = Benchmark.measure do
193
- gemspecs.each do |gemspec|
194
- pins = deserialize_combined_pin_cache gemspec
195
- @pins.concat pins if pins
196
- end
197
- end
198
- logger.info { "DocMap#load_serialized_gem_pins: Loaded and processed serialized pins together in #{time.real} seconds" }
199
- @uncached_yard_gemspecs.uniq!
200
- @uncached_rbs_collection_gemspecs.uniq!
201
- nil
202
- end
203
-
204
- # @return [Hash{String => Array<Gem::Specification>}]
205
- def required_gems_map
206
- @required_gems_map ||= requires.to_h { |path| [path, resolve_path_to_gemspecs(path)] }
207
- end
208
-
209
- # @return [Hash{String => Gem::Specification}]
210
- def preference_map
211
- @preference_map ||= preferences.to_h { |gemspec| [gemspec.name, gemspec] }
212
- end
213
-
214
- # @param gemspec [Gem::Specification]
215
- # @return [Array<Pin::Base>, nil]
216
- def deserialize_yard_pin_cache gemspec
217
- if yard_pins_in_memory.key?([gemspec.name, gemspec.version])
218
- return yard_pins_in_memory[[gemspec.name, gemspec.version]]
219
- end
220
-
221
- cached = PinCache.deserialize_yard_gem(gemspec)
222
- if cached
223
- logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
224
- yard_pins_in_memory[[gemspec.name, gemspec.version]] = cached
225
- cached
226
- else
227
- logger.debug "No YARD pin cache for #{gemspec.name}:#{gemspec.version}"
228
- @uncached_yard_gemspecs.push gemspec
229
- nil
230
- end
231
- end
232
-
233
- # @param gemspec [Gem::Specification]
234
- # @return [void]
235
- def deserialize_combined_pin_cache(gemspec)
236
- unless combined_pins_in_memory[[gemspec.name, gemspec.version]].nil?
237
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
238
- end
239
-
240
- rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
241
- rbs_version_cache_key = rbs_map.cache_key
242
-
243
- cached = PinCache.deserialize_combined_gem(gemspec, rbs_version_cache_key)
244
- if cached
245
- logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
246
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = cached
247
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
248
- end
249
-
250
- rbs_collection_pins = deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
251
-
252
- yard_pins = deserialize_yard_pin_cache gemspec
253
-
254
- if !rbs_collection_pins.nil? && !yard_pins.nil?
255
- logger.debug { "Combining pins for #{gemspec.name}:#{gemspec.version}" }
256
- combined_pins = GemPins.combine(yard_pins, rbs_collection_pins)
257
- PinCache.serialize_combined_gem(gemspec, rbs_version_cache_key, combined_pins)
258
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = combined_pins
259
- logger.info { "Generated #{combined_pins_in_memory[[gemspec.name, gemspec.version]].length} combined pins for #{gemspec.name} #{gemspec.version}" }
260
- return combined_pins
261
- end
262
-
263
- if !yard_pins.nil?
264
- logger.debug { "Using only YARD pins for #{gemspec.name}:#{gemspec.version}" }
265
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = yard_pins
266
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
267
- elsif !rbs_collection_pins.nil?
268
- logger.debug { "Using only RBS collection pins for #{gemspec.name}:#{gemspec.version}" }
269
- combined_pins_in_memory[[gemspec.name, gemspec.version]] = rbs_collection_pins
270
- return combined_pins_in_memory[[gemspec.name, gemspec.version]]
271
- else
272
- logger.debug { "Pins not yet cached for #{gemspec.name}:#{gemspec.version}" }
273
- return nil
274
- end
275
- end
276
-
277
- # @param path [String] require path that might be in the RBS stdlib collection
278
- # @return [void]
279
- def deserialize_stdlib_rbs_map path
280
- map = RbsMap::StdlibMap.load(path)
281
- if map.resolved?
282
- logger.debug { "Loading stdlib pins for #{path}" }
283
- @pins.concat map.pins
284
- logger.debug { "Loaded #{map.pins.length} stdlib pins for #{path}" }
285
- map.pins
286
- else
287
- # @todo Temporarily ignoring unresolved `require 'set'`
288
- logger.debug { "Require path #{path} could not be resolved in RBS" } unless path == 'set'
289
- nil
290
- end
291
- end
292
-
293
- # @param gemspec [Gem::Specification]
294
- # @param rbs_version_cache_key [String]
295
- # @return [Array<Pin::Base>, nil]
296
- def deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
297
- return if rbs_collection_pins_in_memory.key?([gemspec, rbs_version_cache_key])
298
- cached = PinCache.deserialize_rbs_collection_gem(gemspec, rbs_version_cache_key)
299
- if cached
300
- logger.info { "Loaded #{cached.length} pins from RBS collection cache for #{gemspec.name}:#{gemspec.version}" } unless cached.empty?
301
- rbs_collection_pins_in_memory[[gemspec, rbs_version_cache_key]] = cached
302
- cached
303
- else
304
- logger.debug "No RBS collection pin cache for #{gemspec.name} #{gemspec.version}"
305
- @uncached_rbs_collection_gemspecs.push gemspec
306
- nil
307
- end
308
- end
309
-
310
- # @param path [String]
311
- # @return [::Array<Gem::Specification>, nil]
312
- def resolve_path_to_gemspecs path
313
- return nil if path.empty?
314
- return gemspecs_required_from_bundler if path == 'bundler/require'
315
-
316
- # @type [Gem::Specification, nil]
317
- gemspec = Gem::Specification.find_by_path(path)
318
- if gemspec.nil?
319
- gem_name_guess = path.split('/').first
320
- begin
321
- # this can happen when the gem is included via a local path in
322
- # a Gemfile; Gem doesn't try to index the paths in that case.
323
- #
324
- # See if we can make a good guess:
325
- potential_gemspec = Gem::Specification.find_by_name(gem_name_guess)
326
- file = "lib/#{path}.rb"
327
- gemspec = potential_gemspec if potential_gemspec.files.any? { |gemspec_file| file == gemspec_file }
328
- rescue Gem::MissingSpecError
329
- logger.debug { "Require path #{path} could not be resolved to a gem via find_by_path or guess of #{gem_name_guess}" }
330
- []
331
- end
332
- end
333
- return nil if gemspec.nil?
334
- [gemspec_or_preference(gemspec)]
335
- end
336
-
337
- # @param gemspec [Gem::Specification]
338
- # @return [Gem::Specification]
339
- def gemspec_or_preference gemspec
340
- # :nocov: dormant feature
341
- return gemspec unless preference_map.key?(gemspec.name)
342
- return gemspec if gemspec.version == preference_map[gemspec.name].version
343
-
344
- change_gemspec_version gemspec, preference_map[gemspec.name].version
345
- # :nocov:
346
- end
347
-
348
- # @param gemspec [Gem::Specification]
349
- # @param version [Gem::Version]
350
- # @return [Gem::Specification]
351
- def change_gemspec_version gemspec, version
352
- Gem::Specification.find_by_name(gemspec.name, "= #{version}")
353
- rescue Gem::MissingSpecError
354
- Solargraph.logger.info "Gem #{gemspec.name} version #{version} not found. Using #{gemspec.version} instead"
355
- gemspec
356
- end
357
-
358
- # @param gemspec [Gem::Specification]
359
- # @return [Array<Gem::Specification>]
360
- def fetch_dependencies gemspec
361
- # @param spec [Gem::Dependency]
362
- # @param deps [Set<Gem::Specification>]
363
- only_runtime_dependencies(gemspec).each_with_object(Set.new) do |spec, deps|
364
- Solargraph.logger.info "Adding #{spec.name} dependency for #{gemspec.name}"
365
- dep = Gem.loaded_specs[spec.name]
366
- # @todo is next line necessary?
367
- # @sg-ignore Unresolved call to requirement on Gem::Dependency
368
- dep ||= Gem::Specification.find_by_name(spec.name, spec.requirement)
369
- deps.merge fetch_dependencies(dep) if deps.add?(dep)
370
- rescue Gem::MissingSpecError
371
- # @sg-ignore Unresolved call to requirement on Gem::Dependency
372
- Solargraph.logger.warn "Gem dependency #{spec.name} #{spec.requirement} for #{gemspec.name} not found in RubyGems."
373
- end.to_a
374
- end
375
-
376
- # @param gemspec [Gem::Specification]
377
- # @return [Array<Gem::Dependency>]
378
- def only_runtime_dependencies gemspec
379
- gemspec.dependencies - gemspec.development_dependencies
380
- end
381
-
382
-
383
- def inspect
384
- self.class.inspect
385
- end
386
-
387
- # @return [Array<Gem::Specification>, nil]
388
- def gemspecs_required_from_bundler
389
- # @todo Handle projects with custom Bundler/Gemfile setups
390
- return unless workspace.gemfile?
391
-
392
- if workspace.gemfile? && Bundler.definition&.lockfile&.to_s&.start_with?(workspace.directory)
393
- # Find only the gems bundler is now using
394
- Bundler.definition.locked_gems.specs.flat_map do |lazy_spec|
395
- logger.info "Handling #{lazy_spec.name}:#{lazy_spec.version}"
396
- [Gem::Specification.find_by_name(lazy_spec.name, lazy_spec.version)]
397
- rescue Gem::MissingSpecError => e
398
- logger.info("Could not find #{lazy_spec.name}:#{lazy_spec.version} with find_by_name, falling back to guess")
399
- # can happen in local filesystem references
400
- specs = resolve_path_to_gemspecs lazy_spec.name
401
- logger.warn "Gem #{lazy_spec.name} #{lazy_spec.version} from bundle not found: #{e}" if specs.nil?
402
- next specs
403
- end.compact
404
- else
405
- logger.info 'Fetching gemspecs required from Bundler (bundler/require)'
406
- gemspecs_required_from_external_bundle
407
- end
408
- end
409
-
410
- # @return [Array<Gem::Specification>, nil]
411
- def gemspecs_required_from_external_bundle
412
- logger.info 'Fetching gemspecs required from external bundle'
413
- return [] unless workspace&.directory
414
-
415
- Solargraph.with_clean_env do
416
- cmd = [
417
- 'ruby', '-e',
418
- "require 'bundler'; require 'json'; Dir.chdir('#{workspace&.directory}') { puts Bundler.definition.locked_gems.specs.map { |spec| [spec.name, spec.version] }.to_h.to_json }"
419
- ]
420
- o, e, s = Open3.capture3(*cmd)
421
- if s.success?
422
- Solargraph.logger.debug "External bundle: #{o}"
423
- hash = o && !o.empty? ? JSON.parse(o.split("\n").last) : {}
424
- hash.flat_map do |name, version|
425
- Gem::Specification.find_by_name(name, version)
426
- rescue Gem::MissingSpecError => e
427
- logger.info("Could not find #{name}:#{version} with find_by_name, falling back to guess")
428
- # can happen in local filesystem references
429
- specs = resolve_path_to_gemspecs name
430
- logger.warn "Gem #{name} #{version} from bundle not found: #{e}" if specs.nil?
431
- next specs
432
- end.compact
433
- else
434
- Solargraph.logger.warn "Failed to load gems from bundle at #{workspace&.directory}: #{e}"
435
- end
436
- end
437
- end
438
- end
439
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'benchmark'
5
+ require 'open3'
6
+
7
+ module Solargraph
8
+ # A collection of pins generated from required gems.
9
+ #
10
+ class DocMap
11
+ include Logging
12
+
13
+ # @return [Array<String>]
14
+ attr_reader :requires
15
+ alias required requires
16
+
17
+ # @return [Array<Gem::Specification>]
18
+ attr_reader :preferences
19
+
20
+ # @return [Array<Pin::Base>]
21
+ attr_reader :pins
22
+
23
+ # @return [Array<Gem::Specification>]
24
+ def uncached_gemspecs
25
+ uncached_yard_gemspecs.concat(uncached_rbs_collection_gemspecs)
26
+ .sort
27
+ .uniq { |gemspec| "#{gemspec.name}:#{gemspec.version}" }
28
+ end
29
+
30
+ # @return [Array<Gem::Specification>]
31
+ attr_reader :uncached_yard_gemspecs
32
+
33
+ # @return [Array<Gem::Specification>]
34
+ attr_reader :uncached_rbs_collection_gemspecs
35
+
36
+ # @return [String, nil]
37
+ attr_reader :rbs_collection_path
38
+
39
+ # @return [String, nil]
40
+ attr_reader :rbs_collection_config_path
41
+
42
+ # @return [Workspace, nil]
43
+ attr_reader :workspace
44
+
45
+ # @return [Environ]
46
+ attr_reader :environ
47
+
48
+ # @param requires [Array<String>]
49
+ # @param workspace [Workspace, nil]
50
+ # @param [Object] out
51
+ def initialize requires, workspace, out: $stderr
52
+ @requires = requires.compact
53
+ @workspace = workspace
54
+ @rbs_collection_path = workspace&.rbs_collection_path
55
+ @rbs_collection_config_path = workspace&.rbs_collection_config_path
56
+ @environ = Convention.for_global(self)
57
+ @requires.concat @environ.requires if @environ
58
+ load_serialized_gem_pins
59
+ pins.concat @environ.pins
60
+ @out = out
61
+ end
62
+
63
+ # @param out [IO, StringIO, nil]
64
+ # @return [void]
65
+ # @param [Boolean] rebuild
66
+ def cache_all! out, rebuild: false
67
+ # if we log at debug level:
68
+ if logger.info?
69
+ gem_desc = uncached_gemspecs.map { |gemspec| "#{gemspec.name}:#{gemspec.version}" }.join(', ')
70
+ logger.info "Caching pins for gems: #{gem_desc}" unless uncached_gemspecs.empty?
71
+ end
72
+ logger.debug { "Caching for YARD: #{uncached_yard_gemspecs.map(&:name)}" }
73
+ logger.debug { "Caching for RBS collection: #{uncached_rbs_collection_gemspecs.map(&:name)}" }
74
+ load_serialized_gem_pins
75
+ uncached_gemspecs.each do |gemspec|
76
+ cache(gemspec, rebuild: rebuild, out: out)
77
+ end
78
+ load_serialized_gem_pins
79
+ @uncached_rbs_collection_gemspecs = []
80
+ @uncached_yard_gemspecs = []
81
+ end
82
+
83
+ # @param gemspec [Gem::Specification]
84
+ # @param out [IO, StringIO, nil]
85
+ # @return [void]
86
+ def cache_yard_pins gemspec, out
87
+ pins = GemPins.build_yard_pins(yard_plugins, gemspec)
88
+ PinCache.serialize_yard_gem(gemspec, pins)
89
+ logger.info { "Cached #{pins.length} YARD pins for gem #{gemspec.name}:#{gemspec.version}" } unless pins.empty?
90
+ end
91
+
92
+ # @param gemspec [Gem::Specification]
93
+ # @param out [IO, StringIO, nil]
94
+ # @return [void]
95
+ def cache_rbs_collection_pins gemspec, out
96
+ rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
97
+ pins = rbs_map.pins
98
+ rbs_version_cache_key = rbs_map.cache_key
99
+ # cache pins even if result is zero, so we don't retry building pins
100
+ pins ||= []
101
+ PinCache.serialize_rbs_collection_gem(gemspec, rbs_version_cache_key, pins)
102
+ logger.info { "Cached #{pins.length} RBS collection pins for gem #{gemspec.name} #{gemspec.version} with cache_key #{rbs_version_cache_key.inspect}" unless pins.empty? }
103
+ end
104
+
105
+ # @param gemspec [Gem::Specification]
106
+ # @param rebuild [Boolean] whether to rebuild the pins even if they are cached
107
+ # @param out [IO, StringIO, nil] output stream for logging
108
+ # @return [void]
109
+ def cache gemspec, rebuild: false, out: nil
110
+ build_yard = uncached_yard_gemspecs.include?(gemspec) || rebuild
111
+ build_rbs_collection = uncached_rbs_collection_gemspecs.include?(gemspec) || rebuild
112
+ if build_yard || build_rbs_collection
113
+ type = []
114
+ type << 'YARD' if build_yard
115
+ type << 'RBS collection' if build_rbs_collection
116
+ out&.puts("Caching #{type.join(' and ')} pins for gem #{gemspec.name}:#{gemspec.version}")
117
+ end
118
+ cache_yard_pins(gemspec, out) if build_yard
119
+ cache_rbs_collection_pins(gemspec, out) if build_rbs_collection
120
+ end
121
+
122
+ # @return [Array<Gem::Specification>]
123
+ def gemspecs
124
+ @gemspecs ||= required_gems_map.values.compact.flatten
125
+ end
126
+
127
+ # @return [Array<String>]
128
+ def unresolved_requires
129
+ @unresolved_requires ||= required_gems_map.select { |_, gemspecs| gemspecs.nil? }.keys
130
+ end
131
+
132
+ # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
133
+ def self.all_yard_gems_in_memory
134
+ @all_yard_gems_in_memory ||= {}
135
+ end
136
+
137
+ # @return [Hash{String => Hash{Array(String, String) => Array<Pin::Base>}}] stored by RBS collection path
138
+ def self.all_rbs_collection_gems_in_memory
139
+ @all_rbs_collection_gems_in_memory ||= {}
140
+ end
141
+
142
+ # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
143
+ def yard_pins_in_memory
144
+ self.class.all_yard_gems_in_memory
145
+ end
146
+
147
+ # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
148
+ def rbs_collection_pins_in_memory
149
+ # @sg-ignore rbs_collection_path is String | nil but used as hash key
150
+ self.class.all_rbs_collection_gems_in_memory[rbs_collection_path] ||= {}
151
+ end
152
+
153
+ # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
154
+ def self.all_combined_pins_in_memory
155
+ @all_combined_pins_in_memory ||= {}
156
+ end
157
+
158
+ # @todo this should also include an index by the hash of the RBS collection
159
+ # @return [Hash{Array(String, String) => Array<Pin::Base>}] Indexed by gemspec name and version
160
+ def combined_pins_in_memory
161
+ self.class.all_combined_pins_in_memory
162
+ end
163
+
164
+ # @return [Array<String>]
165
+ def yard_plugins
166
+ @environ.yard_plugins
167
+ end
168
+
169
+ # @return [Set<Gem::Specification>]
170
+ def dependencies
171
+ @dependencies ||= (gemspecs.flat_map { |spec| fetch_dependencies(spec) } - gemspecs).to_set
172
+ end
173
+
174
+ private
175
+
176
+ # @return [void]
177
+ def load_serialized_gem_pins
178
+ @pins = []
179
+ @uncached_yard_gemspecs = []
180
+ @uncached_rbs_collection_gemspecs = []
181
+ with_gemspecs, without_gemspecs = required_gems_map.partition { |_, v| v }
182
+ # @type [Array<String>]
183
+ paths = without_gemspecs.to_h.keys
184
+ # @type [Array<Gem::Specification>]
185
+ gemspecs = with_gemspecs.to_h.values.flatten.compact + dependencies.to_a
186
+
187
+ paths.each do |path|
188
+ deserialize_stdlib_rbs_map path
189
+ end
190
+
191
+ logger.debug { 'DocMap#load_serialized_gem_pins: Combining pins...' }
192
+ time = Benchmark.measure do
193
+ gemspecs.each do |gemspec|
194
+ pins = deserialize_combined_pin_cache gemspec
195
+ @pins.concat pins if pins
196
+ end
197
+ end
198
+ logger.info { "DocMap#load_serialized_gem_pins: Loaded and processed serialized pins together in #{time.real} seconds" }
199
+ @uncached_yard_gemspecs.uniq!
200
+ @uncached_rbs_collection_gemspecs.uniq!
201
+ nil
202
+ end
203
+
204
+ # @return [Hash{String => Array<Gem::Specification>}]
205
+ def required_gems_map
206
+ @required_gems_map ||= requires.to_h { |path| [path, resolve_path_to_gemspecs(path)] }
207
+ end
208
+
209
+ # @return [Hash{String => Gem::Specification}]
210
+ def preference_map
211
+ @preference_map ||= preferences.to_h { |gemspec| [gemspec.name, gemspec] }
212
+ end
213
+
214
+ # @param gemspec [Gem::Specification]
215
+ # @return [Array<Pin::Base>, nil]
216
+ def deserialize_yard_pin_cache gemspec
217
+ if yard_pins_in_memory.key?([gemspec.name, gemspec.version])
218
+ return yard_pins_in_memory[[gemspec.name, gemspec.version]]
219
+ end
220
+
221
+ cached = PinCache.deserialize_yard_gem(gemspec)
222
+ if cached
223
+ logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
224
+ yard_pins_in_memory[[gemspec.name, gemspec.version]] = cached
225
+ cached
226
+ else
227
+ logger.debug "No YARD pin cache for #{gemspec.name}:#{gemspec.version}"
228
+ @uncached_yard_gemspecs.push gemspec
229
+ nil
230
+ end
231
+ end
232
+
233
+ # @param gemspec [Gem::Specification]
234
+ # @return [void]
235
+ def deserialize_combined_pin_cache gemspec
236
+ unless combined_pins_in_memory[[gemspec.name, gemspec.version]].nil?
237
+ return combined_pins_in_memory[[gemspec.name, gemspec.version]]
238
+ end
239
+
240
+ rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
241
+ rbs_version_cache_key = rbs_map.cache_key
242
+
243
+ cached = PinCache.deserialize_combined_gem(gemspec, rbs_version_cache_key)
244
+ if cached
245
+ logger.info { "Loaded #{cached.length} cached YARD pins from #{gemspec.name}:#{gemspec.version}" }
246
+ combined_pins_in_memory[[gemspec.name, gemspec.version]] = cached
247
+ return combined_pins_in_memory[[gemspec.name, gemspec.version]]
248
+ end
249
+
250
+ rbs_collection_pins = deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
251
+
252
+ yard_pins = deserialize_yard_pin_cache gemspec
253
+
254
+ if !rbs_collection_pins.nil? && !yard_pins.nil?
255
+ logger.debug { "Combining pins for #{gemspec.name}:#{gemspec.version}" }
256
+ combined_pins = GemPins.combine(yard_pins, rbs_collection_pins)
257
+ PinCache.serialize_combined_gem(gemspec, rbs_version_cache_key, combined_pins)
258
+ combined_pins_in_memory[[gemspec.name, gemspec.version]] = combined_pins
259
+ logger.info { "Generated #{combined_pins_in_memory[[gemspec.name, gemspec.version]].length} combined pins for #{gemspec.name} #{gemspec.version}" }
260
+ return combined_pins
261
+ end
262
+
263
+ if !yard_pins.nil?
264
+ logger.debug { "Using only YARD pins for #{gemspec.name}:#{gemspec.version}" }
265
+ combined_pins_in_memory[[gemspec.name, gemspec.version]] = yard_pins
266
+ combined_pins_in_memory[[gemspec.name, gemspec.version]]
267
+ elsif !rbs_collection_pins.nil?
268
+ logger.debug { "Using only RBS collection pins for #{gemspec.name}:#{gemspec.version}" }
269
+ combined_pins_in_memory[[gemspec.name, gemspec.version]] = rbs_collection_pins
270
+ combined_pins_in_memory[[gemspec.name, gemspec.version]]
271
+ else
272
+ logger.debug { "Pins not yet cached for #{gemspec.name}:#{gemspec.version}" }
273
+ nil
274
+ end
275
+ end
276
+
277
+ # @param path [String] require path that might be in the RBS stdlib collection
278
+ # @return [void]
279
+ def deserialize_stdlib_rbs_map path
280
+ map = RbsMap::StdlibMap.load(path)
281
+ if map.resolved?
282
+ logger.debug { "Loading stdlib pins for #{path}" }
283
+ @pins.concat map.pins
284
+ logger.debug { "Loaded #{map.pins.length} stdlib pins for #{path}" }
285
+ map.pins
286
+ else
287
+ # @todo Temporarily ignoring unresolved `require 'set'`
288
+ logger.debug { "Require path #{path} could not be resolved in RBS" } unless path == 'set'
289
+ nil
290
+ end
291
+ end
292
+
293
+ # @param gemspec [Gem::Specification]
294
+ # @param rbs_version_cache_key [String]
295
+ # @return [Array<Pin::Base>, nil]
296
+ def deserialize_rbs_collection_cache gemspec, rbs_version_cache_key
297
+ return if rbs_collection_pins_in_memory.key?([gemspec, rbs_version_cache_key])
298
+ cached = PinCache.deserialize_rbs_collection_gem(gemspec, rbs_version_cache_key)
299
+ if cached
300
+ logger.info { "Loaded #{cached.length} pins from RBS collection cache for #{gemspec.name}:#{gemspec.version}" } unless cached.empty?
301
+ rbs_collection_pins_in_memory[[gemspec, rbs_version_cache_key]] = cached
302
+ cached
303
+ else
304
+ logger.debug "No RBS collection pin cache for #{gemspec.name} #{gemspec.version}"
305
+ @uncached_rbs_collection_gemspecs.push gemspec
306
+ nil
307
+ end
308
+ end
309
+
310
+ # @param path [String]
311
+ # @return [::Array<Gem::Specification>, nil]
312
+ def resolve_path_to_gemspecs path
313
+ return nil if path.empty?
314
+ return gemspecs_required_from_bundler if path == 'bundler/require'
315
+
316
+ # @type [Gem::Specification, nil]
317
+ gemspec = Gem::Specification.find_by_path(path)
318
+ if gemspec.nil?
319
+ gem_name_guess = path.split('/').first
320
+ begin
321
+ # this can happen when the gem is included via a local path in
322
+ # a Gemfile; Gem doesn't try to index the paths in that case.
323
+ #
324
+ # See if we can make a good guess:
325
+ gemspec = Gem::Specification.find_by_name(gem_name_guess)
326
+ rescue Gem::MissingSpecError
327
+ logger.debug { "Require path #{path} could not be resolved to a gem via find_by_path or guess of #{gem_name_guess}" }
328
+ []
329
+ end
330
+ end
331
+ return nil if gemspec.nil?
332
+ [gemspec_or_preference(gemspec)]
333
+ end
334
+
335
+ # @param gemspec [Gem::Specification]
336
+ # @return [Gem::Specification]
337
+ def gemspec_or_preference gemspec
338
+ # :nocov: dormant feature
339
+ return gemspec unless preference_map.key?(gemspec.name)
340
+ return gemspec if gemspec.version == preference_map[gemspec.name].version
341
+
342
+ change_gemspec_version gemspec, preference_map[gemspec.name].version
343
+ # :nocov:
344
+ end
345
+
346
+ # @param gemspec [Gem::Specification]
347
+ # @param version [Gem::Version, String]
348
+ # @return [Gem::Specification]
349
+ def change_gemspec_version gemspec, version
350
+ Gem::Specification.find_by_name(gemspec.name, "= #{version}")
351
+ rescue Gem::MissingSpecError
352
+ Solargraph.logger.info "Gem #{gemspec.name} version #{version} not found. Using #{gemspec.version} instead"
353
+ gemspec
354
+ end
355
+
356
+ # @param gemspec [Gem::Specification]
357
+ # @return [Array<Gem::Specification>]
358
+ def fetch_dependencies gemspec
359
+ # @param spec [Gem::Dependency]
360
+ # @param deps [Set<Gem::Specification>]
361
+ only_runtime_dependencies(gemspec).each_with_object(Set.new) do |spec, deps|
362
+ Solargraph.logger.info "Adding #{spec.name} dependency for #{gemspec.name}"
363
+ dep = Gem.loaded_specs[spec.name]
364
+ # @todo is next line necessary?
365
+ dep ||= Gem::Specification.find_by_name(spec.name, spec.requirement)
366
+ deps.merge fetch_dependencies(dep) if deps.add?(dep)
367
+ rescue Gem::MissingSpecError
368
+ Solargraph.logger.warn "Gem dependency #{spec.name} for #{gemspec.name} not found in RubyGems."
369
+ end.to_a
370
+ end
371
+
372
+ # @param gemspec [Gem::Specification]
373
+ # @return [Array<Gem::Dependency>]
374
+ def only_runtime_dependencies gemspec
375
+ gemspec.dependencies - gemspec.development_dependencies
376
+ end
377
+
378
+ def inspect
379
+ self.class.inspect
380
+ end
381
+
382
+ # @return [Array<Gem::Specification>, nil]
383
+ def gemspecs_required_from_bundler
384
+ # @todo Handle projects with custom Bundler/Gemfile setups
385
+ return unless workspace&.gemfile?
386
+
387
+ # @sg-ignore workspace is checked for nil above
388
+ if workspace.gemfile? && Bundler.definition&.lockfile&.to_s&.start_with?(workspace.directory) # rubocop:disable Style/SafeNavigationChainLength
389
+ # Find only the gems bundler is now using
390
+ Bundler.definition.locked_gems.specs.flat_map do |lazy_spec|
391
+ logger.info "Handling #{lazy_spec.name}:#{lazy_spec.version}"
392
+ [Gem::Specification.find_by_name(lazy_spec.name, lazy_spec.version)]
393
+ rescue Gem::MissingSpecError => e
394
+ logger.info("Could not find #{lazy_spec.name}:#{lazy_spec.version} with find_by_name, falling back to guess")
395
+ # can happen in local filesystem references
396
+ specs = resolve_path_to_gemspecs lazy_spec.name
397
+ logger.warn "Gem #{lazy_spec.name} #{lazy_spec.version} from bundle not found: #{e}" if specs.nil?
398
+ next specs
399
+ end.compact
400
+ else
401
+ logger.info 'Fetching gemspecs required from Bundler (bundler/require)'
402
+ gemspecs_required_from_external_bundle
403
+ end
404
+ end
405
+
406
+ # @return [Array<Gem::Specification>]
407
+ def gemspecs_required_from_external_bundle
408
+ logger.info 'Fetching gemspecs required from external bundle'
409
+ return [] unless workspace&.directory
410
+
411
+ Solargraph.with_clean_env do
412
+ cmd = [
413
+ 'ruby', '-e',
414
+ # @sg-ignore return above ensures workspace.directory is not nil
415
+ "require 'bundler'; require 'json'; Dir.chdir('#{workspace.directory}') { puts Bundler.definition.locked_gems.specs.map { |spec| [spec.name, spec.version] }.to_h.to_json }"
416
+ ]
417
+ o, e, s = Open3.capture3(*cmd)
418
+ if s.success?
419
+ Solargraph.logger.debug "External bundle: #{o}"
420
+ hash = o && !o.empty? ? JSON.parse(o.split("\n").last) : {}
421
+ hash.flat_map do |name, version|
422
+ Gem::Specification.find_by_name(name, version)
423
+ rescue Gem::MissingSpecError => e
424
+ logger.info("Could not find #{name}:#{version} with find_by_name, falling back to guess")
425
+ # can happen in local filesystem references
426
+ specs = resolve_path_to_gemspecs name
427
+ logger.warn "Gem #{name} #{version} from bundle not found: #{e}" if specs.nil?
428
+ next specs
429
+ end.compact
430
+ else
431
+ # @sg-ignore return above ensures workspace.directory is not nil
432
+ Solargraph.logger.warn "Failed to load gems from bundle at #{workspace.directory}: #{e}"
433
+ []
434
+ end
435
+ end
436
+ end
437
+ end
438
+ end