module: check file ext before dir as documented

The documented resolution algorithm started to search for package.json
files prior to searching for file extensions when searching for a
specifier. Oddly, it did not search for index files at same time it
searched for package.json. This restores the documented behavior of
searching for file extensions prior to searching directories.

PR-URL: https://github.com/nodejs/node/pull/15015
Fixes: https://github.com/nodejs/node/issues/14990
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
This commit is contained in:
Bradley Farias 2017-08-24 09:59:52 -05:00 committed by Ruben Bridgewater
parent 8b69d4aca9
commit 1ed36aeb53
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
4 changed files with 42 additions and 7 deletions

View File

@ -219,6 +219,9 @@ Module._findPath = function(request, paths, isMain) {
var exts;
var trailingSlash = request.length > 0 &&
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
if (!trailingSlash) {
trailingSlash = /(?:^|\/)\.?\.$/.test(request);
}
// For each path
for (var i = 0; i < paths.length; i++) {
@ -236,10 +239,6 @@ Module._findPath = function(request, paths, isMain) {
} else {
filename = toRealPath(basePath);
}
} else if (rc === 1) { // Directory.
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
}
if (!filename) {
@ -251,11 +250,13 @@ Module._findPath = function(request, paths, isMain) {
}
if (!filename && rc === 1) { // Directory.
// try it with each of the extensions at "index"
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain) ||
// try it with each of the extensions at "index"
tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
filename = tryPackage(basePath, exts, isMain);
if (!filename) {
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
}
}
if (filename) {

View File

@ -0,0 +1 @@
module.exports = {};

View File

@ -0,0 +1,3 @@
{
"main": "./package.json"
}

View File

@ -0,0 +1,30 @@
'use strict';
// fixes regression from v4
require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const path = require('path');
const fixturesRequire = require(
fixtures.path('module-extension-over-directory', 'inner'));
assert.strictEqual(
fixturesRequire,
require(fixtures.path('module-extension-over-directory', 'inner.js')),
'test-require-extension-over-directory failed to import fixture' +
' requirements'
);
const fakePath = [
fixtures.path('module-extension-over-directory', 'inner'),
'fake',
'..'
].join(path.sep);
const fixturesRequireDir = require(fakePath);
assert.strictEqual(
fixturesRequireDir,
require(fixtures.path('module-extension-over-directory', 'inner/')),
'test-require-extension-over-directory failed to import fixture' +
' requirements'
);