diff --git a/lib/common/reset-search-paths.ts b/lib/common/reset-search-paths.ts index d3294735bc06..82358923599a 100644 --- a/lib/common/reset-search-paths.ts +++ b/lib/common/reset-search-paths.ts @@ -2,9 +2,6 @@ import * as path from 'path'; const Module = require('module'); -// Clear Node's global search paths. -Module.globalPaths.length = 0; - // We do not want to allow use of the VM module in the renderer process as // it conflicts with Blink's V8::Context internal logic. if (process.type === 'renderer') { diff --git a/patches/node/.patches b/patches/node/.patches index 662a3aef0cc7..7df70532b53e 100644 --- a/patches/node/.patches +++ b/patches/node/.patches @@ -1,4 +1,3 @@ -make_module_globalpaths_a_reference.patch refactor_alter_child_process_fork_to_use_execute_script_with.patch feat_add_uv_loop_watcher_queue_code.patch feat_initialize_asar_support.patch diff --git a/patches/node/chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch b/patches/node/chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch index 7bff759026a3..36696089dbb6 100644 --- a/patches/node/chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch +++ b/patches/node/chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch @@ -29,7 +29,7 @@ index 9d2799c3c9ac3b216c2289ae4e037dd228844d23..5b31df1207d4417a6f9b784574e37796 // TODO(joyeecheung): most of these should be deprecated and removed, diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js -index 40655b66fd2a50dac01f88d38457de4602022381..95147ec8a95761b0654e8403578c2c67191834ad 100644 +index 5d75a0e6cb9692a303be6599226f20783e7863b8..eb7f4346035f0897d0f2b140277e48b3c7bb7546 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1075,6 +1075,13 @@ Module.prototype._compile = function(content, filename) { diff --git a/patches/node/make_module_globalpaths_a_reference.patch b/patches/node/make_module_globalpaths_a_reference.patch deleted file mode 100644 index f9d8cc442d4b..000000000000 --- a/patches/node/make_module_globalpaths_a_reference.patch +++ /dev/null @@ -1,22 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Cheng Zhao -Date: Sun, 14 Apr 2013 15:54:46 +0800 -Subject: Make Module.globalPaths a reference. - -We need to hack the search paths of the require function so we can -load libraries from embedded applications without modifications of -node's module code. - -diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js -index 3f1dac356b81f437503422a8fb69df792db9c523..8258a46076d1d4905298016cb52247e2cf8432c5 100644 ---- a/lib/internal/modules/cjs/loader.js -+++ b/lib/internal/modules/cjs/loader.js -@@ -1251,7 +1251,7 @@ Module._initPaths = function() { - modulePaths = paths; - - // Clone as a shallow copy, for introspection. -- Module.globalPaths = ArrayPrototypeSlice(modulePaths); -+ Module.globalPaths = modulePaths; - }; - - Module._preloadModules = function(requests) { diff --git a/patches/node/pass_all_globals_through_require.patch b/patches/node/pass_all_globals_through_require.patch index 80cce0029686..a1211d473d41 100644 --- a/patches/node/pass_all_globals_through_require.patch +++ b/patches/node/pass_all_globals_through_require.patch @@ -6,7 +6,7 @@ Subject: Pass all globals through "require" (cherry picked from commit 7d015419cb7a0ecfe6728431a4ed2056cd411d62) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js -index 8258a46076d1d4905298016cb52247e2cf8432c5..71059956705d1299a19cc9c940a998d9e9717a21 100644 +index 3f1dac356b81f437503422a8fb69df792db9c523..dfaea25243826878afc0af96a77d0aef153fa76b 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -127,6 +127,13 @@ const { diff --git a/patches/node/refactor_allow_embedder_overriding_of_internal_fs_calls.patch b/patches/node/refactor_allow_embedder_overriding_of_internal_fs_calls.patch index 7e04eb6d931a..ea982f5be201 100644 --- a/patches/node/refactor_allow_embedder_overriding_of_internal_fs_calls.patch +++ b/patches/node/refactor_allow_embedder_overriding_of_internal_fs_calls.patch @@ -22,7 +22,7 @@ index 58f7396990dddb7dd4cf3d23fcdcc1d48f52623e..ef06d0563fa7452348754418867a56c9 const nativeModule = internalBinding('native_module'); diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js -index 71059956705d1299a19cc9c940a998d9e9717a21..40655b66fd2a50dac01f88d38457de4602022381 100644 +index dfaea25243826878afc0af96a77d0aef153fa76b..5d75a0e6cb9692a303be6599226f20783e7863b8 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -86,7 +86,7 @@ const fs = require('fs'); diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index ccf92b74b840..4a2cfb80a3d2 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -467,7 +467,8 @@ node::Environment* NodeBindings::CreateEnvironment( node::Environment* env; uint64_t flags = node::EnvironmentFlags::kDefaultFlags | - node::EnvironmentFlags::kHideConsoleWindows; + node::EnvironmentFlags::kHideConsoleWindows | + node::EnvironmentFlags::kNoGlobalSearchPaths; if (browser_env_ != BrowserEnvironment::kBrowser) { // Only one ESM loader can be registered per isolate - diff --git a/spec-main/index.js b/spec-main/index.js index 40c783093027..6579025ee757 100644 --- a/spec-main/index.js +++ b/spec-main/index.js @@ -1,16 +1,18 @@ -const Module = require('module'); const path = require('path'); const v8 = require('v8'); -Module.globalPaths.push(path.resolve(__dirname, '../spec/node_modules')); +module.paths.push(path.resolve(__dirname, '../spec/node_modules')); // Extra module paths which can be used to load Mocha reporters if (process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS) { for (const modulePath of process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS.split(':')) { - Module.globalPaths.push(modulePath); + module.paths.push(modulePath); } } +// Add search paths for loaded spec files +require('../spec/global-paths')(module.paths); + // We want to terminate on errors, not throw up a dialog process.on('uncaughtException', (err) => { console.error('Unhandled exception in main spec runner:', err); diff --git a/spec-main/modules-spec.ts b/spec-main/modules-spec.ts index 9c5a2f44a57e..8554bc525dab 100644 --- a/spec-main/modules-spec.ts +++ b/spec-main/modules-spec.ts @@ -108,6 +108,15 @@ describe('modules support', () => { }); describe('Module._nodeModulePaths', () => { + // Work around the hack in spec/global-paths. + beforeEach(() => { + Module.ignoreGlobalPathsHack = true; + }); + + afterEach(() => { + Module.ignoreGlobalPathsHack = false; + }); + describe('when the path is inside the resources path', () => { it('does not include paths outside of the resources path', () => { let modulePath = process.resourcesPath; diff --git a/spec/global-paths.js b/spec/global-paths.js new file mode 100644 index 000000000000..fdad3fd3dd6f --- /dev/null +++ b/spec/global-paths.js @@ -0,0 +1,13 @@ +const Module = require('module'); + +module.exports = (paths) => { + const nodeModulePaths = Module._nodeModulePaths; + Module.ignoreGlobalPathsHack = false; + Module._nodeModulePaths = (from) => { + if (Module.ignoreGlobalPathsHack) { + return nodeModulePaths(from); + } else { + return nodeModulePaths(from).concat(paths); + } + }; +}; diff --git a/spec/static/index.html b/spec/static/index.html index 1f710fc2b277..773d715fe453 100644 --- a/spec/static/index.html +++ b/spec/static/index.html @@ -5,7 +5,6 @@ // Deprecated APIs are still supported and should be tested. process.throwDeprecation = false - const Module = require('module'); const path = require('path') const electron = require('electron') const { ipcRenderer } = electron @@ -13,10 +12,13 @@ // Extra module paths which can be used to load Mocha reporters if (process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS) { for (const modulePath of process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS.split(':')) { - Module.globalPaths.push(modulePath); + module.paths.push(modulePath); } } + // Add search paths for loaded spec files + require('../global-paths')(module.paths); + // Set up chai-as-promised here first to avoid conflicts // It must be loaded first or really strange things happen inside // chai that cause test failures