From 7384ca83f97a28b0cecaabe879e9af0fe8631b62 Mon Sep 17 00:00:00 2001 From: Chris Yip Date: Tue, 21 Apr 2015 15:13:27 +0800 Subject: [PATCH] module: remove '' from Module.globalPaths If `$NODE_PATH` contains trailing separators, `Module.globalPaths` will contains empty strings. When `Module` try to resolve a module's path, `path.resolve('', 'index.js')` will boil down to `$PWD/index.js`, which makes sub modules can access global modules and get unexpected result. PR-URL: https://github.com/iojs/io.js/pull/1488 Reviewed-By: Roman Reiss --- lib/module.js | 4 +++- test/parallel/test-module-globalpaths-nodepath.js | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/module.js b/lib/module.js index 02f0ec7..eba3de8 100644 --- a/lib/module.js +++ b/lib/module.js @@ -489,7 +489,9 @@ Module._initPaths = function() { var nodePath = process.env['NODE_PATH']; if (nodePath) { - paths = nodePath.split(path.delimiter).concat(paths); + paths = nodePath.split(path.delimiter).filter(function(path) { + return !!path; + }).concat(paths); } modulePaths = paths; diff --git a/test/parallel/test-module-globalpaths-nodepath.js b/test/parallel/test-module-globalpaths-nodepath.js index d0261e8..6cfa179 100644 --- a/test/parallel/test-module-globalpaths-nodepath.js +++ b/test/parallel/test-module-globalpaths-nodepath.js @@ -6,20 +6,22 @@ var module = require('module'); var isWindows = process.platform === 'win32'; var partA, partB; +var partC = ''; if (isWindows) { partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm'; partB = 'C:\\Program Files (x86)\\nodejs\\'; - process.env['NODE_PATH'] = partA + ';' + partB; + process.env['NODE_PATH'] = partA + ';' + partB + ';' + partC; } else { partA = '/usr/test/lib/node_modules'; partB = '/usr/test/lib/node'; - process.env['NODE_PATH'] = partA + ':' + partB; + process.env['NODE_PATH'] = partA + ':' + partB + ':' + partC; } module._initPaths(); assert.ok(module.globalPaths.indexOf(partA) !== -1); assert.ok(module.globalPaths.indexOf(partB) !== -1); +assert.ok(module.globalPaths.indexOf(partC) === -1); -assert.ok(Array.isArray(module.globalPaths)); \ No newline at end of file +assert.ok(Array.isArray(module.globalPaths)); -- 2.7.4