Repository navigation
feat(runtime): warn in debug when an ES module passes a relative specifier to the global require - #610
Draft
edusperoni wants to merge 5 commits into
Draft
feat(runtime): warn in debug when an ES module passes a relative specifier to the global require#610edusperoni wants to merge 5 commits into
edusperoni wants to merge 5 commits into
Conversation
…ifier to the global require
…' into feat/global-require-esm-diagnostic # Conflicts: # NativeScript/runtime/Runtime.mm
… ReportedModuleUrl
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
…' into feat/global-require-esm-diagnostic
This branch has not been deployed
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.
Stacked on #609 (it uses the
ReportedModuleUrlhelper that PR adds); the base branch is #609's and this retargets tomainonce that merges.What
Docs: the global
requireis available in ES modules for compatibility and resolves a relative specifier (./x,../x) against the app root, whichever module calls it. An ES module that needs module-relative resolution usescreateRequire(import.meta.url), or, when it was loaded overhttp(s),createRequirewith an app file path.Debug-build warning: when the global
requireis called from an ES module with a relative specifier, the runtime logs once per module (per isolate):requireitself does not change in any build: same resolution, same errors, same return values.Why
A CommonJS module gets a
requirebound to its own folder through the module wrapper. An ES module cannot be wrapped, sorequire(...)inside one is the global function, which is bound to the app root.require("./sibling")from an ES module in a subfolder therefore loads<app>/sibling, or fails, and nothing says why. The module-relativerequirefor ES modules iscreateRequire(import.meta.url), as in Node; the global one stays because the unwrapped main bundle, classic scripts and existing ES-module code use it.How
requiregets a native half of its own (DebugGlobalRequireCallback), which checks the call and then runs the unchangedRequireCallback. The bound directory cannot identify the globalrequire(a CommonJS module in the app root is bound to the same directory), and release builds never create the debug variant, so the globalrequirethere is exactly what it was..,..,./…(other than exactly./, which everyrequireresolves to the app's main entry) or../…. It takes the first stack frame outside the require factory script and looks its script id up among the registered ES modules (LookupModuleKeyForScriptId), so classic scripts,new Function/eval code and CommonJS modules never match, and file andhttp(s)ES modules both do.How it was verified
require("./sibling")still resolves against the app root (twice, and with../),~/…andns:specifiers load,createRequire(import.meta.url)("./sibling")loads the sibling, and classic-script and CommonJS callers of the globalrequireresolve against the app root; the same from a module served over HTTP.createRequirerequire or the classic-script caller. No existing spec triggers the warning.-Werror.Not covered by a spec or a captured line:
./..as whole specifiers warning, exactly./and.xnot warning; a release build was compiled but the suite was not run against it.