Browse Source

chore: remove Node.js patch on Module.globalPaths (#31275)

* chore: remove Node.js patch on Module.globalPaths

* chore: update patches

Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Cheng Zhao 3 years ago
parent
commit
959b657903

+ 0 - 3
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') {

+ 0 - 1
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

+ 1 - 1
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) {

+ 0 - 22
patches/node/make_module_globalpaths_a_reference.patch

@@ -1,22 +0,0 @@
-From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
-From: Cheng Zhao <[email protected]>
-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) {

+ 1 - 1
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 {

+ 1 - 1
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');

+ 2 - 1
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 -

+ 5 - 3
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);

+ 9 - 0
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;

+ 13 - 0
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);
+    }
+  };
+};

+ 4 - 2
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