From 75487f0db80e70a3e27fabfe323a33258dfbbea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 15 Apr 2016 13:32:36 +0200 Subject: [PATCH] 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] https://github.com/nodejs/node/pull/5172/commits/ae18bbef48d87d9c641df85369f62cfd5ed8c250 Fixes: https://github.com/nodejs/node/issues/6214 PR-URL: https://github.com/nodejs/node/pull/6215 Reviewed-By: James M Snell Reviewed-By: Brian White --- lib/module.js | 2 ++ .../node_modules/module1/package.json | 3 +++ .../module-require/not-found/trailingSlash.js | 1 + test/parallel/test-require-exceptions.js | 24 ++++++++++++++----- 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/module-require/not-found/node_modules/module1/package.json create mode 100644 test/fixtures/module-require/not-found/trailingSlash.js diff --git a/lib/module.js b/lib/module.js index 29a23776d790d2..f633d62c490c39 100644 --- a/lib/module.js +++ b/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); } diff --git a/test/fixtures/module-require/not-found/node_modules/module1/package.json b/test/fixtures/module-require/not-found/node_modules/module1/package.json new file mode 100644 index 00000000000000..b5018d73b218ea --- /dev/null +++ b/test/fixtures/module-require/not-found/node_modules/module1/package.json @@ -0,0 +1,3 @@ +{ + "main": "doesnotexist" +} diff --git a/test/fixtures/module-require/not-found/trailingSlash.js b/test/fixtures/module-require/not-found/trailingSlash.js new file mode 100644 index 00000000000000..4ac6cba9ca82ed --- /dev/null +++ b/test/fixtures/module-require/not-found/trailingSlash.js @@ -0,0 +1 @@ +require('module1/'); diff --git a/test/parallel/test-require-exceptions.js b/test/parallel/test-require-exceptions.js index e7a191b47ad6f2..0e61ad2f3fd703 100644 --- a/test/parallel/test-require-exceptions.js +++ b/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); - return true; -}); +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)); +}