src: fix module search path for preload modules
authorAli Ijaz Sheikh <ofrobots@google.com>
Wed, 27 May 2015 17:11:30 +0000 (10:11 -0700)
committerJeremiah Senkpiel <fishrock123@rocketmail.com>
Sat, 30 May 2015 14:36:09 +0000 (10:36 -0400)
When the preload module is not a abs/relative path, we should use
the standard search mechanism of looking into the node_modules folders
outwards. The current working directory is deemed to be the 'requiring
module', i.e. parent. The search path starts from cwd outwards.

Fixes: https://github.com/nodejs/io.js/issues/1803
PR-URL: https://github.com/nodejs/io.js/pull/1812
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
lib/module.js
src/node.js
test/fixtures/cluster-preload.js

index 65e44b6..844b683 100644 (file)
@@ -503,6 +503,20 @@ Module.requireRepl = function() {
   return Module._load('internal/repl', '.');
 };
 
+Module._preloadModules = function(requests) {
+  if (!Array.isArray(requests))
+    return;
+
+  // Preloaded modules have a dummy parent module which is deemed to exist
+  // in the current working directory. This seeds the search path for
+  // preloaded modules.
+  var parent = new Module('internal/preload', null);
+  parent.paths = Module._nodeModulePaths(process.cwd());
+  requests.forEach(function(request) {
+    Module._load(request, parent, false);
+  });
+};
+
 Module._initPaths();
 
 // backwards compatibility
index 4521afb..2516478 100644 (file)
   // Load preload modules
   startup.preloadModules = function() {
     if (process._preload_modules) {
-      var Module = NativeModule.require('module');
-      process._preload_modules.forEach(function(module) {
-        Module._load(module);
-      });
+      NativeModule.require('module')._preloadModules(process._preload_modules);
     }
   };
 
index 651fc48..3063e5b 100644 (file)
@@ -1,3 +1,12 @@
+var assert = require('assert');
+
+// https://github.com/nodejs/io.js/issues/1803
+// this module is used as a preload module. It should have a parent with the
+// module search paths initialized from the current working directory
+assert.ok(module.parent);
+var expectedPaths = require('module')._nodeModulePaths(process.cwd());
+assert.deepEqual(module.parent.paths, expectedPaths);
+
 var cluster = require('cluster');
 cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
 // from exit(1) for other random reasons