Repository navigation
fix(core): resolve helper imports against the loader, not the user file - #803
Conversation
Emitted @oxc-node/core/helpers/* specifiers resolved relative to the transformed file, so a global oxnode or a script outside a project failed with ERR_MODULE_NOT_FOUND on the first class field (issue #794). ESM: the resolve hooks resolve helper specifiers with parentURL = import.meta.url. CJS: Module._resolveFilename resolves them via a createRequire bound to this package — require() never reaches module.register() hooks, and registerHooks() funnels require through _resolveFilename, making it the single point that covers every runtime. Closes #794
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9fc1b7cc66
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Two copies of the loader can be registered in one process — NODE_OPTIONS preloading one version while a global oxnode adds another. The previous commit forwarded the helper specifier down the hook chain, where an earlier-registered copy's resolve hook re-claimed it and rewrote parentURL to its own module; the nested Module._resolveFilename wrappers did the same for require(). Helpers could come from a different copy than the transform that emitted them. - resolve hooks now compute the file URL with createRequire(import.meta.url) .resolve() — Node's own resolver, no hook chain — and pass only the URL down via nextResolve, so format detection still runs. - the _resolveFilename re-entrancy flag moves to Symbol.for so the inner resolve falls through every copy's wrapper; the outermost (last-registered) copy owns the resolution, matching the copy whose transform hooks wrapped the importing file. - regression test: two dereferenced copies with marked helpers, registered in order; both the CJS and ESM helper must come from the second copy.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4227e46992
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The WASI job has no .node binding in packages/core — its oxc-node.wasi.cjs needs @oxc-node/core-wasm32-wasi and @napi-rs/wasm-runtime from the workspace store, which a bare copy of the directory lacks. Gate on the native binding's presence, matching the addon tests in register-hooks.
The registry approach from 4227e46 still had one shared specifier — `@oxc-node/core/helpers/x` — so when two copies of different versions were registered, every helper resolved to whichever copy owned the global slot, which could lack a helper the emitter used or carry different semantics. Each copy now derives a tag from its own module URL (`register.mjs` in the main thread, `esm.mjs` on module.register's worker) and passes `@oxc-node/core@<tag>` as the transformer's helper module name, so emitted specifiers name their emitter. A `Symbol.for` registry maps tag → that copy's `createRequire` resolver, shared by the `_resolveFilename` patch and the `resolve` hooks: - tagged specifier → its owner's resolver; unknown tag falls through to Node's own resolution - untagged specifier — emitted by older published copies — tries every registered resolver, newest registration first `OxcTransformer` and `load` take an optional `helperModuleName`; the public `transform`/`transformAsync` keep emitting the bare specifier. The dual-copy test now proves routing, not just ownership: each tagged specifier resolves into its own copy's `src/helpers/`, untagged resolves newest-first, and a transformed file's emitted helper reports the first-registered copy that actually transformed the raw source (pirates chains `_compile`, module.register chains `nextLoad`).
…gged helpers Round-3 review pointed out that an untagged `@oxc-node/core/helpers/x` — emitted by an older registered copy (whose own hook already serves it from itself on the ESM path) or by the public `transform()` API inside a project that has its own `@oxc-node/core` — should keep the resolution a plain `require`/`import` would get, with registered copies only as the fallback for loaders running where no copy is in scope. - `_resolveFilename` patch: untagged specifiers attempt the captured native resolution first; only on failure does the registry try each copy, newest first. A failed fallback re-throws `MODULE_NOT_FOUND`. - resolve hooks: untagged specifiers go to `nextResolve` unchanged (the esm.mjs worker path awaits a rejection, the sync registerHooks path catches a throw), then fall back to the registry. - Test scrub: vitest exports the workspace node_modules via NODE_PATH, which legitimately resolved the untagged specifier to the repo's copy — NODE_PATH is now stripped like NODE_OPTIONS.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58d76ed653
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…ged helper A bare `new Error` loses the `code` and `requireStack` callers use to detect optional modules; re-throw the error from Node.js' own resolution.
Summary
Helper imports emitted by the transformer (
import/require("@oxc-node/core/helpers/*")) resolved relative to the transformed file, so any file without@oxc-node/corein its own resolution scope — a globaloxnode, ornode --import <abs-path>/register.mjson a script outside a project — failed withERR_MODULE_NOT_FOUNDon the first class field (issue #794).The fix resolves helper specifiers against the loader's own copy, which is versioned with the emitted code:
register.mjson the main thread,esm.mjsonmodule.register's worker) and passes@oxc-node/core@<tag>as the transformer'shelper_loader.module_name, so emitted specifiers (@oxc-node/core@<tag>/helpers/x) name the copy that emitted themSymbol.for-keyed map onglobalThisrecords each registered copy'screateRequireresolver under its tag — populated per realm byregister.mjs(main thread,require()andregisterHooks) andesm.mjs(themodule.registerworker). A tagged specifier resolves through exactly its owner's resolver, which never consults the hook chain; the resolved file URL is then passed tonextResolveso format detection and foreign hooks still runregister.mjs):require()never reachesmodule.register()hooks andregisterHooks()forwards it to Node's own CommonJS resolution, which funnels throughModule._resolveFilename— patched to consult the same registry, with a process-wide (Symbol.for) re-entrancy flag so inner lookups fall through every copy's wrapperNODE_OPTIONSpreload + globaloxnode, project + global install), and chained hooks transform each file more than once — on both paths the first-registered copy transforms the raw source. Tagging makes helper resolution independent of registration order and version skewnode_modulesstill resolves/fails exactly as before)@oxc-node/core— deliberate, since helper versions are coupled to the transformer versionOut of scope: the reporter's second point — not lowering
class_properties/class_static_block/ERM on Node versions with native support. It's a separate optimization (would eliminate most helper imports) and worth a follow-up issue.Closes #794
Test plan
Extended
helper-imports.spec.tswith aglobal installsuite (linkCore: false+ absolute-path--importsimulates the global CLI):class Counter { count = 1 }printscount: 1ERR_MODULE_NOT_FOUND) from the project@oxc-node/core(no helpers export) cannot shadow the loader's helpersrequirethrough the patched_resolveFilename)Verified:
vp fmt+vp check,cargo test,pnpm test(all integrate-* suites) green.