Skip to content

Commit

Permalink
Always expand indexables' full paths (#1977)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
st0012 authored Apr 26, 2024
1 parent 494e90e commit 1c7199a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/ruby_indexer/test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1c7199a

Please sign in to comment.