fix(bundler): avoid shadowed __dirname in import.meta patch#5916
fix(bundler): avoid shadowed __dirname in import.meta patch#5916
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe bundler now generates import.meta.dirname purely from the computed filename (by locating the last '/' or '' and applying slicing rules including drive-letter/root edge cases), removing any conditional runtime fallback to __dirname; import.meta.filename and url remain filename-based. ChangesImport.meta dirname calculation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #5916 +/- ##
=======================================
Coverage 85.03% 85.04%
=======================================
Files 667 667
Lines 19123 19123
Branches 3723 3723
=======================================
+ Hits 16262 16263 +1
+ Misses 2468 2467 -1
Partials 393 393 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime TDZ failure in egg-bundler’s post-pack Turbopack import.meta patching by removing references to __dirname, which can be shadowed by later lexical const __dirname declarations in the same module scope.
Changes:
- Update the generated Turbopack
import.metareplacement to deriveimport.meta.dirnamepurely from the resolved filename (avoiding__dirnameaccess). - Add an integration regression test covering a chunk that declares
const __dirnameafter the patchedimport.metaobject.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tools/egg-bundler/src/lib/Bundler.ts | Adjusts generated import.meta object patch to avoid __dirname TDZ by deriving dirname from filename. |
| tools/egg-bundler/test/integration.test.ts | Adds a regression test ensuring patched chunks don’t touch a shadowed __dirname binding. |
There was a problem hiding this comment.
Code Review
This pull request simplifies the dirname calculation in Bundler.ts by removing the check for a global __dirname and adds an integration test to ensure import.meta patching works correctly when __dirname is shadowed. A review comment points out that the current regex-based calculation returns an empty string for files at the root directory and suggests a fallback to match Node.js behavior.
Deploying egg with
|
| Latest commit: |
ca921a6
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://826d3b69.egg-cci.pages.dev |
| Branch Preview URL: | https://agent-egg-dev-0a638d6f.egg-cci.pages.dev |
Deploying egg-v3 with
|
| Latest commit: |
ca921a6
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://16e04e7c.egg-v3.pages.dev |
| Branch Preview URL: | https://agent-egg-dev-0a638d6f.egg-v3.pages.dev |
ba21354 to
ca921a6
Compare
Summary
__dirnameinside the generated Turbopackimport.metareplacement.import.meta.dirnamefrom the resolved filename instead, so later generated lexicalconst __dirnamedeclarations cannot trigger TDZ.const __dirnameafter theimport.metaobject.Root Cause
The post-pack import.meta patch emitted
typeof __dirname === "string" ? __dirname : ....In cnpmcore's generated root chunk, Turbopack also emits a later
const __dirname = ...in the same module scope. That lexical declaration shadows CommonJS__dirnamefor the whole scope, so eventypeof __dirnamethrowsReferenceError: Cannot access __dirname before initializationbefore the later declaration runs.This is a general shadowed-lexical-binding issue in Egg's import.meta output patch, not cnpmcore-specific application code.
Verification
pnpm vitest run test/integration.test.tsfromtools/egg-bundlerpnpm --filter @eggjs/egg-bundler typecheckpnpm --filter @eggjs/egg-bundler lintpnpm --filter @eggjs/egg-bundler test971784993927local reproduction withegg@4.1.2-beta.9dependencies:supports-coloralias completed and produceddist-bundle-tdz-fix/worker.jstimeout 20s node dist-bundle-tdz-fix/worker.jsno longer hit the__dirnameTDZ blockerCannot find native bindingfor@cnpmjs/packument-linux-x64-gnu/packument.linux-x64-gnu.nodeSummary by CodeRabbit
Bug Fixes
Tests