Skip to content

fix(externals): resolve non-hoisted traced require() ids from rolldown's __require helper - #4992

Merged
pi0 merged 4 commits into
nitrojs:mainfrom
Gourav224:fix/traced-require-from-runtime
Nov 1, 2026
Merged

pi0 merged 4 commits into
nitrojs:mainfrom
Gourav224:fix/traced-require-from-runtime

Conversation

@Gourav224

@Gourav224 Gourav224 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Resolves #4991

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Since #4740, when a traced dependency is not the hoisted version, Nitro writes it as a path relative to the chunk that uses it. That is right for import, but not for require() in bundled CommonJS. Rolldown (and Vite) send those calls through one shared __require helper (createRequire(import.meta.url)), defined in the chunk that holds the rolldown runtime (usually _runtime.mjs). So the path is resolved from that chunk, not from the calling chunk. Any chunk in a subfolder (_libs/…) then fails with MODULE_NOT_FOUND. Example from the issue: tsyringe requires tslib@1 while the app uses tslib@2.

// .output/server/_libs/tsyringe.mjs
import { __require } from "../_runtime.mjs";
__require("../node_modules/.nf3/[email protected]/tslib.js"); // before: resolved from _runtime.mjs, outside the output
__require("./node_modules/.nf3/[email protected]/tslib.js");  // after

Fix (renderChunk in nitro:externals):

  • import / import() keep the path relative to the chunk.
  • For __require("…") calls, the path is computed from the chunk that defines the helper. That chunk is found from bundle metadata (meta.chunks): the chunk whose modules include \0rolldown/runtime.js. It can be _runtime.mjs, a lib chunk (when only one chunk uses the runtime), or the calling chunk itself (inlineDynamicImports). If no chunk has it, the old chunk-relative path is used.
  • Rolldown renames the helper to __require$1 when another module in the same chunk declares its own __require (tsup/esbuild ESM output does this). The renamed form is matched too.

The Rollup builder is not affected: @rollup/plugin-commonjs turns external requires into imports.

Tests (in the existing "resolve module version conflicts" test):

  • nitro-cjs-nested-requirer: a bundled CommonJS package in _libs/ that requires its own nested @fixture/[email protected].
  • nitro-cjs-shadowed-requirer: the same setup, plus its own __require so the helper gets renamed (nested @fixture/[email protected]).

Both fail on rolldown without the fix (500, MODULE_NOT_FOUND) and pass with it.

test/unit/externals-require-helper.test.ts builds a small app where the runtime lives in _libs/pkg-cjs.mjs (and in index.mjs with inlineDynamicImports), next to a doc comment that looks like an import of __require. It guards against finding the helper by scanning the chunk text. test/presets/node.test.ts and test/presets/vercel.test.ts pass on rolldown and rollup. Lint, format and typecheck are clean.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

🤖 Generated with AI assistant

…ire helper

Rolldown routes `require()` in bundled CommonJS through one shared `__require`
helper, so a non-hoisted traced id must be relative to the chunk that defines
it, not to the calling chunk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Gourav224
Gourav224 requested a review from pi0 as a code owner November 1, 2026 13:04
@vercel

vercel Bot commented Nov 1, 2026

Copy link
Copy Markdown

@Gourav224 is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Walkthrough

renderChunk now uses the runtime helper chunk filename for traced IDs in bundled __require calls. The changes add tests for nested and shadowed CommonJS dependency versions.

Changes: Traced external path resolution

Layer / File(s) Summary
Resolve traced paths for bundled require calls
src/build/plugins/externals.ts
renderChunk detects __require calls, including suffixed helper names, and locates the chunk containing Rolldown’s runtime module. It uses that chunk’s filename for traced IDs in those calls, and otherwise uses the current chunk filename.
Add nested and shadowed CommonJS version cases
test/unit/externals-require-helper.test.ts, test/fixture/server/routes/modules.ts, test/tests.ts
The unit test builds fixtures with root and nested dependency versions under both inlineDynamicImports settings. The module-version fixture and test add nested and shadowed requirers with expected versions 4.0.0 and 5.0.0.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Bundled CommonJS require() of a non-hoisted traced external resolves from _runtime.mjs, not its chunk. The change selects the runtime helper chunk filename for traced IDs used in __require calls.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title follows the Conventional Commits format and accurately describes the fix for traced require() paths in externals.
Description check Passed The description clearly explains the bug, the implementation, the affected builders, and the regression tests. It is directly related to the changeset.
Linked Issues check Passed The changes satisfy the coding requirements in #4991. src/build/plugins/externals.ts computes traced paths for bundled CommonJS __require calls from the helper-defining chunk, detects numbered hel…
Out of Scope Changes check Passed The changes are limited to external path resolution and focused regression coverage in the fixture route and module-version-conflict test. These changes directly support #4991. No unrelated implementa…
Full details: Linked Issues check

Explanation

The changes satisfy the coding requirements in #4991. src/build/plugins/externals.ts computes traced paths for bundled CommonJS __require calls from the helper-defining chunk, detects numbered helper names, and preserves current-chunk paths for other traced imports. The route and module-version-conflict test add coverage for nested chunks and the shadowed helper case. The excluded fixture files are intentionally unavailable, but the reviewed test wiring and assertions provide independent evidence of the required regression coverage.

Full details: Out of Scope Changes check

Explanation

The changes are limited to external path resolution and focused regression coverage in the fixture route and module-version-conflict test. These changes directly support #4991. No unrelated implementation or test changes are shown.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • 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

@pi0 pi0 added the v3 label Nov 1, 2026
Rolldown renames its helper (`__require$1`) when another module in the
chunk declares its own `__require` (e.g. tsup/esbuild output). Also resolve
the helper's chunk once per chunk.
@pi0x pi0x changed the title fix(externals): resolve traced CommonJS requires from the shared require helper fix(externals): resolve non-hoisted traced require() ids from rolldown's __require helper Nov 1, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4992

commit: 9fc4443

Scanning the chunk for an import of `__require` could match a comment or a
string when the helper is defined in the chunk itself (inlined builds, or a
lib chunk that holds the rolldown runtime). Use the chunk whose modules
include the rolldown runtime instead.
@pi0
pi0 merged commit 5b14014 into nitrojs:main Nov 1, 2026
14 of 16 checks passed
@Gourav224
Gourav224 deleted the fix/traced-require-from-runtime branch November 2, 2026 03:58
@pi0x pi0x removed the v3 label Nov 10, 2026
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.

Bundled CommonJS require() of a non-hoisted traced external resolves from _runtime.mjs, not its chunk

3 participants