From 8c9320eaad848b673f62101576a999247399ced9 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 27 May 2015 10:11:30 -0700 Subject: [PATCH] src: fix module search path for preload modules 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: #1803 --- lib/module.js | 13 +++++++++++++ src/node.js | 5 +---- test/fixtures/cluster-preload.js | 12 ++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/module.js b/lib/module.js index 515ab6789ca658..520a3df32bd58d 100644 --- a/lib/module.js +++ b/lib/module.js @@ -508,6 +508,19 @@ Module.requireRepl = function() { return Module._load('internal/repl', '.'); }; +Module._preloadModules = function(requests) { + if (requests) { + // 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 diff --git a/src/node.js b/src/node.js index 065c337e743993..b07716703cb19a 100644 --- a/src/node.js +++ b/src/node.js @@ -858,10 +858,7 @@ // 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); } }; diff --git a/test/fixtures/cluster-preload.js b/test/fixtures/cluster-preload.js index 651fc480e8b84a..23e020aa20e5b1 100644 --- a/test/fixtures/cluster-preload.js +++ b/test/fixtures/cluster-preload.js @@ -1,3 +1,15 @@ +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.ok(module.parent.paths.length === expectedPaths.length && + module.parent.paths.every(function(e,i) { + return e === expectedPaths[i]; + })); + var cluster = require('cluster'); cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish // from exit(1) for other random reasons