Skip to content

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
refactor/loader-url-entry-pointsfrom
feat/global-require-esm-diagnostic
Draft

edusperoni wants to merge 5 commits into
refactor/loader-url-entry-pointsfrom
feat/global-require-esm-diagnostic

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #609 (it uses the ReportedModuleUrl helper that PR adds); the base branch is #609's and this retargets to main once that merges.

What

  • Docs: the global require is 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 uses createRequire(import.meta.url), or, when it was loaded over http(s), createRequire with an app file path.

  • Debug-build warning: when the global require is called from an ES module with a relative specifier, the runtime logs once per module (per isolate):

    NativeScript: require("./x.js") in ES module file:///app/features/a/index.mjs resolves against the app root, not the module's folder. Use createRequire(import.meta.url) from "ns:module" (from a module loaded over http(s): createRequire with an app file path).
    

require itself does not change in any build: same resolution, same errors, same return values.

Why

A CommonJS module gets a require bound to its own folder through the module wrapper. An ES module cannot be wrapped, so require(...) 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-relative require for ES modules is createRequire(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

  • In debug builds the global require gets a native half of its own (DebugGlobalRequireCallback), which checks the call and then runs the unchanged RequireCallback. The bound directory cannot identify the global require (a CommonJS module in the app root is bound to the same directory), and release builds never create the debug variant, so the global require there is exactly what it was.
  • The check runs only for a string specifier that is ., .., ./… (other than exactly ./, which every require resolves 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 and http(s) ES modules both do.
  • Warned modules are remembered per isolate, by registry key, so a module recompiled during development does not warn again and each worker has its own set.
  • The warning code runs no JavaScript and cannot throw.

How it was verified

  • New specs pin the behaviour that does not change: from an ES module in a subfolder, the global require("./sibling") still resolves against the app root (twice, and with ../), ~/… and ns: specifiers load, createRequire(import.meta.url)("./sibling") loads the sibling, and classic-script and CommonJS callers of the global require resolve against the app root; the same from a module served over HTTP.
  • The suite cannot observe log output and no test-only hook was added. The warning was checked by capturing the simulator log of each full run: exactly two lines per run, one for the file ES module fixture and one for the HTTP one, and none for the repeat call, another relative specifier from the same module, non-relative specifiers, the CommonJS caller, the createRequire require or the classic-script caller. No existing spec triggers the warning.
  • Full suite on an iOS simulator on top of refactor(runtime): one URL parse for every http(s) module entry point #609: 2344 test cases, 0 failures. Under AddressSanitizer it last ran before refactor(runtime): one URL parse for every http(s) module entry point #609's final two commits were merged in (2338 test cases, 0 failures at that point). Debug and Release builds with -Werror.

Not covered by a spec or a captured line: ./.. as whole specifiers warning, exactly ./ and .x not warning; a release build was compiled but the suite was not run against it.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@edusperoni
edusperoni added this pull request to stack #611 November 27, 2026 00:19

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant