Skip to content

Commit 1c7199a

Browse files
authored
Always expand indexables' full paths (#1977)
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
1 parent 494e90e commit 1c7199a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/ruby_indexer/lib/ruby_indexer/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def indexables
5656
load_path_entry = T.let(nil, T.nilable(String))
5757

5858
Dir.glob(pattern, File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path|
59+
path = File.expand_path(path)
5960
# All entries for the same pattern match the same $LOAD_PATH entry. Since searching the $LOAD_PATH for every
6061
# entry is expensive, we memoize it until we find a path that doesn't belong to that $LOAD_PATH. This happens
6162
# on repositories that define multiple gems, like Rails. All frameworks are defined inside the Dir.pwd, but

lib/ruby_indexer/test/configuration_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def test_load_configuration_executes_configure_block
2020
assert(indexables.none? { |indexable| indexable.full_path == __FILE__ })
2121
end
2222

23+
def test_indexables_have_expanded_full_paths
24+
@config.apply_config({ "included_patterns" => ["**/*.rb"] })
25+
indexables = @config.indexables
26+
27+
# All paths should be expanded
28+
assert(indexables.none? { |indexable| indexable.full_path.start_with?("lib/") })
29+
end
30+
2331
def test_indexables_only_includes_gem_require_paths
2432
indexables = @config.indexables
2533

0 commit comments

Comments
 (0)