fix: avoid package.json check on built-in modules (#39418)

This commit is contained in:
Samuel Attard 2023-08-09 00:32:00 -07:00 committed by GitHub
parent ab08803959
commit d24d8f1f78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,19 +6,42 @@ Subject: fix: do not resolve electron entrypoints
This wastes fs cycles and can result in strange behavior if this path actually exists on disk This wastes fs cycles and can result in strange behavior if this path actually exists on disk
diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js
index 5a50d5d6afab6e6648f72a1c0efa1df4cd80bcd9..0be45309028b00a6957ee473322a9452a7fa7d67 100644 index 5a50d5d6afab6e6648f72a1c0efa1df4cd80bcd9..ebc9999358ccf16689dc02322eb1aeb86355f39b 100644
--- a/lib/internal/modules/run_main.js --- a/lib/internal/modules/run_main.js
+++ b/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js
@@ -13,6 +13,12 @@ const { @@ -3,6 +3,7 @@
const {
ObjectCreate,
StringPrototypeEndsWith,
+ StringPrototypeStartsWith,
} = primordials;
const CJSLoader = require('internal/modules/cjs/loader');
const { Module, toRealPath, readPackageScope } = CJSLoader;
@@ -13,6 +14,13 @@ const {
} = require('internal/modules/esm/handle_process_exit'); } = require('internal/modules/esm/handle_process_exit');
function resolveMainPath(main) { function resolveMainPath(main) {
+ // For built-in modules used as the main entry point we _never_ + // For built-in modules used as the main entry point we _never_
+ // want to waste cycles resolving them to file paths on disk + // want to waste cycles resolving them to file paths on disk
+ // that actually might exist + // that actually might exist
+ if (typeof main === 'string' && main.startsWith('electron/js2c')) { + if (typeof main === 'string' && StringPrototypeStartsWith(main, 'electron/js2c')) {
+ return main; + return main;
+ } + }
+
// Note extension resolution for the main entry point can be deprecated in a // Note extension resolution for the main entry point can be deprecated in a
// future major. // future major.
// Module._findPath is monkey-patchable here. // Module._findPath is monkey-patchable here.
@@ -28,6 +36,13 @@ function resolveMainPath(main) {
}
function shouldUseESMLoader(mainPath) {
+ // For built-in modules used as the main entry point we _never_
+ // want to waste cycles resolving them to file paths on disk
+ // that actually might exist
+ if (typeof mainPath === 'string' && StringPrototypeStartsWith(mainPath, 'electron/js2c')) {
+ return false;
+ }
+
/**
* @type {string[]} userLoaders A list of custom loaders registered by the user
* (or an empty list when none have been registered).