From bc1038ab5b9318a6999ed4453414b380f45d6714 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 23 Feb 2018 14:21:49 -0600 Subject: [PATCH] module: fix cyclical dynamic import ensures that instantiation result is only used during initial loading PR-URL: https://github.com/nodejs/node/pull/18965 Reviewed-By: Gus Caplan Reviewed-By: Guy Bedford Reviewed-By: Colin Ihrig Reviewed-By: Jeremiah Senkpiel Reviewed-By: Jan Krems Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/loader/ModuleJob.js | 7 ++++--- test/es-module/test-esm-cyclic-dynamic-import.mjs | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 test/es-module/test-esm-cyclic-dynamic-import.mjs diff --git a/lib/internal/loader/ModuleJob.js b/lib/internal/loader/ModuleJob.js index f79cc6cfe1d94a..b3553fc7235d95 100644 --- a/lib/internal/loader/ModuleJob.js +++ b/lib/internal/loader/ModuleJob.js @@ -50,10 +50,11 @@ class ModuleJob { } async instantiate() { - if (this.instantiated) { - return this.instantiated; + if (!this.instantiated) { + return this.instantiated = this._instantiate(); } - return this.instantiated = this._instantiate(); + await this.instantiated; + return this.module; } // This method instantiates the module associated with this job and its diff --git a/test/es-module/test-esm-cyclic-dynamic-import.mjs b/test/es-module/test-esm-cyclic-dynamic-import.mjs new file mode 100644 index 00000000000000..c8dfff919c2f7e --- /dev/null +++ b/test/es-module/test-esm-cyclic-dynamic-import.mjs @@ -0,0 +1,3 @@ +// Flags: --experimental-modules +import '../common'; +import('./test-esm-cyclic-dynamic-import');