Browse Source

module: fix resolution of filename with trailing slash

A recent optimization of module loading performance [1] forgot to check that
extensions were set in a certain code path.

[1] ae18bbef48

Fixes: https://github.com/nodejs/node/issues/6214
PR-URL: https://github.com/nodejs/node/pull/6215
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
process-exit-stdio-flushing
Michaël Zasso 9 years ago
committed by James M Snell
parent
commit
75487f0db8
  1. 2
      lib/module.js
  2. 3
      test/fixtures/module-require/not-found/node_modules/module1/package.json
  3. 1
      test/fixtures/module-require/not-found/trailingSlash.js
  4. 22
      test/parallel/test-require-exceptions.js

2
lib/module.js

@ -170,6 +170,8 @@ Module._findPath = function(request, paths) {
}
if (!filename) {
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts);
}

3
test/fixtures/module-require/not-found/node_modules/module1/package.json

@ -0,0 +1,3 @@
{
"main": "doesnotexist"
}

1
test/fixtures/module-require/not-found/trailingSlash.js

@ -0,0 +1 @@
require('module1/');

22
test/parallel/test-require-exceptions.js

@ -14,9 +14,21 @@ assert.throws(function() {
// Requiring a module that does not exist should throw an
// error with its `code` set to MODULE_NOT_FOUND
assert.throws(function() {
require(common.fixturesDir + '/DOES_NOT_EXIST');
}, function(e) {
assert.equal('MODULE_NOT_FOUND', e.code);
assertModuleNotFound('/DOES_NOT_EXIST');
assertExists('/module-require/not-found/trailingSlash.js');
assertExists('/module-require/not-found/node_modules/module1/package.json');
assertModuleNotFound('/module-require/not-found/trailingSlash');
function assertModuleNotFound(path) {
assert.throws(function() {
require(common.fixturesDir + path);
}, function(e) {
assert.strictEqual(e.code, 'MODULE_NOT_FOUND');
return true;
});
});
}
function assertExists(fixture) {
assert(common.fileExists(common.fixturesDir + fixture));
}

Loading…
Cancel
Save