microsoft / microsoft/TypeScript

The module namespace object returned by __importStar is not the same when import the same module multiple times

オープン
#63,391 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

Bug Domain: JS Emit
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 Search Terms
  • __importStar
  • module object
  • namespace object
🕗 Version & Regression Information
  • Version: 6.0.2
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "namespace import". I did not found any information related to this.
⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&jsx=0&module=1&ts=6.0.2#code/DYUwLgBADgjBC8ECWBbKB7ATmAFAIgDMBnPASgG4BYAKFEigCYFk0tdCSKaaAFTdFEiIgAdAENgwHAG1YAGmgMAuqRFgAFiAB2OGcRgLiy0ggB8EAN40INiAGN0WoulAjg6AOY59CeIiMUEAD0QRBgmACuIDQAvlzUQA

💻 Code
let p1 = import("fs");
let p2 = import("fs");

Promise.all([p1, p2]).then(([fs1, fs2]) => {
    console.log(fs1 === fs2); // expected true
});
🙁 Actual behavior

When the compiled code with node is run, false is printed. This means that the two imported module namespace object is different. each __importStar call create a new module namespace object.

🙂 Expected behavior

true should be printed. The module namespace objects of these two import("fs") should be the same object.
If we run this script directly by node without compiled, true will be printed.

Additional information about the issue

To reproduce this issue, "module" in "tsconfig.json" need to be set to "commonjs". This makes tsc use __importStar and require to import the module.

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

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

はじめの一歩

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

調査の方向性

__importStar ヘルパーと issue に記載されている CommonJS モジュールパスから始め、リンクされている Playground の例と Node.js の実行を再現手順として使用します。2 つの Promise の結果を比較し、import("fs") の呼び出しを繰り返すと namespace オブジェクトがどのように作成されるかを追跡します。コンパイルされた CommonJS のケースが Node.js の直接的な動作と一致し、同じオブジェクトを返せば完了です。

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

評価

技術スタック
node.js, typescript
領域
build-system, compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

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

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