fix: load entity files through import when require cannot parse them - #12796
Open
Gauravtiwari31 wants to merge 1 commit into
Open
fix: load entity files through import when require cannot parse them#12796Gauravtiwari31 wants to merge 1 commit into
Gauravtiwari31 wants to merge 1 commit into
Conversation
Entities, migrations and subscribers given as file globs are loaded with `require` whenever the nearest package.json is not `"type": "module"`. Vite based runners such as Vitest transform TypeScript for `import()` but leave `require` on the plain CommonJS loader, so requiring a `.ts` file throws `SyntaxError: Invalid or unexpected token` and the data source fails to start. Listing every entity class by hand was the only workaround. Retry with `import()` when `require` fails to parse the file. Runtimes that can already require the file are unaffected, and when the retry does not help the original require error is thrown instead of the less useful one from `import()`, so a genuine syntax error in a user file stays readable. Closes typeorm#11570
Code Review by Qodo
1. Flaky ESM fallback test
|
alumni
self-requested a review
August 26, 2026 07:14
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.
Description of change
Entities, migrations and subscribers passed as file globs (
entities: ["./src/entity/*.ts"]) fail to load under Vitest withSyntaxError: Invalid or unexpected token. Listing every class by hand (entities: [User]) is currently the only workaround, which is reported as still broken in 0.3.25 through 0.3.28.Cause:
importOrRequireFilepicksrequirefor.ts/.jswhenever the nearestpackage.jsonis not"type": "module". Vite based runners transform TypeScript forimport(), but they leaverequirepointing at Node's plain CommonJS loader, which cannot parse TypeScript — so the file blows up before Vitest ever gets to transform it. Underts-node/tsxthis works only because those tools patchrequireitself.Change: when
requirethrows aSyntaxError, retry the file throughimport(). Vite based runners intercept dynamic imports, so the file is transformed and loads normally.Two deliberate details:
SyntaxErrortriggers the retry. A module that requires fine, or that fails for any other reason (a missing dependency, a throwing side effect), behaves exactly as before — this cannot mask a runtime failure as a load failure.requireerror is rethrown, not the one fromimport(). Otherwise a genuine syntax error in a user's entity file would surface on plain Node asERR_UNKNOWN_FILE_EXTENSION ".ts", which says nothing about the actual mistake.moduleTypeis reported as"esm"when the retry succeeds, which is whatConnectionOptionsReaderneeds in order to unwrapdefaultoff the module namespace.Verification. Reproduced the runner's exact conditions — CommonJS loader unable to parse the file,
import()able to — and ran the current and patchedimportOrRequireFileagainst it:Two tests were added to
test/unit/util/import-utils.test.ts. The first stubs the CommonJS loader for one fixture file to throw the way Vitest does and asserts the module still loads as ESM — it fails onmasterand passes with this change. The second asserts the original require error survives when the retry cannot help; it guards the error-quality behaviour above rather than reproducing the bug, so it passes either way by design.Full suite: 3046 passing, 0 failing on the default
sqljsconfig. No documented behaviour changes, so no docs update.Closes #11570
Pull-Request Checklist
masterbranchFixes #NNNN,Closes #NNNN, orResolves #NNNNtests/**.test.ts)docs/docs/**.md) — N/A, bug fix with no API or behaviour change to document