From 1312a57984d9c058e257286ea4b93dec89b3a5de Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 4 Apr 2021 12:26:00 +0100 Subject: [PATCH] fix: Properly await already evaluating dynamic imports (#9984) --- cli/tests/088_dynamic_import_already_evaluating.ts | 2 ++ .../088_dynamic_import_already_evaluating.ts.out | 4 ++++ cli/tests/088_dynamic_import_target.ts | 3 +++ cli/tests/integration_tests.rs | 5 +++++ core/runtime.rs | 12 +++--------- 5 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 cli/tests/088_dynamic_import_already_evaluating.ts create mode 100644 cli/tests/088_dynamic_import_already_evaluating.ts.out create mode 100644 cli/tests/088_dynamic_import_target.ts diff --git a/cli/tests/088_dynamic_import_already_evaluating.ts b/cli/tests/088_dynamic_import_already_evaluating.ts new file mode 100644 index 0000000000..272163a5d9 --- /dev/null +++ b/cli/tests/088_dynamic_import_already_evaluating.ts @@ -0,0 +1,2 @@ +import("./088_dynamic_import_target.ts").then(() => console.log(3)); +import("./088_dynamic_import_target.ts").then(() => console.log(3)); diff --git a/cli/tests/088_dynamic_import_already_evaluating.ts.out b/cli/tests/088_dynamic_import_already_evaluating.ts.out new file mode 100644 index 0000000000..a36dd11e71 --- /dev/null +++ b/cli/tests/088_dynamic_import_already_evaluating.ts.out @@ -0,0 +1,4 @@ +[WILDCARD]1 +2 +3 +3 diff --git a/cli/tests/088_dynamic_import_target.ts b/cli/tests/088_dynamic_import_target.ts new file mode 100644 index 0000000000..226f1851a6 --- /dev/null +++ b/cli/tests/088_dynamic_import_target.ts @@ -0,0 +1,3 @@ +console.log(1); +await new Promise((r) => setTimeout(r, 100)); +console.log(2); diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 6589eb1353..5f8d5d4032 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2841,6 +2841,11 @@ console.log("finish"); output: "087_no_check_imports_not_used_as_values.ts.out", }); + itest!(_088_dynamic_import_already_evaluating { + args: "run --allow-read 088_dynamic_import_already_evaluating.ts", + output: "088_dynamic_import_already_evaluating.ts.out", + }); + itest!(js_import_detect { args: "run --quiet --reload js_import_detect.ts", output: "js_import_detect.ts.out", diff --git a/core/runtime.rs b/core/runtime.rs index f358cf05eb..04c6ca1af5 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -784,15 +784,9 @@ impl JsRuntime { module.get_status() }; - // Since the same module might be dynamically imported more than once, - // we short-circuit is it is already evaluated. - if status == v8::ModuleStatus::Evaluated { - self.dyn_import_done(load_id, id); - return Ok(()); - } - - if status != v8::ModuleStatus::Instantiated { - return Ok(()); + match status { + v8::ModuleStatus::Instantiated | v8::ModuleStatus::Evaluated => {} + _ => return Ok(()), } // IMPORTANT: Top-level-await is enabled, which means that return value