Skip to content

fix(orama): break the search circular dependency behind Metro/Expo failure (#961)#1026

Merged
thatjuan merged 1 commit into
mainfrom
fix/orama-search-circular-dep
Jun 27, 2026
Merged

fix(orama): break the search circular dependency behind Metro/Expo failure (#961)#1026
thatjuan merged 1 commit into
mainfrom
fix/orama-search-circular-dep

Conversation

@thatjuan

Copy link
Copy Markdown
Contributor

Summary

Root-cause fix for #961 (fetchDocuments is not a function under Metro/Expo). Supersedes #963, which worked around the same cycle by shipping an esbuild-bundled artifact.

The bug

methods/search.ts was overloaded — it held both:

  • the search() dispatcher, which imports fullTextSearch / searchVector / hybridSearch, and
  • the fetchDocuments / fetchDocumentsWithDistinct helpers, which those same per-mode modules import back.

That mutual import (search.ts ⇄ search-fulltext.ts, search.ts ⇄ search-hybrid.ts) is a circular dependency. It's harmless under Node and most bundlers, but fatal under Metro/Expo with experimentalImportSupport (the new tree-shaking), where the binding is read before initialization → fetchDocuments is not a function.

The fix

Move the two pure helpers into a new methods/fetch-documents.ts (they depend only on internal-document-id-store + getNested — no back-edge), and import them from there in search-fulltext.ts / search-hybrid.ts. search.ts keeps only the dispatcher.

  • search-fulltext / search-hybrid no longer import search.ts → the cycle is gone.
  • Fixes it for every bundler, not just Metro — no opt-in bundle export condition, no second artifact, no esbuild added to the build.
  • It also sidesteps the maintainer's stated reason for not bundling (ESM↔CJS pain, noted in Circular dependencies / require cycle #961) — this is a pure source move, no bundler involved.

Why this instead of #963

#963 (the maintainer's own suggestion) fixes the symptom by adding a bundle export condition + an esbuild postbuild that ships pre-bundled dist/bundle/*. It works, but: consumers must configure their resolver to prefer the bundle condition, it ships a parallel minified copy of the library, adds esbuild to the core build, and masks cycles rather than removing them. This PR removes the cycle at the source instead.

Validation

  • pnpm build — 18/18 packages green.
  • pnpm --filter @orama/orama test1326 pass / 0 fail / 3 skip (40/40 suites). Pure code move, no behavior change.
  • Public API unchanged — fetchDocuments/fetchDocumentsWithDistinct were never exported (only search is).

Verification still needed

This removes the specific cycle behind the reported error. @isaachinman — you have the Metro/Expo repro (orama-require-cycle-reproduction) and kindly offered to verify; could you confirm bun run web is clean against this branch? If Metro surfaces a second cycle once this one's gone, I'll extract that too. Keeping #963 open as a fallback until the repro confirms.

Addresses #961.

methods/search.ts held both the search() dispatcher (which imports the per-mode
search modules) and the fetchDocuments/fetchDocumentsWithDistinct helpers those
same modules import back — a circular dependency. Harmless under most bundlers,
but fatal under Metro/Expo with experimentalImportSupport (tree-shaking), where
it surfaces as 'fetchDocuments is not a function' (#961).

Extract the two helpers into methods/fetch-documents.ts and import them from
there in search-fulltext.ts / search-hybrid.ts. search.ts no longer has a
back-edge, so the cycle is eliminated for every bundler — no extra build step
or shipped bundle artifact. Pure code move, no behavioral change (1326 orama
tests pass).

Addresses #961. Supersedes #963, which worked around the cycle with an esbuild bundle.
@thatjuan thatjuan merged commit d6b7afd into main Jun 27, 2026
2 checks passed
@thatjuan thatjuan deleted the fix/orama-search-circular-dep branch June 27, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant