Skip to content

Commit 150f834

Browse files
authored
Prevent full indexing (index_all) from being called multiple times (#2864)
Prevent `index_all` being called multiple times
1 parent 50daa53 commit 150f834

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/ruby_indexer/lib/ruby_indexer/index.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Index
77

88
class UnresolvableAliasError < StandardError; end
99
class NonExistingNamespaceError < StandardError; end
10+
class IndexNotEmptyError < StandardError; end
1011

1112
# The minimum Jaro-Winkler similarity score for an entry to be considered a match for a given fuzzy search query
1213
ENTRY_SIMILARITY_THRESHOLD = 0.7
@@ -360,6 +361,15 @@ def resolve(name, nesting, seen_names = [])
360361
).void
361362
end
362363
def index_all(indexable_paths: @configuration.indexables, &block)
364+
# When troubleshooting an indexing issue, e.g. through irb, it's not obvious that `index_all` will augment the
365+
# existing index values, meaning it may contain 'stale' entries. This check ensures that the user is aware of this
366+
# behavior and can take appropriate action.
367+
# binding.break
368+
if @entries.any?
369+
raise IndexNotEmptyError,
370+
"The index is not empty. To prevent invalid entries, `index_all` can only be called once."
371+
end
372+
363373
RBSIndexer.new(self).index_ruby_core
364374
# Calculate how many paths are worth 1% of progress
365375
progress_step = (indexable_paths.length / 100.0).ceil

lib/ruby_indexer/test/index_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,5 +2023,12 @@ def test_build_non_redundant_name
20232023
),
20242024
)
20252025
end
2026+
2027+
def test_prevents_multiple_calls_to_index_all
2028+
# For this test class, `index_all` is already called once in `setup`.
2029+
assert_raises(Index::IndexNotEmptyError) do
2030+
@index.index_all
2031+
end
2032+
end
20262033
end
20272034
end

0 commit comments

Comments
 (0)