module: remove '' from Module.globalPaths
authorChris Yip <i@chrisyip.im>
Tue, 21 Apr 2015 07:13:27 +0000 (15:13 +0800)
committerRoman Reiss <me@silverwind.io>
Thu, 23 Apr 2015 14:57:50 +0000 (16:57 +0200)
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 <me@silverwind.io>
lib/module.js
test/parallel/test-module-globalpaths-nodepath.js

index 02f0ec7..eba3de8 100644 (file)
@@ -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;
index d0261e8..6cfa179 100644 (file)
@@ -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));