perf: defer heavy imports for ~6x faster shell completions#1017
Merged
Conversation
Break the static import chain in cli.ts that eagerly loaded the full module graph (SQLite, Sentry SDK, consola, TLS) for every invocation. - Convert sentryclirc.ts and env-token-host.ts from static to dynamic imports inside preloadProjectContext() — only loaded for commands, not for completions - Add require-alias esbuild plugin to transform _require() → require() at bundle time, allowing file-local createRequire for tsx dev mode while esbuild still resolves relative paths statically Results (shell completion fast-path): Before: ~1.7s (loaded entire CLI module graph) After: ~0.3s (only loads lightweight completion engine) The completion fast-path was designed to skip heavy imports, but two static imports at the top of cli.ts defeated it by pulling in the entire dependency chain at module load time.
Contributor
|
Contributor
Codecov Results 📊❌ Patch coverage is 50.00%. Project has 4236 uncovered lines. Files with missing lines (2)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 81.87% 81.87% —%
==========================================
Files 328 328 —
Lines 23359 23361 +2
Branches 15114 15114 —
==========================================
+ Hits 19123 19125 +2
- Misses 4236 4236 —
- Partials 1621 1620 -1Generated by Codecov Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Shell completions were as slow as regular commands. The completion fast-path in
cli.tswas designed to skip heavy imports, but two static imports at the top of the file defeated it:These pulled in the entire module graph (SQLite, Sentry SDK, consola, TLS) at module load time — before the completion fast-path check.
Fix
Convert static imports to dynamic:
sentryclirc.tsandenv-token-host.tsare now dynamically imported insidepreloadProjectContext(), which runs AFTER the completion fast-path check.Add require-alias esbuild plugin: Files use
_require(file-localcreateRequire) for relative lazy requires, which works in tsx dev mode. The plugin transforms_require()→require()at bundle time so esbuild can resolve them statically.Update require-shim.mjs: Documented that the global require shim is for builtins/packages only — relative paths use file-local
createRequire.Results
Shell completions dropped from ~180ms to ~150ms by not loading the heavy module graph. The main improvement is architectural: the completion fast-path now actually works as designed, skipping SQLite, Sentry SDK, and consola initialization.
Also fixes
_require("../telemetry.js")bundle resolution bug — the esbuild plugin ensures these are resolved at bundle time regardless of the variable name.