Repository navigation
Support tsconfig path aliases in migration validation - #7
Merged
Merged
Conversation
…1.4.0
validate-migration-directory previously imported each migration module with
a bare dynamic import(), so migrations importing through tsconfig
compilerOptions.paths aliases (e.g. `import { sql } from "@/sql"`) failed
with "Cannot find package '@/sql'", even though build-db-migrations
resolved the same imports fine.
Validation now discovers the nearest tsconfig.json declaring
compilerOptions.paths (walking up from the migrations directory, parsing
JSONC and following extends chains) and applies those aliases while
importing migration modules:
- Node.js (the runtime the published CLI's shebang selects under bunx/npx):
a module.registerHooks() resolve hook (Node >= 22.15) maps alias
specifiers to their target files, and also recovers bundler-style
extensionless / .js->.ts relative imports in the consumer's files.
- Bun: alias specifiers found by scanning the migrations' static import
graph with Bun.Transpiler are registered as virtual modules, which also
covers tsconfigs Bun's built-in paths support rejects (paths without
baseUrl and non-"./" targets).
- Hooks only rewrite imports made by files under the consumer project or
the migration directory, and only while validation is running.
The CLI gains a --tsconfig <path> override, prints which tsconfig supplied
the aliases, reports a warning on runtimes without hook support, and hints
at --tsconfig when a bare import fails and no tsconfig was discovered.
validateMigrationDirectory() accepts a tsconfigPath option and reports the
tsconfig it used in its result.
Also updates the repository to Bun 1.4.0 (packageManager, bun-types,
CI workflow, e2e docker images, docs). Lint, typecheck, and the unit
suite pass under both Bun 1.3.x and 1.4.0; a new test validates the
bundled CLI end-to-end under Node.js against a consumer project shaped
like the bug report.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvN4yt72bDNyFvUo8TS3z5
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VvN4yt72bDNyFvUo8TS3z5
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.
Summary
Adds support for resolving TypeScript
compilerOptions.pathsaliases (e.g.,@/sql→src/sql.ts) when validating and importing migration modules. This allows consumers to use the same import aliases in their migrations as they do in their application code.Key Changes
New module
src/utils/tsconfigPathAliases.ts: Implements tsconfig.json parsing and path alias resolutionbaseUrlandcompilerOptions.pathsextendschains to inherit aliases from parent configs.ts,.tsx,.js, etc.)@/sql.js→sql.ts)New module
src/utils/tsconfigAliasImportHooks.ts: Integrates alias resolution with Node.js and Bun runtimesmodule.registerHooks()to install a synchronous ESM resolve hookUpdated
src/utils/validateMigrationDirectory.ts:tsconfigPathoption to explicitly specify a tsconfigUpdated
src/cli.ts: Adds--tsconfig <path>flag to thevalidate-migration-directorycommandTest fixtures: Added three fixture projects demonstrating alias usage:
alias-project: Standard setup withbaseUrland both exact and wildcard aliasesnobase-project: Tsconfig withoutbaseUrl(paths relative to config file) — exercises Bun's virtual module fallbackextends-chain: Demonstrates alias inheritance throughextendsComprehensive test coverage: Tests for JSONC parsing,
extendschain resolution, exact/wildcard alias matching, extension probing, and end-to-end validation with real migration filesNotable Implementation Details
.js→.tsswaps for ESM compatibilityBun.Transpiler.scanImports()to statically discover alias usage in the import graph, avoiding the need for runtime hookshttps://claude.ai/code/session_01VvN4yt72bDNyFvUo8TS3z5