Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Imports within try blocks are hoisted incorrectly

オープン
#639 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript, nodejs, rollup
領域
build-system

調査の方向性

まず Node v12.17.0 上で microbundle v0.12.0 を使って再現コマンドを実行し、次に dist/bug-test.js を調べ、生成された require の配置を esm のソーススニペットと比較します。hoisting が microbundle と Rollup のどちらに起因するのかを特定します。require が try ブロックによって保護されたままで、node dist/bug-test.js がエラーなしで終了すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
JavaScript
スター
8.1k
フォーク
358
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

developit/microbundle のほかの issue

developit/microbundle の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。