From 7d2a57fd24e0bd71e2dcd87eeaa35f1a00c99301 Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Mon, 15 May 2023 10:27:38 +0200 Subject: [PATCH] Re-throw module.error when instantiating runtime modules (#4918) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description Runtime modules can be executed multiple times within the same context. However, when such a module encounters a runtime error during execution, this error is cached in module.error and not properly re-thrown when the module is required again from `getOrInstantiateRuntimeModule`, which means that on later runs we’ll silently fail to instantiate the module, and run into any errors that expects a global side-effect from the module. link WEB-1045 --- crates/turbopack-dev/js/src/runtime.js | 49 +++++++++++-------- ...ot_basic_async_chunk_input_index_0d348e.js | 49 +++++++++++-------- ...apshot_basic_chunked_input_index_e77e9f.js | 49 +++++++++++-------- ...apshot_basic_shebang_input_index_b1f0c2.js | 49 +++++++++++-------- ...shot_comptime_define_input_index_6b0d2b.js | 49 +++++++++++-------- ..._absolute-uri-import_input_index_fa9a30.js | 49 +++++++++++-------- ...sts_snapshot_css_css_input_index_37a138.js | 49 +++++++++++-------- ...shot_emotion_emotion_input_index_b080c4.js | 49 +++++++++++-------- ...sts_snapshot_env_env_input_index_29a23f.js | 49 +++++++++++-------- ...entrry_runtime_entry_input_index_f59cc7.js | 49 +++++++++++-------- ...shot_example_example_input_index_78b6bf.js | 49 +++++++++++-------- ...ot_export-alls_cjs-2_input_index_289ae7.js | 49 +++++++++++-------- ...port-alls_cjs-script_input_index_3e96b7.js | 49 +++++++++++-------- ...shot_import-meta_cjs_input_index_537553.js | 49 +++++++++++-------- ...rt-meta_esm-multiple_input_index_c00392.js | 49 +++++++++++-------- ...ort-meta_esm-mutable_input_index_6c9201.js | 49 +++++++++++-------- ...port-meta_esm-object_input_index_6fcf7d.js | 49 +++++++++++-------- ...shot_import-meta_esm_input_index_c4c88a.js | 49 +++++++++++-------- ...shot_import-meta_url_input_index_988b57.js | 49 +++++++++++-------- ...shot_imports_dynamic_input_index_45c162.js | 49 +++++++++++-------- ...napshot_imports_json_input_index_961ae2.js | 49 +++++++++++-------- ...ts_resolve_error_cjs_input_index_f8412b.js | 49 +++++++++++-------- ...ts_resolve_error_esm_input_index_0b3e45.js | 49 +++++++++++-------- ...s_static-and-dynamic_input_index_ec8693.js | 49 +++++++++++-------- ...pshot_imports_static_input_index_885269.js | 49 +++++++++++-------- ...de_protocol_external_input_index_667edf.js | 49 +++++++++++-------- ...ts_styled_components_input_index_afc482.js | 49 +++++++++++-------- ...nsforms_input_packages_app_index_4a3d65.js | 49 +++++++++++-------- ...ransforms_preset_env_input_index_9dcfd0.js | 49 +++++++++++-------- ...ipt_jsconfig-baseurl_input_index_8f1e58.js | 49 +++++++++++-------- ...sconfig-baseurl_input_index.ts_0aa04e._.js | 49 +++++++++++-------- ...odule-full-path_input_index.ts_a751eb._.js | 49 +++++++++++-------- ...-extends-module_input_index.ts_a662d4._.js | 49 +++++++++++-------- ...ds-relative-dir_input_index.ts_be3d7b._.js | 49 +++++++++++-------- ...nds-without-ext_input_index.ts_38aae8._.js | 49 +++++++++++-------- ...sconfig-extends_input_index.ts_18c083._.js | 49 +++++++++++-------- 36 files changed, 1044 insertions(+), 720 deletions(-) diff --git a/crates/turbopack-dev/js/src/runtime.js b/crates/turbopack-dev/js/src/runtime.js index d13e717155e95..e322c55b3db85 100644 --- a/crates/turbopack-dev/js/src/runtime.js +++ b/crates/turbopack-dev/js/src/runtime.js @@ -482,8 +482,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -501,11 +502,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -530,21 +531,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1418,6 +1424,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js index 9faac2c50aeec..67e3df5963889 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js +++ b/crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js index ee4075eb29004..35770e1121614 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js +++ b/crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js index a4847ad6eaff1..b3526d8ff9a51 100644 --- a/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js +++ b/crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js index 4487995094958..21a3fd426bba9 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js +++ b/crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_fa9a30.js b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_fa9a30.js index c78ddb8403f55..93acd8dba7818 100644 --- a/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_fa9a30.js +++ b/crates/turbopack-tests/tests/snapshot/css/absolute-uri-import/output/crates_turbopack-tests_tests_snapshot_css_absolute-uri-import_input_index_fa9a30.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_37a138.js b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_37a138.js index b94e3db0e07d8..007b7a651beb4 100644 --- a/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_37a138.js +++ b/crates/turbopack-tests/tests/snapshot/css/css/output/crates_turbopack-tests_tests_snapshot_css_css_input_index_37a138.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js index cbb2213240007..9a40e3c27baf7 100644 --- a/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js +++ b/crates/turbopack-tests/tests/snapshot/emotion/emotion/output/crates_turbopack-tests_tests_snapshot_emotion_emotion_input_index_b080c4.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js index 048efca1a5c08..940fd535b1e8a 100644 --- a/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js +++ b/crates/turbopack-tests/tests/snapshot/env/env/output/crates_turbopack-tests_tests_snapshot_env_env_input_index_29a23f.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js index ff834e2efc6e3..00d9330d5d92e 100644 --- a/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js +++ b/crates/turbopack-tests/tests/snapshot/evaluated_entrry/runtime_entry/output/a587c_tests_snapshot_evaluated_entrry_runtime_entry_input_index_f59cc7.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js index f0c25a2106a57..b5e8880764033 100644 --- a/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js +++ b/crates/turbopack-tests/tests/snapshot/example/example/output/crates_turbopack-tests_tests_snapshot_example_example_input_index_78b6bf.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js index 764c6d18f653c..076674a4c8ff5 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-2_input_index_289ae7.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js index 77ae01e426dca..5f9df13220446 100644 --- a/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js +++ b/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/output/crates_turbopack-tests_tests_snapshot_export-alls_cjs-script_input_index_3e96b7.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js index 96214f1fda13b..be59d90b341b6 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/cjs/output/crates_turbopack-tests_tests_snapshot_import-meta_cjs_input_index_537553.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js index 756b271c5eef6..d6f027efb234b 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-multiple/output/79fb1_turbopack-tests_tests_snapshot_import-meta_esm-multiple_input_index_c00392.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js index 85b62498eb3da..c9b2cfbc731b7 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-mutable/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-mutable_input_index_6c9201.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js index 5a856f3271654..19af0fa2736fb 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm-object/output/crates_turbopack-tests_tests_snapshot_import-meta_esm-object_input_index_6fcf7d.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js index f03d318c58144..54ba2a78ff9a1 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/esm/output/crates_turbopack-tests_tests_snapshot_import-meta_esm_input_index_c4c88a.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js index 0eeaa505be03d..e8716224cd143 100644 --- a/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js +++ b/crates/turbopack-tests/tests/snapshot/import-meta/url/output/crates_turbopack-tests_tests_snapshot_import-meta_url_input_index_988b57.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js index 0c2e44afe9be8..72e7915a5132e 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js +++ b/crates/turbopack-tests/tests/snapshot/imports/dynamic/output/crates_turbopack-tests_tests_snapshot_imports_dynamic_input_index_45c162.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js index cab2b1da5f1a1..10f02270be3c2 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js +++ b/crates/turbopack-tests/tests/snapshot/imports/json/output/crates_turbopack-tests_tests_snapshot_imports_json_input_index_961ae2.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js index 483ebe58ea515..4a119e898371a 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_cjs_input_index_f8412b.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js index fa3d7dd1f8cb5..45f6682203864 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_esm/output/79fb1_turbopack-tests_tests_snapshot_imports_resolve_error_esm_input_index_0b3e45.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js index bd82767967a96..2225c2e14d034 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static-and-dynamic/output/79fb1_turbopack-tests_tests_snapshot_imports_static-and-dynamic_input_index_ec8693.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js index f7ff37164dd04..152c7e714acd1 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js +++ b/crates/turbopack-tests/tests/snapshot/imports/static/output/crates_turbopack-tests_tests_snapshot_imports_static_input_index_885269.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js index 1faef93b5247d..1dd789077ec36 100644 --- a/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js +++ b/crates/turbopack-tests/tests/snapshot/node/node_protocol_external/output/79fb1_turbopack-tests_tests_snapshot_node_node_protocol_external_input_index_667edf.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js index dde419d9c2660..d8806e4c3cb4b 100644 --- a/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js +++ b/crates/turbopack-tests/tests/snapshot/styled_components/styled_components/output/a587c_tests_snapshot_styled_components_styled_components_input_index_afc482.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js index 90df7488d675b..4a194d553e25a 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/mono_transforms/output/a587c_tests_snapshot_swc_transforms_mono_transforms_input_packages_app_index_4a3d65.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_9dcfd0.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_9dcfd0.js index 415413abf8d6e..92c32027da470 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_9dcfd0.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_9dcfd0.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js index 7f96583a5436f..f31da5f173e91 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/jsconfig-baseurl/output/79fb1_turbopack-tests_tests_snapshot_typescript_jsconfig-baseurl_input_index_8f1e58.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js index 66e3b0df0eb2c..7675e4c7494ad 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-baseurl/output/a587c_tests_snapshot_typescript_tsconfig-baseurl_input_index.ts_0aa04e._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module-full-path/output/8562f_snapshot_typescript_tsconfig-extends-module-full-path_input_index.ts_a751eb._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module-full-path/output/8562f_snapshot_typescript_tsconfig-extends-module-full-path_input_index.ts_a751eb._.js index ae2bfdedff4fb..b96e1170da3e2 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module-full-path/output/8562f_snapshot_typescript_tsconfig-extends-module-full-path_input_index.ts_a751eb._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module-full-path/output/8562f_snapshot_typescript_tsconfig-extends-module-full-path_input_index.ts_a751eb._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module/output/a587c_tests_snapshot_typescript_tsconfig-extends-module_input_index.ts_a662d4._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module/output/a587c_tests_snapshot_typescript_tsconfig-extends-module_input_index.ts_a662d4._.js index c1b244a7921f3..288c4b661fb0f 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module/output/a587c_tests_snapshot_typescript_tsconfig-extends-module_input_index.ts_a662d4._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-module/output/a587c_tests_snapshot_typescript_tsconfig-extends-module_input_index.ts_a662d4._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-relative-dir/output/a587c_tests_snapshot_typescript_tsconfig-extends-relative-dir_input_index.ts_be3d7b._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-relative-dir/output/a587c_tests_snapshot_typescript_tsconfig-extends-relative-dir_input_index.ts_be3d7b._.js index 526f49bfd1564..765269862787b 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-relative-dir/output/a587c_tests_snapshot_typescript_tsconfig-extends-relative-dir_input_index.ts_be3d7b._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-relative-dir/output/a587c_tests_snapshot_typescript_tsconfig-extends-relative-dir_input_index.ts_be3d7b._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-without-ext/output/a587c_tests_snapshot_typescript_tsconfig-extends-without-ext_input_index.ts_38aae8._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-without-ext/output/a587c_tests_snapshot_typescript_tsconfig-extends-without-ext_input_index.ts_38aae8._.js index 2c3a7fbdde133..fa745333aa513 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-without-ext/output/a587c_tests_snapshot_typescript_tsconfig-extends-without-ext_input_index.ts_38aae8._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends-without-ext/output/a587c_tests_snapshot_typescript_tsconfig-extends-without-ext_input_index.ts_38aae8._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; } diff --git a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends/output/a587c_tests_snapshot_typescript_tsconfig-extends_input_index.ts_18c083._.js b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends/output/a587c_tests_snapshot_typescript_tsconfig-extends_input_index.ts_18c083._.js index 9f230f9ea0fcb..cfc5d9b63be4f 100644 --- a/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends/output/a587c_tests_snapshot_typescript_tsconfig-extends_input_index.ts_18c083._.js +++ b/crates/turbopack-tests/tests/snapshot/typescript/tsconfig-extends/output/a587c_tests_snapshot_typescript_tsconfig-extends_input_index.ts_18c083._.js @@ -491,8 +491,9 @@ function instantiateModule(id, source) { break; } - runModuleExecutionHooks(module, (refresh) => { - try { + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. + try { + runModuleExecutionHooks(module, (refresh) => { moduleFactory.call(module.exports, { e: module.exports, r: commonJsRequire.bind(null, module), @@ -510,11 +511,11 @@ function instantiateModule(id, source) { k: refresh, __dirname: module.id.replace(/(^|\/)[\/]+$/, ""), }); - } catch (error) { - module.error = error; - throw error; - } - }); + }); + } catch (error) { + module.error = error; + throw error; + } module.loaded = true; if (module.namespaceObject && module.exports !== module.namespaceObject) { @@ -539,21 +540,26 @@ function runModuleExecutionHooks(module, executeModule) { ? globalThis.$RefreshInterceptModuleExecution$(module.id) : () => {}; - executeModule({ - register: globalThis.$RefreshReg$, - signature: globalThis.$RefreshSig$, - }); + try { + executeModule({ + register: globalThis.$RefreshReg$, + signature: globalThis.$RefreshSig$, + }); - if ("$RefreshHelpers$" in globalThis) { - // This pattern can also be used to register the exports of - // a module with the React Refresh runtime. - registerExportsAndSetupBoundaryForReactRefresh( - module, - globalThis.$RefreshHelpers$ - ); + if ("$RefreshHelpers$" in globalThis) { + // This pattern can also be used to register the exports of + // a module with the React Refresh runtime. + registerExportsAndSetupBoundaryForReactRefresh( + module, + globalThis.$RefreshHelpers$ + ); + } + } catch (e) { + throw e; + } finally { + // Always cleanup the intercept, even if module execution failed. + cleanupReactRefreshIntercept(); } - - cleanupReactRefreshIntercept(); } /** @@ -1427,6 +1433,9 @@ function instantiateRuntimeModule(moduleId, chunkPath) { function getOrInstantiateRuntimeModule(moduleId, chunkPath) { const module = moduleCache[moduleId]; if (module) { + if (module.error) { + throw module.error; + } return module; }