Skip to content

fix(bundler): normalize tegg manifest paths#5932

Merged
killagu merged 6 commits intonextfrom
agent/egg-dev/85bc11ed
May 7, 2026
Merged

fix(bundler): normalize tegg manifest paths#5932
killagu merged 6 commits intonextfrom
agent/egg-dev/85bc11ed

Conversation

@killagu
Copy link
Copy Markdown
Contributor

@killagu killagu commented May 6, 2026

Summary

  • Normalize tegg manifest moduleReferences paths with moduleDescriptors unitPath so LoaderFactory can match precomputed decoratedFiles in bundled runtime.
  • Include app/port decorated files in EntryGenerator coverage.
  • Let tegg LoaderUtil load pre-bundled files through the registered bundle module loader before falling back to dynamic import.

Tests

  • pnpm -C tools/egg-bundler exec vitest run test/ManifestLoader.test.ts test/EntryGenerator.test.ts
  • pnpm exec vitest run tegg/core/loader/test/Loader.test.ts
  • pnpm -C tools/egg-bundler run typecheck
  • pnpm -C tegg/core/loader run typecheck
  • pnpm -C tools/egg-bundler run lint
  • pnpm exec oxlint --type-aware tegg/core/loader/src/LoaderUtil.ts tegg/core/loader/test/Loader.test.ts
  • pnpm exec oxfmt --check tools/egg-bundler/src/lib/ManifestLoader.ts tools/egg-bundler/test/ManifestLoader.test.ts tools/egg-bundler/test/EntryGenerator.test.ts tegg/core/loader/src/LoaderUtil.ts tegg/core/loader/test/Loader.test.ts
  • git diff --check

Summary by CodeRabbit

  • New Features

    • Enhanced module loading with improved bundler support for better module resolution and TEgg manifest handling.
  • Bug Fixes

    • Improved error handling for module loading failures with clearer error messages.
  • Tests

    • Added comprehensive test coverage for bundled module loading scenarios and manifest normalization.
  • Chores

    • Added type definitions workspace dependency.

Copilot AI review requested due to automatic review settings May 6, 2026 10:03
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR implements and integrates a bundler-based module loader mechanism. It adds support for loading pre-bundled modules via globalThis.__EGG_BUNDLE_MODULE_LOADER__ as a primary loader with dynamic import as fallback. The bundler tooling is updated to normalize module references in manifests, and typing is centralized via @eggjs/typings/global.

Changes

Bundler Module Loader Integration

Layer / File(s) Summary
Type System Integration
tegg/core/loader/src/LoaderUtil.ts, tegg/core/loader/test/Loader.test.ts, tools/egg-bundler/src/lib/EntryGenerator.ts, packages/utils/src/import.ts
Type-only imports from @eggjs/typings/global added to enable globalThis.__EGG_BUNDLE_MODULE_LOADER__ type assertions across loader infrastructure.
Manifest Data Shape
tools/egg-bundler/src/lib/ManifestLoader.ts
TeggManifestExtension extended with moduleReferences alongside moduleDescriptors; new TeggModuleReference interface introduced.
Bundler Manifest Normalization
tools/egg-bundler/src/lib/ManifestLoader.ts
#normalizeExtensions now normalizes tegg.moduleReferences and moduleDescriptors; new #normalizeTeggUnitPath helper maps absolute unit paths to normalized module-map paths or falls back to relative paths.
Worker Entry Generation
tools/egg-bundler/src/lib/EntryGenerator.ts
Generated worker entry template updated to use type-only import for globalThis typing, replacing inline type assertions while preserving bundler loader assignment logic.
Core Loader Fallback Logic
tegg/core/loader/src/LoaderUtil.ts
LoaderUtil.loadFile() refactored to attempt globalThis.__EGG_BUNDLE_MODULE_LOADER__ with normalized paths first, then Windows file:// conversion and dynamic import() as fallback; new createLoadError standardizes error handling across bundler and import failures.
Shared Utilities
packages/utils/src/import.ts
importModule() and setBundleModuleLoader() switched to read/write globalThis.__EGG_BUNDLE_MODULE_LOADER__ directly; removed cached alias pattern while preserving bundle-hit unwrapping semantics.
Tests and Dependencies
tegg/core/loader/test/Loader.test.ts, tools/egg-bundler/test/EntryGenerator.test.ts, tools/egg-bundler/test/ManifestLoader.test.ts, plugins/mock/test/mock_service_cluster.test.ts, tegg/core/loader/package.json, tools/egg-bundler/package.json
New tests validate bundler loader success, null fallback, and error paths; afterEach hook clears __EGG_BUNDLE_MODULE_LOADER__ between tests; @eggjs/typings added as workspace dependency; test timeout increased for cluster tests.

Sequence Diagram

sequenceDiagram
    actor User as Application Code
    participant Load as LoaderUtil.loadFile()
    participant Global as globalThis
    participant Bundle as __EGG_BUNDLE_MODULE_LOADER__
    participant Dynamic as import()

    User->>Load: loadFile(filePath)
    Load->>Load: normalize path (\ to /)
    Load->>Global: read __EGG_BUNDLE_MODULE_LOADER__
    Global-->>Load: loader function
    Load->>Bundle: call loader(normalizedPath)
    
    alt Bundle Returns Module
        Bundle-->>Load: module object
        Load-->>User: parsed prototypes
    else Bundle Returns null/undefined
        Bundle-->>Load: null/undefined
        Load->>Load: apply Windows file:// conversion
        Load->>Dynamic: import(filePath)
        Dynamic-->>Load: module exports
        Load-->>User: parsed prototypes
    else Bundle Throws Error
        Bundle-->>Load: Error thrown
        Load->>Load: createLoadError(filePath, error)
        Load-->>User: wrapped Error with cause
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • eggjs/egg#5896: Modifies code to use shared @eggjs/typings/global augmentation and read/write globalThis.__EGG_BUNDLE_MODULE_LOADER__.
  • eggjs/egg#5911: Modifies bundler module loader wiring and globalThis.EGG_BUNDLE_MODULE_LOADER handling with associated path normalization and error handling.
  • eggjs/egg#5867: Implements and uses the same runtime bundle-module loader on globalThis with path normalization and module unwrapping behavior.

Suggested reviewers

  • jerryliang64
  • fengmk2

Poem

🐰 A bundler loader springs to life,
With globalThis as a type-safe knife,
Manifests normalized with care,
Fallbacks flowing everywhere,
Pre-bundled modules load with glee!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: normalizing tegg manifest paths for bundler integration, which is supported by path normalization work in ManifestLoader and related test updates.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/egg-dev/85bc11ed

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 6, 2026

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: 49408cc
Status: ✅  Deploy successful!
Preview URL: https://3324b18c.egg-cci.pages.dev
Branch Preview URL: https://agent-egg-dev-85bc11ed.egg-cci.pages.dev

View logs

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.19%. Comparing base (aca1168) to head (49408cc).
⚠️ Report is 2 commits behind head on next.

Files with missing lines Patch % Lines
tegg/core/loader/src/LoaderUtil.ts 90.90% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             next    #5932   +/-   ##
=======================================
  Coverage   85.18%   85.19%           
=======================================
  Files         668      668           
  Lines       19284    19288    +4     
  Branches     3782     3784    +2     
=======================================
+ Hits        16428    16432    +4     
  Misses       2464     2464           
  Partials      392      392           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a bundle module loader mechanism to tegg/core/loader and updates tools/egg-bundler to support path normalization for moduleReferences and moduleDescriptors. The changes include adding a global hook for module loading, updating the loadFile utility, and adding comprehensive tests for the new functionality. A review comment suggests improving error handling in the catch block to safely handle non-Error exceptions.

Comment thread tegg/core/loader/src/LoaderUtil.ts Outdated
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 6, 2026

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 49408cc
Status: ✅  Deploy successful!
Preview URL: https://aa037ac2.egg-v3.pages.dev
Branch Preview URL: https://agent-egg-dev-85bc11ed.egg-v3.pages.dev

View logs

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tegg/core/loader/src/LoaderUtil.ts`:
- Around line 73-91: The bundle loader can return null to indicate "not bundled"
but the current fallback only checks exports === undefined causing a later
TypeError; update the check around the bundle result (the local variable exports
produced by (globalThis as BundleModuleGlobalThis).__EGG_BUNDLE_MODULE_LOADER__
invocation in LoaderUtil.ts) to treat both null and undefined as "not bundled"
(e.g., use exports == null or explicit exports === undefined || exports ===
null) so the dynamic import fallback (await import(filePath)) runs when the
bundle loader returns null.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d8b70aa8-a84f-446c-b4dc-5f3f7812fbb7

📥 Commits

Reviewing files that changed from the base of the PR and between 0389f2d and 80a0285.

📒 Files selected for processing (5)
  • tegg/core/loader/src/LoaderUtil.ts
  • tegg/core/loader/test/Loader.test.ts
  • tools/egg-bundler/src/lib/ManifestLoader.ts
  • tools/egg-bundler/test/EntryGenerator.test.ts
  • tools/egg-bundler/test/ManifestLoader.test.ts

Comment thread tegg/core/loader/src/LoaderUtil.ts Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tegg/core/loader/src/LoaderUtil.ts`:
- Around line 73-75: The call to the global bundle loader in LoaderUtil.loadFile
is not wrapped in a try/catch so thrown values bypass the standard
"[tegg/loader] load ... failed" wrapper; update the code around the
__EGG_BUNDLE_MODULE_LOADER__ invocation in loadFile to catch any thrown value
(including non-Error), and rethrow a new Error that prefixes the original
message/stack with "[tegg/loader] load <originalFilePath> failed: " (or include
the original error message when no stack exists) so behavior matches the
import-failure handling currently used in the later catch block.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: df572c9c-91e5-441a-bf15-9b21cb511433

📥 Commits

Reviewing files that changed from the base of the PR and between 80a0285 and 14a239b.

📒 Files selected for processing (2)
  • tegg/core/loader/src/LoaderUtil.ts
  • tegg/core/loader/test/Loader.test.ts

Comment thread tegg/core/loader/src/LoaderUtil.ts Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the bundling pipeline and tegg loader integration so that tegg manifest paths and decorated files remain resolvable in a bundled runtime, and so tegg can load pre-bundled modules via the bundle module loader before falling back to dynamic import.

Changes:

  • Normalize tegg moduleReferences[].path and moduleDescriptors[].unitPath to consistent, app-relative (or moduleMap-normalized) paths during manifest normalization.
  • Expand EntryGenerator coverage to include decorated files under app/port from tegg manifest descriptors.
  • Enhance LoaderUtil.loadFile() to consult a registered globalThis.__EGG_BUNDLE_MODULE_LOADER__ first, with tests covering the fast-path and fallback behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tools/egg-bundler/src/lib/ManifestLoader.ts Normalize tegg module reference/descriptor paths via a shared helper to ensure consistent keys at runtime.
tools/egg-bundler/test/ManifestLoader.test.ts Add regression test asserting tegg path normalization and decorated-file resolution.
tools/egg-bundler/test/EntryGenerator.test.ts Add test ensuring app/port tegg-decorated files are included in generated worker imports.
tegg/core/loader/src/LoaderUtil.ts Add bundle-module-loader fast path before dynamic import for pre-bundled file loading.
tegg/core/loader/test/Loader.test.ts Add tests for bundle-module-loader loading and fallback, plus global cleanup between tests.

Comment thread tegg/core/loader/src/LoaderUtil.ts Outdated
Comment thread tegg/core/loader/test/Loader.test.ts
Copilot AI review requested due to automatic review settings May 6, 2026 15:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tegg/core/loader/src/LoaderUtil.ts`:
- Around line 88-90: The import failure wrapper is using filePath (which can be
a file:// URL on Windows) when calling createLoadError; change the catch to call
createLoadError with originalFilePath instead so the error uses the
caller-facing path. Locate the catch block around the dynamic import (the line
with "exports = await import(filePath);" and the subsequent throw
createLoadError(...)) and replace the second argument from filePath to
originalFilePath; keep the original caught error (e) passed through unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e465c7db-34d3-4a51-9328-1ccd149139dd

📥 Commits

Reviewing files that changed from the base of the PR and between 2c6da68 and 49408cc.

⛔ Files ignored due to path filters (1)
  • tools/egg-bundler/test/__snapshots__/EntryGenerator.worker.canonical.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • packages/utils/src/import.ts
  • tegg/core/loader/src/LoaderUtil.ts
  • tegg/core/loader/test/Loader.test.ts
  • tools/egg-bundler/src/lib/EntryGenerator.ts

Comment on lines +88 to +90
exports = await import(filePath);
} catch (e: unknown) {
throw createLoadError(filePath, e);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Preserve original path in import-failure errors.

At Line 90, wrapping with filePath can emit a file://... URL on Windows, which makes troubleshooting less clear than the caller-facing path. Use originalFilePath in the error wrapper.

Suggested patch
       try {
         exports = await import(filePath);
       } catch (e: unknown) {
-        throw createLoadError(filePath, e);
+        throw createLoadError(originalFilePath, e);
       }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
exports = await import(filePath);
} catch (e: unknown) {
throw createLoadError(filePath, e);
exports = await import(filePath);
} catch (e: unknown) {
throw createLoadError(originalFilePath, e);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tegg/core/loader/src/LoaderUtil.ts` around lines 88 - 90, The import failure
wrapper is using filePath (which can be a file:// URL on Windows) when calling
createLoadError; change the catch to call createLoadError with originalFilePath
instead so the error uses the caller-facing path. Locate the catch block around
the dynamic import (the line with "exports = await import(filePath);" and the
subsequent throw createLoadError(...)) and replace the second argument from
filePath to originalFilePath; keep the original caught error (e) passed through
unchanged.

@killagu killagu merged commit f6c7b7c into next May 7, 2026
23 of 26 checks passed
@killagu killagu deleted the agent/egg-dev/85bc11ed branch May 7, 2026 02:23
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.

2 participants