From 0a2560e3a97a2cec0ad5cd608592a407ae5400a2 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 3 Jun 2025 10:50:27 +0000 Subject: [PATCH 1/2] fix: do not load electron from ESM loader's CJS compatibility Co-authored-by: clavin --- ..._do_not_resolve_electron_entrypoints.patch | 13 -------- ...n_electron_module_via_the_esm_loader.patch | 33 ++++++++++++++++++- ...in_esm_loaders_to_apply_asar_patches.patch | 2 +- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/patches/node/fix_do_not_resolve_electron_entrypoints.patch b/patches/node/fix_do_not_resolve_electron_entrypoints.patch index 68ec69cf3ee8f..5a7bbe40231e6 100644 --- a/patches/node/fix_do_not_resolve_electron_entrypoints.patch +++ b/patches/node/fix_do_not_resolve_electron_entrypoints.patch @@ -5,19 +5,6 @@ Subject: fix: do not resolve electron entrypoints This wastes fs cycles and can result in strange behavior if this path actually exists on disk -diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js -index c9d4a3536d0f60375ae623b48ca2fa7095c88d42..d818320fbbc430d06a0c2852e4723981d6e1a844 100644 ---- a/lib/internal/modules/esm/load.js -+++ b/lib/internal/modules/esm/load.js -@@ -109,7 +109,7 @@ async function defaultLoad(url, context = kEmptyObject) { - source = null; - format ??= 'builtin'; - } else if (format !== 'commonjs' || defaultType === 'module') { -- if (source == null) { -+ if (format !== 'electron' && source == null) { - ({ responseURL, source } = await getSource(urlInstance, context)); - context = { __proto__: context, source }; - } diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 49aacb6262502ced54e817f99dd596db85b9659c..f9f065bb743275e9b2ce71375e6a9f06e00c0f36 100644 --- a/lib/internal/modules/esm/translators.js diff --git a/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch b/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch index baa89612d412d..93f77631b35c3 100644 --- a/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch +++ b/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch @@ -18,9 +18,18 @@ index 9519f947b8dfdc69808839948c9cb8434a0acf0e..23ce72d479f638c33edffcea7c35f5da /** diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js -index 5ba13096b98047ff33e4d44167c2a069ccc5e69d..a00b5979e3b5deb4ba315b4635c7e5d2801c376e 100644 +index 5ba13096b98047ff33e4d44167c2a069ccc5e69d..09a332c0999086b30fd952d9456f788925240e27 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js +@@ -106,7 +106,7 @@ async function defaultLoad(url, context = kEmptyObject) { + + throwIfUnsupportedURLScheme(urlInstance); + +- if (urlInstance.protocol === 'node:') { ++ if (urlInstance.protocol === 'node:' || format === 'electron') { + source = null; + format ??= 'builtin'; + } else if (format !== 'commonjs' || defaultType === 'module') { @@ -119,7 +119,7 @@ async function defaultLoad(url, context = kEmptyObject) { // Now that we have the source for the module, run `defaultGetFormat` to detect its format. format = await defaultGetFormat(urlInstance, context); @@ -30,6 +39,15 @@ index 5ba13096b98047ff33e4d44167c2a069ccc5e69d..a00b5979e3b5deb4ba315b4635c7e5d2 // For backward compatibility reasons, we need to discard the source in // order for the CJS loader to re-fetch it. source = null; +@@ -167,7 +167,7 @@ function defaultLoadSync(url, context = kEmptyObject) { + + throwIfUnsupportedURLScheme(urlInstance, false); + +- if (urlInstance.protocol === 'node:') { ++ if (urlInstance.protocol === 'node:' || format === 'electron') { + source = null; + } else if (source == null) { + ({ responseURL, source } = getSourceSync(urlInstance, context)); @@ -200,12 +200,13 @@ function throwIfUnsupportedURLScheme(parsed) { protocol !== 'file:' && protocol !== 'data:' && @@ -45,6 +63,19 @@ index 5ba13096b98047ff33e4d44167c2a069ccc5e69d..a00b5979e3b5deb4ba315b4635c7e5d2 throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, schemes); } } +diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js +index fdecd0c926c953d33117d3d05366261ccf034a21..2e1ddd821ea6254fe00c52292620c3a58a62c046 100644 +--- a/lib/internal/modules/esm/loader.js ++++ b/lib/internal/modules/esm/loader.js +@@ -426,7 +426,7 @@ class ModuleLoader { + } + + const cjsModule = wrap[imported_cjs_symbol]; +- if (cjsModule) { ++ if (cjsModule && finalFormat !== 'electron') { + assert(finalFormat === 'commonjs-sync'); + // Check if the ESM initiating import CJS is being required by the same CJS module. + if (cjsModule?.[kIsExecuting]) { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index bfd9bd3d127404de1cbb6f30c43ab0342590759d..9e7d8ef0adef3b68a3ec186e4b218f591aa69266 100644 --- a/lib/internal/modules/esm/resolve.js diff --git a/patches/node/fix_lazyload_fs_in_esm_loaders_to_apply_asar_patches.patch b/patches/node/fix_lazyload_fs_in_esm_loaders_to_apply_asar_patches.patch index 8de78da28f2fe..3eb241ca1eb19 100644 --- a/patches/node/fix_lazyload_fs_in_esm_loaders_to_apply_asar_patches.patch +++ b/patches/node/fix_lazyload_fs_in_esm_loaders_to_apply_asar_patches.patch @@ -6,7 +6,7 @@ Subject: fix: lazyload fs in esm loaders to apply asar patches Changes { foo } from fs to just "fs.foo" so that our patching of fs is applied to esm loaders diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js -index a00b5979e3b5deb4ba315b4635c7e5d2801c376e..c9d4a3536d0f60375ae623b48ca2fa7095c88d42 100644 +index 09a332c0999086b30fd952d9456f788925240e27..910ac85cdc86e7fb3f850f7edf0b95ea09d464dc 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -10,7 +10,7 @@ const { From 175ba1bd6f4775e1236772d563a6bb5cc1cfebc4 Mon Sep 17 00:00:00 2001 From: clavin Date: Wed, 4 Jun 2025 16:03:00 -0600 Subject: [PATCH 2/2] chore: upgrade patches --- ...pose_the_built-in_electron_module_via_the_esm_loader.patch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch b/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch index 93f77631b35c3..3fbb7b9faf5c4 100644 --- a/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch +++ b/patches/node/fix_expose_the_built-in_electron_module_via_the_esm_loader.patch @@ -64,10 +64,10 @@ index 5ba13096b98047ff33e4d44167c2a069ccc5e69d..09a332c0999086b30fd952d9456f7889 } } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js -index fdecd0c926c953d33117d3d05366261ccf034a21..2e1ddd821ea6254fe00c52292620c3a58a62c046 100644 +index ae03073aff8140b11c63b6c05d831ba573568dba..b70c7cfe40e2eaaeea7b5ad6fcf0aaee87276aa1 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js -@@ -426,7 +426,7 @@ class ModuleLoader { +@@ -492,7 +492,7 @@ class ModuleLoader { } const cjsModule = wrap[imported_cjs_symbol];