diff --git a/lib/rubydex/graph.rb b/lib/rubydex/graph.rb index 672f9ae9..cf4d35c1 100644 --- a/lib/rubydex/graph.rb +++ b/lib/rubydex/graph.rb @@ -45,6 +45,7 @@ def workspace_paths end add_workspace_dependency_paths(paths) + add_core_rbs_definition_paths(paths) paths.uniq! paths end @@ -71,5 +72,21 @@ def add_workspace_dependency_paths(paths) nil end end + + # Searches for the latest installation of the `rbs` gem and adds the paths for the core and stdlib RBS definitions + # to the list of paths. This method does not require `rbs` to be a part of the bundle. It searches for whatever + # latest installation of `rbs` exists in the system and fails silently if we can't find one + # + #: (Array[String]) -> void + def add_core_rbs_definition_paths(paths) + rbs_gem_path = Gem.path + .flat_map { |path| Dir.glob(File.join(path, "gems", "rbs-[0-9]*/")) } + .max_by { |path| Gem::Version.new(File.basename(path).delete_prefix("rbs-")) } + + return unless rbs_gem_path + + paths << File.join(rbs_gem_path, "core") + paths << File.join(rbs_gem_path, "stdlib") + end end end diff --git a/test/graph_test.rb b/test/graph_test.rb index 9570e7c2..43538756 100644 --- a/test/graph_test.rb +++ b/test/graph_test.rb @@ -560,6 +560,23 @@ def test_workspace_paths end end + def test_index_workspace_includes_rbs_core_definitions + # Ensure the `rbs` gem is installed on CI + Gem.install("rbs") if ENV["CI"] + + graph = Rubydex::Graph.new + graph.index_workspace + graph.resolve + + ["Kernel", "Object", "BasicObject", "Integer"].each do |core_namespace| + rbs_kernel = graph[core_namespace].definitions.find do |definition| + uri = URI(definition.location.uri) + File.extname(uri.path) == ".rbs" + end + assert(rbs_kernel, "Expected to find RBS definition for `#{core_namespace}` in the graph") + end + end + private def assert_diagnostics(expected, actual)