Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Imports within try blocks are hoisted incorrectly

Đang mở
#639 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, nodejs, rollup
Lĩnh vực
build-system

Hướng nghiên cứu

Bắt đầu bằng cách chạy các lệnh tái hiện với microbundle v0.12.0 trên Node v12.17.0, sau đó kiểm tra dist/bug-test.js và so sánh vị trí của require được tạo ra với đoạn mã nguồn từ esm. Xác định liệu việc hoisting bắt nguồn từ microbundle hay Rollup; hoàn tất có nghĩa là require vẫn được bảo vệ bởi khối try và node dist/bug-test.js thoát mà không có lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

upstream
Problem

Microbundle hoists a require() call out of a try {} block in a dependency. When that require() call throws, it is not caught by the try {} block anymore.

I'm not sure whether this is an issue with microbundle or rollup. A solution to this would be not to hoist require() calls if they are in a nested block or wrap them in a function to evaluate them lazily.

How to reproduce

Make sure you have yarn installed globally, then run following commands:

mkdir bug-test
cd bug-test
yarn add microbundle esm
echo "import 'esm'" > test.js
yarn microbundle test.js --format cjs --target node --external none
node dist/bug-test.js
Expected behaviour

Commands above should execute and exit without error code.

Actual behaviour

The node dist/bug-test.js command throws this error:

internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module 'internal/bootstrap/loaders'
Require stack:
- /Users/dany/code/bug-test/dist/bug-test.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
    at Function.Module._load (internal/modules/cjs/loader.js:842:27)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/dany/code/bug-test/dist/bug-test.js:7:31)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/dany/code/bug-test/dist/bug-test.js' ]

The reason for this error is that there is this code in the esm module:

[…] function q(e){let t;try{const{internalBinding:n}=require("internal/bootstrap/loaders"),r=n("natives");x(r,e)&&(t=r[e])}catch(e){}return"string"==typeof t?t:""} […]

// Simplified:
try { require("internal/bootstrap/loaders") } catch (e) {}

which is modified to this by microbundle in dist/bug-test.js:

[…] var loaders = _interopDefault(require('internal/bootstrap/loaders')); […] function q(e){let t;try{const{internalBinding:n}=loaders,r=n("natives");x(r,e)&&(t=r[e]);}catch(e){}return "string"==typeof t?t:""} […]

// Simplified:
var loaders = require("internal/bootstrap/loaders")
try { loaders } catch (e) {}
Additional info

microbundle v0.12.0
Node v12.17.0
Mac OS 10.15.5
Related: https://github.com/nodejs/node/issues/33656

Ngôn ngữ chính
JavaScript
Star
8.1k
Fork
358
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của developit/microbundle

Tất cả issue của developit/microbundle

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.