From f8fe95052a5821ce9371a56ddf00b7b7a63756e3 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 25 Apr 2024 21:30:48 +0100 Subject: [PATCH] Always expand indexables' full paths Depends on the `included_pattern`'s value, `Dir.glob` may return relative paths as its default base is the current working directory. For example, if the `included_pattern` is `**/*.rb`, the `Dir.glob` will return relative paths like `lib/foo.rb` instead of the full path like `/path/to/project/lib/foo.rb`. This commit ensures that the full paths are always expanded by using `File.expand_path` on the paths returned by `Dir.glob`. Fixes #1971 --- lib/ruby_indexer/lib/ruby_indexer/configuration.rb | 1 + lib/ruby_indexer/test/configuration_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb index 34d5e5c80..a3377fbf8 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb @@ -56,6 +56,7 @@ def indexables load_path_entry = T.let(nil, T.nilable(String)) Dir.glob(pattern, File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path| + path = File.expand_path(path) # All entries for the same pattern match the same $LOAD_PATH entry. Since searching the $LOAD_PATH for every # entry is expensive, we memoize it until we find a path that doesn't belong to that $LOAD_PATH. This happens # on repositories that define multiple gems, like Rails. All frameworks are defined inside the Dir.pwd, but diff --git a/lib/ruby_indexer/test/configuration_test.rb b/lib/ruby_indexer/test/configuration_test.rb index 09a342815..e7e774e6e 100644 --- a/lib/ruby_indexer/test/configuration_test.rb +++ b/lib/ruby_indexer/test/configuration_test.rb @@ -20,6 +20,14 @@ def test_load_configuration_executes_configure_block assert(indexables.none? { |indexable| indexable.full_path == __FILE__ }) end + def test_indexables_have_expanded_full_paths + @config.apply_config({ "included_patterns" => ["**/*.rb"] }) + indexables = @config.indexables + + # All paths should be expanded + assert(indexables.none? { |indexable| indexable.full_path.start_with?("lib/") }) + end + def test_indexables_only_includes_gem_require_paths indexables = @config.indexables